简体   繁体   中英

Having trouble understanding JList

I've built a program (which runs fine), but I really wanted to use JList instead of radio buttons. The problem is, I'm having a horrible time trying to do this and I end up with a mess of errors and a dysfunctional program. If anyone could provide any examples of how lists are properly used in Java, it would be greatly appreciated! I've not posted my program, as I'm not looking for answers, just general advice on JList. Thanks for those who have suggested the tutorial link - it helped out! :)

If anyone could provide any examples of how lists are properly used in Java

Read the JList API and follow the link titled How to Use Lists to the Swing tutorial which contains working examples.

Other comments:

  1. Don't use setBounds() to size/position components. Swing was designed to be used with layout managers for too many reasons to list here. The Swing tutorial also has a section on layout managers.

  2. Don't use a KeyListner. That is an old approach when using AWT. Swing has better API's. In this case you would add a DocumentListener to the Document of the text field. Again the tutorial has a section on how to write a DocumentListener.

Keep the tutorial link handy, it will help solve many problems.

I have done something with your code have look

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));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM