(Java)JPanel未打印出来
我在大学课程中做一个JPanel作业,最后的打印结果有问题。面板本身看起来很棒,所有的事情,我唯一不能让它做的就是显示应该打印出来的行 它被设计为接收杂货项目的输入,名称、价格、类别和数量都由用户输入。单击“添加到购物车”按钮时,应显示以前键入的所有信息。但到目前为止,情况并非如此 以下是我使用的两个类: MyCart.java(Java)JPanel未打印出来,java,swing,jpanel,Java,Swing,Jpanel,我在大学课程中做一个JPanel作业,最后的打印结果有问题。面板本身看起来很棒,所有的事情,我唯一不能让它做的就是显示应该打印出来的行 它被设计为接收杂货项目的输入,名称、价格、类别和数量都由用户输入。单击“添加到购物车”按钮时,应显示以前键入的所有信息。但到目前为止,情况并非如此 以下是我使用的两个类: MyCart.java package assignment2; import javax.swing.*; import java.awt.*; import java.awt.event.
package assignment2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class MyCart extends JFrame {
private JPanel display;
private JLabel itemLabel;
private JTextField item;
private JLabel catLabel;
private JComboBox category;
private JLabel quanLabel;
private JTextField quantity;
private JLabel priceLabel;
private JTextField price;
private JPanel btns;
private JButton addBtn;
private JButton exitBtn;
private JPanel list;
private JList myList;
private ArrayList<Item> itemList;
public MyCart() {
itemList = new ArrayList();
myList = new JList(itemList.toArray());
this.setLayout(new GridLayout(2,1,5,5));
display = new JPanel();
display.setLayout(new GridLayout(5,2,5,5));
itemLabel = new JLabel(" Item: ");
item = new JTextField(10);
catLabel = new JLabel(" Category: ");
category = new JComboBox(new String[]{"Meat", "Fruit/Vegetable", "Dairy", "Grains", "Sweets", "Other"});
quanLabel = new JLabel(" Quantity: ");
quantity = new JTextField(10);
priceLabel = new JLabel(" Price: ");
price = new JTextField(10);
display.add(itemLabel);
display.add(item);
display.add(catLabel);
display.add(category);
display.add(quanLabel);
display.add(quantity);
display.add(priceLabel);
display.add(price);
this.add(display);
btns = new JPanel();
btns.setLayout(new GridLayout(1,3,5,5));
addBtn = new JButton("Add to Cart");
exitBtn = new JButton("Exit");
btns.add(addBtn);
btns.add(exitBtn);
this.add(btns);
list = new JPanel();
list.setLayout(new FlowLayout());
list.add(new JLabel("My Cart:"));
JScrollPane myScrollPane = new JScrollPane(myList);
list.add(myScrollPane);
setLayout(new BorderLayout());
this.add(display,BorderLayout.NORTH);
this.add(btns,BorderLayout.CENTER);
this.add(list,BorderLayout.SOUTH);
//event listener object created
ButtonListeners buttonListener = new ButtonListeners();
addBtn.addActionListener(buttonListener);
exitBtn.addActionListener(buttonListener);
}
public static void main(String[] args) {
MyCart cart = new MyCart();
cart.setTitle("Cart <Adam>");
cart.setSize(400,350);
cart.setLocationRelativeTo(null);
cart.setVisible(true);
cart.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//event listener class for adding
class ButtonListeners implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
switch(e.getActionCommand()) {
case "Add": String n = item.getText();
String c = category.getSelectedItem().toString();
double q = Double.valueOf(quantity.getText());
double p = Double.valueOf(price.getText());
Item myItem = new Item();
myItem.setName(n);
myItem.setCategory(c);
myItem.setQuantity(q);
myItem.setPrice(p);
itemList.add(myItem);
myList.setListData(itemList.toArray());
break;
case "Exit": System.exit(0);
}
}
}
//exit listener
class ExitListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
}
您忘记为检查
ActionListener中的case语句的JButton
添加ActionCommand
每次调用此开关(例如getActionCommand())
时,侦听器都会抓取已添加按钮的ActionCommand
,如果未添加,则按钮的名称为ActionCommand
解决方案:
addBtn = new JButton("Add to Cart");
addBtn.setActionCommand("Add"); //will call the Add case actionListener
您忘记为检查ActionListener中的case语句的JButton
添加ActionCommand
每次调用此开关(例如getActionCommand())
时,侦听器都会抓取已添加按钮的ActionCommand
,如果未添加,则按钮的名称为ActionCommand
解决方案:
addBtn = new JButton("Add to Cart");
addBtn.setActionCommand("Add"); //will call the Add case actionListener
Rod为您提供了实际问题的解决方案,但是您没有正确使用JList
和ListModel
对模型中数据的更改应通过ListModel
完成。因此,您的ArrayList不需要跟踪项目。ListModel
将为您执行此操作
因此,您的基本代码应该是:
DefaultListModel<Item> model = new DefaultListModel<Item>();
JList list = new JList( model );
Rod为您提供了实际问题的解决方案,但是您没有正确使用JList
和ListModel
对模型中数据的更改应通过ListModel
完成。因此,您的ArrayList不需要跟踪项目。ListModel
将为您执行此操作
因此,您的基本代码应该是:
DefaultListModel<Item> model = new DefaultListModel<Item>();
JList list = new JList( model );
我们还没有看到这是什么,所以我在这段代码中使用的格式是我们到目前为止学到的PWe还没有被显示出来,所以我在这段代码中使用的格式是我们到目前为止学到的P非常感谢,我的朋友!:)非常感谢,我的朋友!:)