Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have done something with your code have look</p> <pre><code>import java.awt.*; import java.awt.event.*; import java.text.*; import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class PizzaOrder extends JFrame implements ActionListener, KeyListener, ListSelectionListener { double smallPizzaPrice = 7.00, mediumPizzaPrice = 9.00, largePizzaPrice = 11.00; double sausage = 1.00, ham = 1.00, pineapple = 1.00, mushroom = 1.00, pepperoni = 1.00; JLabel lab1, lab2, lab3, toppers, lab4, lab5; Button button; JTextField text1, text2; ButtonGroup group; JRadioButton small, medium, large; JCheckBox chk1, chk2, chk3, chk4, chk5; JScrollPane jScrollPane1 = new javax.swing.JScrollPane(); JList jList1 = new javax.swing.JList(); DefaultListModel modellist=new DefaultListModel(); PizzaOrder() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(null); lab1 = new JLabel("Name: "); lab2 = new JLabel("Quantity: "); lab3 = new JLabel("How hungry are you?"); lab3.setForeground(Color.BLUE); lab3.setFont(new Font("Arial", Font.BOLD, 14)); toppers = new JLabel("Add some toppings : "); toppers.setForeground(new Color(0, 0, 205)); toppers.setFont(new Font("Arial", Font.ITALIC, 14)); lab4 = new JLabel("Total: "); lab4.setForeground(Color.RED); lab4.setFont(new Font("Arial", Font.BOLD, 14)); lab5 = new JLabel("$0.00"); lab5.setForeground(Color.RED); text1 = new JTextField(20); text2 = new JTextField(20); text2.setText(""); small = new JRadioButton("Small", true); medium = new JRadioButton("Medium", false); large = new JRadioButton("Large", false); group = new ButtonGroup(); group.add(small); group.add(medium); group.add(large); chk1 = new JCheckBox("Mushroom", false); chk2 = new JCheckBox("Ham", false); chk3 = new JCheckBox("Pepperoni", false); chk4 = new JCheckBox("Sausage", false); chk5 = new JCheckBox("Pineapple", false); modellist.addElement("Small"); modellist.addElement("Medium"); modellist.addElement("Large"); jList1.setModel(modellist); button = new Button("Order Now"); small.addActionListener(this); medium.addActionListener(this); large.addActionListener(this); chk1.addActionListener(this); chk2.addActionListener(this); chk3.addActionListener(this); chk4.addActionListener(this); chk5.addActionListener(this); text2.addKeyListener(this); button.addActionListener(this); jList1.addListSelectionListener(this); lab1.setBounds(50, 50, 200, 20); lab2.setBounds(50, 90, 200, 20); text1.setBounds(200, 50, 200, 20); text2.setBounds(200, 90, 200, 20); lab3.setBounds(50, 170, 500, 20); small.setBounds(300, 170, 100, 20); medium.setBounds(400, 170, 100, 20); large.setBounds(500, 170, 100, 20); toppers.setBounds(50, 200, 300, 20); chk1.setBounds(50, 230, 200, 20); chk2.setBounds(50, 260, 200, 20); chk3.setBounds(50, 290, 200, 20); chk4.setBounds(50, 320, 200, 20); chk5.setBounds(50, 350, 200, 20); lab4.setBounds(50, 550, 400, 40); lab5.setBounds(200, 550, 500, 40); jScrollPane1.setBounds(300, 270, 200, 300); jScrollPane1.setViewportView(jList1); button.setBounds(50, 600, 100, 20); add(lab1); add(lab2); add(text1); add(text2); add(lab3); add(small); add(medium); add(large); add(toppers); add(chk1); add(chk2); add(chk3); add(chk4); add(chk5); add(lab4); add(lab5); add(button); add(jScrollPane1); text2.selectAll(); setVisible(true); setSize(800, 700); } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { try { Integer.parseInt(text2.getText()); } catch (NumberFormatException fe) { text2.setText(""); } refreshPrice(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { JOptionPane.showMessageDialog(this, "Hi " + text1.getText() + ", thanks for ordering with us!" + "\n\nYour pizza's in the oven. ", "Orders Confirmed", JOptionPane.INFORMATION_MESSAGE); } refreshPrice(); } private void refreshPrice() { double price = 0; int numOfPizzas = Integer.parseInt(text2.getText()); NumberFormat numberForm = NumberFormat.getNumberInstance(); DecimalFormat moneyForm = (DecimalFormat) numberForm; moneyForm.applyPattern("0.00"); if (small.isSelected()) { price += smallPizzaPrice * numOfPizzas; } if (medium.isSelected()) { price += mediumPizzaPrice * numOfPizzas; } if (large.isSelected()) { price += largePizzaPrice * numOfPizzas; } if (chk1.isSelected()) { price += mushroom * numOfPizzas; } if (chk2.isSelected()) { price += sausage * numOfPizzas; } if (chk3.isSelected()) { price += pineapple * numOfPizzas; } if (chk4.isSelected()) { price += pepperoni * numOfPizzas; } if (chk5.isSelected()) { price += ham * numOfPizzas; } lab5.setText("$" + moneyForm.format(price)); } public static void main(String[] args) { @SuppressWarnings("unused") PizzaOrder order = new PizzaOrder(); } @Override public void valueChanged(ListSelectionEvent e) { double price = 0; int numOfPizzas = Integer.parseInt(text2.getText()); NumberFormat numberForm = NumberFormat.getNumberInstance(); DecimalFormat moneyForm = (DecimalFormat) numberForm; moneyForm.applyPattern("0.00"); System.out.println(jList1.getSelectedIndex()); if(jList1.getSelectedIndex()==0) { price += smallPizzaPrice * numOfPizzas; } if(jList1.getSelectedIndex()==1) { price += mediumPizzaPrice * numOfPizzas; } if(jList1.getSelectedIndex()==2) { price += largePizzaPrice * numOfPizzas; } lab5.setText("$" + moneyForm.format(price)); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload