Error in Tax Return GUI - java

I've been trying to make this Tax Return Program to work but im having some difficulties. It runs sure, but the "Calculation Button" does not work.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Tax extends JFrame
{
private int NewNetPay = 0;
private JPanel panelN;
private JPanel panelS;
private JPanel panelE;
private JPanel panelW;
private JPanel panelC;
private JButton calcButton;
private JButton resetButton;
private JButton exitButton;
private JRadioButton hohRB;
private JRadioButton marriedJRB;
private JRadioButton marriedSRB;
private JRadioButton singleRB;
private JCheckBox mortgageCB;
private JCheckBox charitableCB;
private JCheckBox childCB;
private JCheckBox educationCB;
private JTextField childTF;
private JTextField mortgageTF;
private JTextField charitableTF;
private JTextField educationTF;
private JLabel firstName;
private JTextField name1TF;
private JLabel lastName;
private JTextField name2TF;
private JLabel gross;
private JTextField grossTF;
private JLabel netPay;
private JTextField netPayTF;
final int WIN_WIDTH = 500;
final int WIN_HEIGHT = 300;
public static void main (String[] args)
{
new Tax();
}
public Tax()
{
setTitle("CSC142 Tax Calculator");
setSize(WIN_WIDTH,WIN_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout(20, 20));
buildPanel();
//add(panel);
setVisible(true);
}
private void buildPanel()
{
JFrame frame = new JFrame();
hohRB = new JRadioButton("Head of Household");
marriedJRB = new JRadioButton("Married, Jointly");
marriedSRB = new JRadioButton("Married, Seperately");
singleRB = new JRadioButton("Single");
ButtonGroup radioGroup = new ButtonGroup();
radioGroup.add(hohRB);
radioGroup.add(marriedJRB);
radioGroup.add(marriedSRB);
radioGroup.add(singleRB);
mortgageCB = new JCheckBox("Mortgage");
charitableCB = new JCheckBox("Charitable Donation");
childCB = new JCheckBox("Child Deduction");
educationCB = new JCheckBox("Education Expenses");
ButtonGroup checkGroup = new ButtonGroup();
checkGroup.add(mortgageCB);
checkGroup.add(charitableCB);
checkGroup.add(childCB);
checkGroup.add(educationCB);
calcButton = new JButton(" Calculate Taxes ");
resetButton = new JButton(" Reset Values ");
exitButton = new JButton (" Exit Application ");
panelN = new JPanel();
panelS = new JPanel();
panelE = new JPanel();
panelW = new JPanel();
panelC = new JPanel();
add(panelN, BorderLayout.NORTH);
add(panelS, BorderLayout.SOUTH);
add(panelE, BorderLayout.EAST);
add(panelW, BorderLayout.WEST);
add(panelC, BorderLayout.CENTER);
panelN.setLayout(new BoxLayout(panelN, BoxLayout.X_AXIS));
panelS.setLayout(new BoxLayout(panelS, BoxLayout.X_AXIS));
panelE.setLayout(new BoxLayout(panelE, BoxLayout.Y_AXIS));
panelW.setLayout(new BoxLayout(panelW, BoxLayout.Y_AXIS));
panelC.setLayout(new BoxLayout(panelC, BoxLayout.Y_AXIS));
panelW.add(hohRB);
panelW.add(marriedJRB);
panelW.add(marriedSRB);
panelW.add(singleRB);
JTextField childTF = new JTextField();
childTF.setPreferredSize( new Dimension (10,10));
JTextField mortgageTF = new JTextField();
mortgageTF.setPreferredSize( new Dimension (10,10));
JTextField charitableTF = new JTextField();
charitableTF.setPreferredSize( new Dimension (10,10));
JTextField educationTF = new JTextField();
educationTF.setPreferredSize( new Dimension (10,10));
childTF.setEnabled(false);
mortgageTF.setEnabled(false);
charitableTF.setEnabled(false);
educationTF.setEnabled(false);
panelC.add(mortgageCB);
panelC.add(mortgageTF);
panelC.add(charitableCB);
panelC.add(charitableTF);
panelC.add(childCB);
panelC.add(childTF);
panelC.add(educationCB);
panelC.add(educationTF);
panelS.add(Box.createRigidArea(new Dimension(35,0)));
panelS.add(calcButton);
panelS.add(Box.createRigidArea(new Dimension(20,0)));
panelS.add(resetButton);
panelS.add(Box.createRigidArea(new Dimension(20,0)));
panelS.add(exitButton);
panelW.setBorder(BorderFactory.createTitledBorder("Filing Status"));
panelC.setBorder(BorderFactory.createTitledBorder("Deductions"));
JTextField name1TF = new JTextField();
JTextField name2TF = new JTextField();
JTextField grossTF = new JTextField();
JLabel firstName = new JLabel(" First Name ");
JLabel lastName = new JLabel(" Last Name ");
JLabel gross = new JLabel(" Gross Income ");
panelN.add(firstName);
panelN.add(name1TF);
panelN.add(lastName);
panelN.add(name2TF);
panelN.add(gross);
panelN.add(grossTF);
JLabel netPay = new JLabel("Net Pay");
JTextField netPayTF = new JTextField(1);
panelE.add(netPay);
panelE.add(netPayTF);
calcButton.addActionListener(new CalcButtonListener());
}
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String inputo;
inputo = grossTF.getText();
if (mortgageCB.isSelected())
NewNetPay = Integer.parseInt(inputo) - 10000;
else if (charitableCB.isSelected())
NewNetPay = Integer.parseInt(inputo) - 5000 ;
else if (childCB.isSelected())
NewNetPay = Integer.parseInt(inputo) - 1000;
else if (educationCB.isSelected())
NewNetPay = Integer.parseInt(inputo) - 20000;
netPayTF.setText(" " + NewNetPay);
}
}
} `enter code here`
I am trying to make it so that a user enters a salary in the gross income text field, then depending on what checkbox they click they will get a deduction. Then finally, the amount will be displayed in the net pay text field.

