我编写了我的第一个 Java 应用程序:一个杂货清单。我的第一个版本运行良好。如果您填写两个文本字段并从下拉列表中选择一个项目并按“添加”按钮。带有金额和单位的项目显示在第二个 jPanel 的文本区域中。但现在我不想将其显示在文本区域中,而是希望将项目显示在 jLabels 中。因此,每次单击“添加”按钮时,第二个 jPanel 中都会出现一个新标签。但我似乎无法让它发挥作用。我在 stackoverflow 和 google 上的不同网站上阅读了很多不同的方法,我也阅读了 oracle 网站。但我一定做错了什么。你能看一下我的代码并告诉我我做错了什么吗?
代码
GroceryList2ActionListener.java
package javaclasses;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import grocerylist2jframe.GroceryList2JFrame;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.JLabel;
public class GroceryList2ActionListener {
public static class GroceryListActionListenerHandler extends GroceryList2JFrame {
protected static String groceryItem, unit, quantity;
protected static JButton buttonAddGroceryItemToGrocerylist = buttonAddGroceryItem, buttonRemoveGroceryItemFromGrocerylist = buttonRemoveGroceryItem;
protected static JLabel label;
public static void getButtonActionAddGroceryItem() {
buttonAddGroceryItemToGrocerylist.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
groceryItem = jTextField1GroceryItem.getText(); …Run Code Online (Sandbox Code Playgroud)