0

我已经构建了一个程序(运行良好),但我真的想使用 JList 而不是单选按钮。问题是,我在尝试执行此操作时遇到了可怕的事情,最终我遇到了一堆错误和功能失调的程序。如果有人可以提供任何关于如何在 Java 中正确使用列表的示例,将不胜感激!我没有发布我的程序,因为我不是在寻找答案,只是关于 JList 的一般建议。感谢那些建议教程链接的人 - 它帮助了!:)

4

2 回答 2

2

如果有人可以提供任何关于如何在 Java 中正确使用列表的示例

阅读 JList API 并点击标题How to Use Lists为包含工作示例的 Swing 教程的链接。

其他的建议:

  1. 不要使用 setBounds() 来调整/定位组件的大小。Swing 被设计为与布局管理器一起使用,原因有很多,无法在此列出。Swing 教程也有关于布局管理器的部分。

  2. 不要使用 KeyListner。这是使用 AWT 时的一种旧方法。Swing 有更好的 API。在这种情况下,您需要将 DocumentListener 添加到文本字段的 Document 中。本教程再次有一节介绍如何编写 DocumentListener。

保持教程链接方便,它将帮助解决许多问题。

于 2013-10-12T15:50:45.083 回答
0

我已经用你的代码做了一些事情,看看

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));
    }
}
于 2013-10-12T17:44:21.617 回答