繁体   English   中英

无法理解JList

[英]Having trouble understanding JList

我已经构建了一个程序(运行正常),但我真的想使用JList而不是单选按钮。 问题是,我正在努力做到这一点,我最终得到了一堆错误和一个功能失调的程序。 如果有人能提供如何在Java中正确使用列表的任何示例,我们将不胜感激! 我没有发布我的程序,因为我不是在寻找答案,只是对JList的一般建议。 感谢那些建议教程链接的人 - 它帮助了! :)

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

阅读JList API并按照标题为How to Use Lists to the Swing的链接教程,其中包含工作示例。

其他的建议:

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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