I'm learning Java and I am trying to get this class to work, this is only my second time using ActionListener. In this application my buttons do not work when I click them. A system exit should occur, but it doesn't...and I just can't figure out whats wrong. I'd greatly appreciate any help or insight. This code compiles correctly, but you can get the complete file here (cheesy name,eh).
==========================================================
import javax.swing.*;
import java.awt.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
public class LunchOrder extends JFrame
{
public LunchOrder()
{
setTitle("Lunch Order"
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent eex)
{
System.exit(0);
}
});
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
int width = 400;
int height = 300;
setBounds((d.width - width)/2, (d.height - height)/2, width, height);
Container contentPane = getContentPane();
LunchPanel lnPanel = new LunchPanel();
contentPane.add(lnPanel);
setResizable(false);
}
public static void main(String[] args)
{
JFrame frame = new LunchOrder();
frame.show();
}
}
class LunchPanel extends JPanel implements ActionListener
{
public JButton orderButton, exitButton;
private JRadioButton hamburgerButton, pizzaButton, saladButton;
private JCheckBox lettuceCheck, mayoCheck, mustardCheck;
public LunchPanel()
{
//panels
JPanel coursePanel = new JPanel();
JPanel addOnsPanel = new JPanel();
JPanel totalPanel = new JPanel();
//borders
Border etchedBorder = BorderFactory.createEtchedBorder();
Border courseBorder = BorderFactory.createTitledBorder(etchedBorder,"Main Course"
Border addOnsBorder = BorderFactory.createTitledBorder(etchedBorder,"Add-Ons($.25/each)"
Border totalsBorder = BorderFactory.createTitledBorder(etchedBorder,"Order Total"
coursePanel.setBorder(courseBorder);
addOnsPanel.setBorder(addOnsBorder);
totalPanel.setBorder(totalsBorder);
//radiobuttons
coursePanel.setLayout(new BorderLayout());
hamburgerButton = new JRadioButton("Hamburger - $6.95 ", true);
pizzaButton = new JRadioButton("Pizza - $5.95"
saladButton = new JRadioButton("Salad - $4.95"
pizzaButton.addActionListener(this);
saladButton.addActionListener(this);
hamburgerButton.addActionListener(this);
ButtonGroup radioGroup= new ButtonGroup();
radioGroup.add(hamburgerButton);
radioGroup.add(pizzaButton);
radioGroup.add(saladButton);
coursePanel.add(hamburgerButton, BorderLayout.NORTH);
coursePanel.add(pizzaButton, BorderLayout.CENTER);
coursePanel.add(saladButton, BorderLayout.SOUTH);
//buttons
JButton orderButton = new JButton("Place Order"
JButton exitButton = new JButton("Exit"
orderButton.addActionListener(this);
exitButton.addActionListener(this);
//checks
addOnsPanel.setLayout(new BorderLayout());
lettuceCheck = new JCheckBox("Lettuce, tomato, and onions"
mayoCheck = new JCheckBox("Mayonnaise"
mustardCheck = new JCheckBox("Mustard"
addOnsPanel.add(lettuceCheck, BorderLayout.NORTH);
addOnsPanel.add(mayoCheck, BorderLayout.CENTER);
addOnsPanel.add(mustardCheck, BorderLayout.SOUTH);
//textfields
totalPanel.setLayout(new BorderLayout());
JLabel subtLabel = new JLabel("SubTotal:"
JLabel taxLabel = new JLabel("Tax(7.85%)"
JLabel totalLabel = new JLabel("Total due:"
JTextField subtText = new JTextField(7);
JTextField taxText = new JTextField(7);
JTextField totalText = new JTextField(7);
JPanel subtPanel = new JPanel();
JPanel taxPanel = new JPanel();
JPanel totPanel = new JPanel();
subtPanel.add(subtLabel, BorderLayout.WEST);
taxPanel.add(taxLabel, BorderLayout.WEST);
totPanel.add(totalLabel, BorderLayout.WEST);
subtPanel.add(subtText, BorderLayout.EAST);
taxPanel.add(taxText, BorderLayout.EAST);
totPanel.add(totalText, BorderLayout.EAST);
totalPanel.add(subtPanel, BorderLayout.NORTH);
totalPanel.add(taxPanel, BorderLayout.CENTER);
totalPanel.add(totPanel, BorderLayout.SOUTH);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = 100;
c.weighty = 100;
c.ipadx = 5;
//c.anchor = GridBagConstraints.WEST;
c = getConstraints(c, 1, 1, 1, 2);
add(coursePanel, c);
//c.anchor = GridBagConstraints.EAST;
c = getConstraints(c, 3, 1, 2, 2);
add(addOnsPanel, c);
//c.anchor = GridBagConstraints.WEST;
c = getConstraints(c, 1, 3, 2, 2);
add(totalPanel, c);
//c.anchor = GridBagConstraints.EAST;
c = getConstraints(c, 3, 3, 1, 1);
add(orderButton, c);
c = getConstraints(c, 3, 4, 1, 1);
add(exitButton, c);
}
private GridBagConstraints getConstraints(GridBagConstraints c, int x, int y, int width, int height)
{
c.gridx = x;
c.gridy = y;
c.gridwidth = width;
c.gridheight = height;
return c;
}
public void actionPerformed(ActionEvent e)
{
//try
//{
Object source = e.getSource();
if (source == exitButton)
System.exit(0);
if (source == orderButton)
{
System.exit(0);
}
// }
// catch(NullPointerException exc)
// {
// JOptionPane.showMessageDialog(this,"Invalid data entered. \nPlease check all numbers and try again"
// }
//
// catch (NumberFormatException nfe)
// {
// JOptionPane.showMessageDialog(this,"Invalid data entered. \nPlease check all numbers and try again"
// }
}
}
==========================================================
import javax.swing.*;
import java.awt.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
public class LunchOrder extends JFrame
{
public LunchOrder()
{
setTitle("Lunch Order"
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent eex)
{
System.exit(0);
}
});
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
int width = 400;
int height = 300;
setBounds((d.width - width)/2, (d.height - height)/2, width, height);
Container contentPane = getContentPane();
LunchPanel lnPanel = new LunchPanel();
contentPane.add(lnPanel);
setResizable(false);
}
public static void main(String[] args)
{
JFrame frame = new LunchOrder();
frame.show();
}
}
class LunchPanel extends JPanel implements ActionListener
{
public JButton orderButton, exitButton;
private JRadioButton hamburgerButton, pizzaButton, saladButton;
private JCheckBox lettuceCheck, mayoCheck, mustardCheck;
public LunchPanel()
{
//panels
JPanel coursePanel = new JPanel();
JPanel addOnsPanel = new JPanel();
JPanel totalPanel = new JPanel();
//borders
Border etchedBorder = BorderFactory.createEtchedBorder();
Border courseBorder = BorderFactory.createTitledBorder(etchedBorder,"Main Course"
Border addOnsBorder = BorderFactory.createTitledBorder(etchedBorder,"Add-Ons($.25/each)"
Border totalsBorder = BorderFactory.createTitledBorder(etchedBorder,"Order Total"
coursePanel.setBorder(courseBorder);
addOnsPanel.setBorder(addOnsBorder);
totalPanel.setBorder(totalsBorder);
//radiobuttons
coursePanel.setLayout(new BorderLayout());
hamburgerButton = new JRadioButton("Hamburger - $6.95 ", true);
pizzaButton = new JRadioButton("Pizza - $5.95"
saladButton = new JRadioButton("Salad - $4.95"
pizzaButton.addActionListener(this);
saladButton.addActionListener(this);
hamburgerButton.addActionListener(this);
ButtonGroup radioGroup= new ButtonGroup();
radioGroup.add(hamburgerButton);
radioGroup.add(pizzaButton);
radioGroup.add(saladButton);
coursePanel.add(hamburgerButton, BorderLayout.NORTH);
coursePanel.add(pizzaButton, BorderLayout.CENTER);
coursePanel.add(saladButton, BorderLayout.SOUTH);
//buttons
JButton orderButton = new JButton("Place Order"
JButton exitButton = new JButton("Exit"
orderButton.addActionListener(this);
exitButton.addActionListener(this);
//checks
addOnsPanel.setLayout(new BorderLayout());
lettuceCheck = new JCheckBox("Lettuce, tomato, and onions"
mayoCheck = new JCheckBox("Mayonnaise"
mustardCheck = new JCheckBox("Mustard"
addOnsPanel.add(lettuceCheck, BorderLayout.NORTH);
addOnsPanel.add(mayoCheck, BorderLayout.CENTER);
addOnsPanel.add(mustardCheck, BorderLayout.SOUTH);
//textfields
totalPanel.setLayout(new BorderLayout());
JLabel subtLabel = new JLabel("SubTotal:"
JLabel taxLabel = new JLabel("Tax(7.85%)"
JLabel totalLabel = new JLabel("Total due:"
JTextField subtText = new JTextField(7);
JTextField taxText = new JTextField(7);
JTextField totalText = new JTextField(7);
JPanel subtPanel = new JPanel();
JPanel taxPanel = new JPanel();
JPanel totPanel = new JPanel();
subtPanel.add(subtLabel, BorderLayout.WEST);
taxPanel.add(taxLabel, BorderLayout.WEST);
totPanel.add(totalLabel, BorderLayout.WEST);
subtPanel.add(subtText, BorderLayout.EAST);
taxPanel.add(taxText, BorderLayout.EAST);
totPanel.add(totalText, BorderLayout.EAST);
totalPanel.add(subtPanel, BorderLayout.NORTH);
totalPanel.add(taxPanel, BorderLayout.CENTER);
totalPanel.add(totPanel, BorderLayout.SOUTH);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = 100;
c.weighty = 100;
c.ipadx = 5;
//c.anchor = GridBagConstraints.WEST;
c = getConstraints(c, 1, 1, 1, 2);
add(coursePanel, c);
//c.anchor = GridBagConstraints.EAST;
c = getConstraints(c, 3, 1, 2, 2);
add(addOnsPanel, c);
//c.anchor = GridBagConstraints.WEST;
c = getConstraints(c, 1, 3, 2, 2);
add(totalPanel, c);
//c.anchor = GridBagConstraints.EAST;
c = getConstraints(c, 3, 3, 1, 1);
add(orderButton, c);
c = getConstraints(c, 3, 4, 1, 1);
add(exitButton, c);
}
private GridBagConstraints getConstraints(GridBagConstraints c, int x, int y, int width, int height)
{
c.gridx = x;
c.gridy = y;
c.gridwidth = width;
c.gridheight = height;
return c;
}
public void actionPerformed(ActionEvent e)
{
//try
//{
Object source = e.getSource();
if (source == exitButton)
System.exit(0);
if (source == orderButton)
{
System.exit(0);
}
// }
// catch(NullPointerException exc)
// {
// JOptionPane.showMessageDialog(this,"Invalid data entered. \nPlease check all numbers and try again"
// }
//
// catch (NumberFormatException nfe)
// {
// JOptionPane.showMessageDialog(this,"Invalid data entered. \nPlease check all numbers and try again"
// }
}
}