You are adding JTextField grossTF = new JTextField(); where grossTF is a local Variable to the frame, and trying to get data from inputo = grossTF.getText(); where this.grossTF which is a class member that would be initialized null when getting the data, hence you'd get a NPE.
Also you've done the same for netPayTF, change them to..
netPayTF = new JTextField(1); & grossTF = new JTextField();

Related

How to prevent writing in both TextField when I am writing in one TextField using this keypad?

I'm beginner in java and i stucked writing simple project using eclipse. I added numbered Buttons and they should be showed in TextField when user click them.
But when i'm trying to check this they add every number to both TextField.
How i can prevent this?
See Picture show it better:
private JFrame frame;
private final JPanel panel = new JPanel();
private final JButton btnNewButton_1 = new JButton("1");
private final JButton btnNewButton_1_1 = new JButton("2");
private final JButton btnNewButton_1_2 = new JButton("3");
private final JButton btnNewButton_1_3 = new JButton("4");
private final JButton btnNewButton_1_1_1 = new JButton("5");
private final JButton btnNewButton_1_2_1 = new JButton("6");
private final JButton btnNewButton_1_4 = new JButton("7");
private final JButton btnNewButton_1_1_2 = new JButton("8");
private final JButton btnNewButton_1_2_2 = new JButton("9");
private final JButton btnNewButton_1_1_3 = new JButton("0");
private final JButton btnNewButton_1_2_4 = new JButton("Enter");
private final JButton btnNewButton_1_2_4_1 = new JButton("Cancel");
private final JButton btnNewButton_1_2_4_2 = new JButton("Clear");
private final JLabel lblNewLabel_4 = new JLabel("Welcome..!");
private final JLabel lblNewLabel_5 = new JLabel("Account Number:");
private final JLabel lblNewLabel_5_1 = new JLabel("PIN:");
private final JTextField textField = new JTextField();
private final JPasswordField passwordField = new JPasswordField();
private void initialize() {
frame = new JFrame();
frame.setBounds(150, 10, 1000, 650);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.getContentPane().add(panel);
panel.setLayout(null);
btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 23));
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Enternumber1 = textField.getText()+btnNewButton_1.getText();
textField.setText(Enternumber1);
String Enternumber2 = passwordField.getText()+btnNewButton_1.getText();
passwordField.setText(Enternumber2);
}
Create a custom Action that extends from TextAction. The TextAction allows you to track the last text component that had focus. You can then apply your keypad logic to this component:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.*;
public class NumpadPanel extends JPanel
{
public NumpadPanel()
{
setLayout( new BorderLayout() );
JTextField textField1 = new JTextField(4);
JTextField textField2 = new JTextField(2);
JTextField textField3 = new JTextField(2);
JPanel panel = new JPanel();
panel.add( textField1 );
panel.add( textField2 );
panel.add( textField3 );
add(panel, BorderLayout.PAGE_START);
Action numberAction = new TextAction("")
{
#Override
public void actionPerformed(ActionEvent e)
{
JTextComponent textComponent = getFocusedComponent();
if (textComponent != null)
textComponent.replaceSelection(e.getActionCommand());
}
};
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout( new GridLayout(0, 5) );
add(buttonPanel, BorderLayout.CENTER);
for (int i = 0; i < 10; i++)
{
String text = String.valueOf(i);
JButton button = new JButton( text );
button.addActionListener( numberAction );
button.setMargin( new Insets(20, 20, 20, 20) );
button.setFocusable( false );
buttonPanel.add( button );
}
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("Numpad Panel");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( new NumpadPanel() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}

Java Swing Event Handling between classes

I have a main frame class, and 2 panel classes.
One of the panels has input fields
The second panel has buttons
I am trying to figure out how to set up event handling such that when a button in the second panel is clicked, I am able to perform an operation which requires accessing components (text field) from the first panel.
The problem is I am unable to access the components from a different class
Code:
public class MainFrame extends JFrame {
private InputPanel input_panel;
private ButtonPanel button_panel;
public MainFrame() {
setTitle("Shop");
setSize(650,350);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
input_panel = new InputPanel();
button_panel = new ButtonPanel();
add(input_panel, BorderLayout.CENTER);
add(button_panel, BorderLayout.SOUTH);
setVisible(true);
}
}
public class InputPanel extends JPanel {
private static JLabel item_num;
private static JLabel book_id;
private static JLabel quantity;
private static JLabel item_info;
private static JLabel subtotal;
private static JTextField t_item_num;
private static JTextField t_book_id;
private static JTextField t_quantity;
private static JTextField t_item_info;
private static JTextField t_subtotal;
public InputPanel() {
//Layout Manager
FlowLayout flow_input_layout = new FlowLayout(FlowLayout.TRAILING, 75, 10);
//Panel
setLayout(flow_input_layout);
setBackground(Color.yellow);
//Text Fields and Labels
item_num = new JLabel("Enter number of items in this order:");
add(item_num);
t_item_num = new JTextField(20);
add(t_item_num);
book_id = new JLabel("Enter Book ID for Item # 1:");
add(book_id);
t_book_id = new JTextField(20);
add(t_book_id);
quantity = new JLabel("Enter quantity for Item # 1:");
add(quantity);
t_quantity = new JTextField(20);
add(t_quantity);
item_info = new JLabel("Item # 1 Info:");
add(item_info);
t_item_info = new JTextField(20);
add(t_item_info);
subtotal = new JLabel("Order subtotal for X Items:");
add(subtotal);
t_subtotal = new JTextField(20);
add(t_subtotal);
}
}
public class ButtonPanel extends JPanel implements ActionListener {
private static JButton process_btn;
private static JButton confirm_btn;
private static JButton view_btn;
private static JButton finish_btn;
private static JButton new_btn;
private static JButton exit_btn;
public ButtonPanel() {
//Layout Manager
FlowLayout flow_input_layout = new FlowLayout();
//Button Panel
setLayout(flow_input_layout);
setBackground(Color.blue);
//Buttons
process_btn = new JButton("Process Item #1");
process_btn.addActionListener(this);
add(process_btn);
confirm_btn = new JButton("Confirm Item #1");
confirm_btn.addActionListener(this);
add(confirm_btn);
view_btn = new JButton("View Order");
view_btn.addActionListener(this);
add(view_btn);
finish_btn = new JButton("Finish Order");
finish_btn.addActionListener(this);
add(finish_btn);
new_btn = new JButton("New Order");
new_btn.addActionListener(this);
add(new_btn);
exit_btn = new JButton("Exit");
exit_btn.addActionListener(this);
add(exit_btn);
}
#Override
public void actionPerformed(ActionEvent event) {
;
}
}
Problem is I cannot access components from my other panel class. Not sure what to do
The easiest thing would be to add the InputPanel to the constructor of the ButtonPanel and then store it in a variable.
input_panel = new InputPanel();
button_panel = new ButtonPanel(input_panel);
And in the ButtonPanel:
public class ButtonPanel extends JPanel implements ActionListener {
...
private final InputPanel inputPanel;
public ButtonPanel(final InputPanel inputPanel) {
this.inputPanel = inputPanel;
...
}
#Override
public void actionPerformed(ActionEvent event) {
this.inputPanel.getItemNum().doSomething() // you will need to also create accessor methods in the inputPanel
}
And btw you should be using camelCase in Java.

StackOverflowError when adding JTextField to the constructor

So I do know what an StackOverflowError is, the problem however is I can't find it here. I am supposed to make a simple GUI with labels, text fields and buttons. One of these buttons is supposed to "clear" the text fields, so I add that by putting text field as an argument in the constructor, but when I actually add the text fields i just get an stack overflow error. Here is the code:
Orders class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Orders extends JFrame {
public Orders() {
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(4, 2, 2, 2));
JLabel name = new JLabel("Item name:");
JLabel number = new JLabel("Number of:");
JLabel cost = new JLabel("Cost:");
JLabel amount = new JLabel("Amount owed:");
JTextField nameJtf = new JTextField(10);
JTextField numberJtf = new JTextField(10);
JTextField costJtf = new JTextField(10);
JTextField amountJtf = new JTextField(10);
panel1.add(name);
panel1.add(nameJtf);
panel1.add(number);
panel1.add(numberJtf);
panel1.add(cost);
panel1.add(costJtf);
panel1.add(amount);
panel1.add(amountJtf);
JPanel panel2 = new JPanel();
JButton calculate = new JButton("Calculate");
JButton save = new JButton("Save");
JButton clear = new JButton("Clear");
JButton exit = new JButton("Exit");
panel2.add(calculate);
panel2.add(save);
panel2.add(clear);
panel2.add(exit);
OnClick action = new OnClick(exit, clear, save, calculate, nameJtf, numberJtf, costJtf, amountJtf);
exit.addActionListener(action);
this.setTitle("Otto's Items Orders Calculator");
this.add(panel1, BorderLayout.NORTH);
this.add(panel2, BorderLayout.SOUTH);
this.setSize(400, 200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Orders();
}
}
OnClick class :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class OnClick extends Orders implements ActionListener {
private JButton exitModify = null;
private JButton clearModify = null;
private JButton saveModify = null;
private JButton calculateModify = null;
private JTextField nameModify = null;
private JTextField numberModify = null;
private JTextField costModify = null;
private JTextField amountModify = null;
public OnClick (JButton _exitModify, JButton _clearModify, JButton _saveModify, JButton _calculateModify, JTextField _nameModify, JTextField _numberModify, JTextField _costModify, JTextField _amountModify) {
exitModify = _exitModify;
clearModify = _clearModify;
saveModify = _saveModify;
calculateModify = _calculateModify;
nameModify = _nameModify;
numberModify = _numberModify;
costModify = _numberModify;
amountModify = _amountModify;
}
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if (o == this.exitModify) {
System.exit(0);
} else if (o == this.clearModify) {
amountModify = null;
nameModify = null;
costModify = null;
numberModify = null;
}
}
}
As soon as I add nameJtf I get this error.

Java JTexfield Input and Output for Property Tax

I have been trying to run this code which takes the user's assessed home value, the county and school taxes and then outputs them to a text field. This code compiles and runs but there is a logical error somewhere that doesn't let it output any text. I'm just starting with Java so any help would be appreciated.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PropertyTax3 extends JFrame
{
// set parameters to define extent of the window
private static final int WIDTH = 500, HEIGHT = 300;
private ExitHandler ebHandler;
private CalculateHandler cbHandler;
private JTextField assessTF, schoolRateTF, countyRateTF, schoolTaxTF, countyTaxTF, totalTaxTF;
public PropertyTax3()
{
// set title, size and visibility aspects of window
setTitle("Calculation of Property Taxes");
//Label Definitions
JLabel assessL = new JLabel("Assesment Home Value:", SwingConstants.RIGHT);
JLabel schoolRateL = new JLabel("Decimal Value of School Tax Rate:", SwingConstants.RIGHT);
JLabel countyRateL = new JLabel("Decimal Value of County Tax Rate:", SwingConstants.RIGHT);
JLabel schoolTaxL = new JLabel("School Taxes:", SwingConstants.RIGHT);
JLabel countyTaxL = new JLabel("County Taxes:", SwingConstants.RIGHT);
JLabel totalTaxL = new JLabel("Total Taxes:", SwingConstants.RIGHT);
//Text Field Definitions
JTextField assessTF = new JTextField(10);
JTextField schoolRateTF = new JTextField(10);
JTextField countyRateTF = new JTextField(10);
JTextField schoolTaxTF = new JTextField(10);
JTextField countyTaxTF = new JTextField(10);
JTextField totalTaxTF = new JTextField(10);
//Exit Button
JButton exit = new JButton("Exit");
ebHandler = new ExitHandler();
exit.addActionListener(ebHandler);
//Calculate Button
JButton calculate = new JButton("Calculate");
cbHandler = new CalculateHandler();
calculate.addActionListener(cbHandler);
//Container format
Container pane = getContentPane();
pane.setLayout(new GridLayout(7,2));
pane.add(assessL);
pane.add(assessTF);
pane.add(schoolRateL);
pane.add(schoolRateTF);
pane.add(countyRateL);
pane.add(countyRateTF);
pane.add(schoolTaxL);
pane.add(schoolTaxTF);
pane.add(countyTaxL);
pane.add(countyTaxTF);
pane.add(totalTaxL);
pane.add(totalTaxTF);
pane.add(exit);
pane.add(calculate);
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class ExitHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
private class CalculateHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double countyRate, schoolRate, assessment, schoolTax, countyTax, totalTax;
assessment = Double.parseDouble(assessTF.getText());
schoolRate = Double.parseDouble(schoolRateTF.getText());
countyRate = Double.parseDouble(countyRateTF.getText());
schoolTax = assessment * schoolRate * .01;
countyTax = assessment * countyRate * .01;
totalTax = schoolTax + countyTax;
schoolTaxTF.setText(""+ String.format("%.2f", schoolTax));
countyTaxTF.setText(""+ String.format("%.2f", countyTax));
totalTaxTF.setText(""+ String.format("%.2f", totalTax));
}
}
public static void main(String[] args)
{
// main program to invoke constructor
PropertyTax3 proptax = new PropertyTax3();
}
}
Change this
JTextField assessTF = new JTextField(10);
JTextField schoolRateTF = new JTextField(10);
JTextField countyRateTF = new JTextField(10);
JTextField schoolTaxTF = new JTextField(10);
JTextField countyTaxTF = new JTextField(10);
JTextField totalTaxTF = new JTextField(10);
to this
assessTF = new JTextField(10);
schoolRateTF = new JTextField(10);
countyRateTF = new JTextField(10);
schoolTaxTF = new JTextField(10);
countyTaxTF = new JTextField(10);
totalTaxTF = new JTextField(10);
That way, you aren't using the variables you defined as a class field, but creating new ones. And that's why you couldn't access them later with getText and setText.
That's because there are no values for the program to run with...

One part of layout manager not working and ActionListener not working [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to add the buttons to the mainPanel after i click the button,
currently only appetizer is set,
The second thing is the GridLayout on my mainAppPanel is not working, the rows ,vgap and hgap is working but the columns is not.
Please tell me what can be done.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class sushi implements ActionListener
{
private JFrame frame;
private JPanel appPanel;
private JPanel sideAppPanel;
private JPanel mainAppPanel;
private JButton appItemButton1;
private JButton appItemButton2;
private JButton appItemButton4;
private JButton appItemButton5;
private JButton appItemButton6;
private JButton appItemButton7;
private JButton appItemButton8;
private JButton appItemButton9;
private JButton appButton;
private JButton mainButton;
private JButton sidesButton;
private JButton dessertButton;
private JButton drinksButton;
private JButton totalButton;
private JLabel blank1;
private JLabel blank2;
private JLabel blank3;
private JLabel blank4;
private JLabel blank5;
private JLabel blank6;
private JLabel blank7;
private JLabel blank8;
private JLabel blank9;
private JLabel blank10;
private JLabel blank11;
private JLabel blank12;
private JLabel blank13;
private JLabel blank14;
private JLabel blank15;
private JLabel blank16;
private JLabel blank17;
private JLabel blank18;
private JLabel blank19;
public static void main(String args[])
{
sushi lol=new sushi();
}
public sushi()
{
//frame settings
frame=new JFrame("sushi master");
frame.setSize(700,700);
frame.setResizable(false);
frame.setLocation(300,100);
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLayout(null);
//sidepanel
sideAppPanel=new JPanel();
sideAppPanel.setSize(100,700);
sideAppPanel.setBackground(Color.red);
sideAppPanel.setLocation(0,0);
sideAppPanel.setLayout(new GridLayout(18,0));
frame.add(sideAppPanel);
//items in side panel
appButton=new JButton("Appetizer");
mainButton=new JButton("Main course");
sidesButton=new JButton("Sides");
drinksButton=new JButton("Drinks");
totalButton=new JButton("Total");
blank1=new JLabel(" ");
blank2=new JLabel(" ");
blank3=new JLabel(" ");
blank4=new JLabel(" ");
blank5=new JLabel("");
sideAppPanel.add(blank1);
sideAppPanel.add(appButton);
sideAppPanel.add(blank2);
sideAppPanel.add(mainButton);
sideAppPanel.add(blank3);
sideAppPanel.add(sidesButton);
sideAppPanel.add(blank4);
sideAppPanel.add(drinksButton);
sideAppPanel.add(blank5);
sideAppPanel.add(totalButton);
//items in main panel
blank5=new JLabel(" ");
blank6=new JLabel(" ");
blank7=new JLabel(" ");
blank8=new JLabel(" ");
blank9=new JLabel(" ");
blank10=new JLabel(" ");
blank11=new JLabel(" ");
blank12=new JLabel(" ");
blank13=new JLabel(" ");
blank14=new JLabel(" ");
blank15=new JLabel(" ");
blank16=new JLabel(" ");
appItemButton1=new JButton("1");
appItemButton2=new JButton("2");
appItemButton3=new JButton("3");
appItemButton4=new JButton("4");
appItemButton5=new JButton("5");
appItemButton6=new JButton("6");
appItemButton7=new JButton("7");
appItemButton8=new JButton("8");
appItemButton9=new JButton("9");
//adding action listening to side panel buttons
appButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
if(event.getSource()==appButton)
{
mainAppPanel=new JPanel();
mainAppPanel.setSize(600,700);
mainAppPanel.setLocation(100,0);
mainAppPanel.setBackground(Color.blue);
mainAppPanel.setLayout(new GridLayout(5,4,20,20));
frame.add(mainAppPanel);
appItemButton1=new JButton("1");
appItemButton2=new JButton("2");
appItemButton3=new JButton("3");
appItemButton4=new JButton("4");
appItemButton5=new JButton("5");
appItemButton6=new JButton("6");
appItemButton7=new JButton("7");
appItemButton8=new JButton("8");
appItemButton9=new JButton("9");
mainAppPanel.add(appItemButton1);
mainAppPanel.add(appItemButton2);
mainAppPanel.add(appItemButton3);
mainAppPanel.add(appItemButton4);
mainAppPanel.add(appItemButton5);
mainAppPanel.add(appItemButton6);
mainAppPanel.add(appItemButton7);
mainAppPanel.add(appItemButton8);
mainAppPanel.add(appItemButton9);
mainAppPanel.add(blank5);
mainAppPanel.add(blank6);
mainAppPanel.add(blank7);
}
}
}
Some Points i figured out:
You have not defined
private JButton appItemButton3;
Your actionlistener works, but you have to validate the changes to the frame.
frame.validate();
Do you use eclipse or netbeans ide? It helps to format the code and shows some errors or naming conventions like: Classnames uppercase
class Sushi implements ActionListener
Do not use a null-Layout. You can use Borderlayout to handle all elements.
This should work but i think you should look for some tutorials to learn more about swing:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Sushi implements ActionListener {
private JFrame frame;
private JPanel appPanel;
private JPanel sideAppPanel;
private JPanel mainAppPanel;
private JButton appItemButton1;
private JButton appItemButton2;
private JButton appItemButton3; //added
private JButton appItemButton4;
private JButton appItemButton5;
private JButton appItemButton6;
private JButton appItemButton7;
private JButton appItemButton8;
private JButton appItemButton9;
private JButton appButton;
private JButton mainButton;
private JButton sidesButton;
private JButton dessertButton;
private JButton drinksButton;
private JButton totalButton;
private JLabel blank1;
private JLabel blank2;
private JLabel blank3;
private JLabel blank4;
private JLabel blank5;
private JLabel blank6;
private JLabel blank7;
private JLabel blank8;
private JLabel blank9;
private JLabel blank10;
private JLabel blank11;
private JLabel blank12;
private JLabel blank13;
private JLabel blank14;
private JLabel blank15;
private JLabel blank16;
private JLabel blank17;
private JLabel blank18;
private JLabel blank19;
public static void main(String args[]) {
Sushi lol = new Sushi();
}
public Sushi() {
// frame settings
frame = new JFrame("sushi master");
frame.setSize(700, 700);
frame.setResizable(false);
frame.setLocation(300, 100);
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout()); //changed
// sidepanel
sideAppPanel = new JPanel();
sideAppPanel.setSize(100, 700);
sideAppPanel.setBackground(Color.red);
sideAppPanel.setLocation(0, 0);
sideAppPanel.setLayout(new GridLayout(18, 0));
frame.add(sideAppPanel, BorderLayout.WEST); //changed
// items in side panel
appButton = new JButton("Appetizer");
mainButton = new JButton("Main course");
sidesButton = new JButton("Sides");
drinksButton = new JButton("Drinks");
totalButton = new JButton("Total");
blank1 = new JLabel(" ");
blank2 = new JLabel(" ");
blank3 = new JLabel(" ");
blank4 = new JLabel(" ");
blank5 = new JLabel("");
sideAppPanel.add(blank1);
sideAppPanel.add(appButton);
sideAppPanel.add(blank2);
sideAppPanel.add(mainButton);
sideAppPanel.add(blank3);
sideAppPanel.add(sidesButton);
sideAppPanel.add(blank4);
sideAppPanel.add(drinksButton);
sideAppPanel.add(blank5);
sideAppPanel.add(totalButton);
// items in main panel
blank5 = new JLabel(" ");
blank6 = new JLabel(" ");
blank7 = new JLabel(" ");
blank8 = new JLabel(" ");
blank9 = new JLabel(" ");
blank10 = new JLabel(" ");
blank11 = new JLabel(" ");
blank12 = new JLabel(" ");
blank13 = new JLabel(" ");
blank14 = new JLabel(" ");
blank15 = new JLabel(" ");
blank16 = new JLabel(" ");
appItemButton1 = new JButton("1");
appItemButton2 = new JButton("2");
appItemButton3 = new JButton("3");
appItemButton4 = new JButton("4");
appItemButton5 = new JButton("5");
appItemButton6 = new JButton("6");
appItemButton7 = new JButton("7");
appItemButton8 = new JButton("8");
appItemButton9 = new JButton("9");
// adding action listening to side panel buttons
appButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == appButton) {
mainAppPanel = new JPanel();
mainAppPanel.setSize(600, 700);
mainAppPanel.setLocation(100, 0);
mainAppPanel.setBackground(Color.blue);
mainAppPanel.setLayout(new GridLayout(5, 4, 20, 20));
frame.add(mainAppPanel, BorderLayout.CENTER); //changed
appItemButton1 = new JButton("1");
appItemButton2 = new JButton("2");
appItemButton3 = new JButton("3");
appItemButton4 = new JButton("4");
appItemButton5 = new JButton("5");
appItemButton6 = new JButton("6");
appItemButton7 = new JButton("7");
appItemButton8 = new JButton("8");
appItemButton9 = new JButton("9");
mainAppPanel.add(appItemButton1);
mainAppPanel.add(appItemButton2);
mainAppPanel.add(appItemButton3);
mainAppPanel.add(appItemButton4);
mainAppPanel.add(appItemButton5);
mainAppPanel.add(appItemButton6);
mainAppPanel.add(appItemButton7);
mainAppPanel.add(appItemButton8);
mainAppPanel.add(appItemButton9);
mainAppPanel.add(blank5);
mainAppPanel.add(blank6);
mainAppPanel.add(blank7);
frame.validate(); //added
}
}
}

Categories

Resources