gpt4 book ai didi

java - 如何在 actionPerfomed() 中调用 actionPerformed()

转载 作者:行者123 更新时间:2023-11-30 05:50:52 25 4
gpt4 key购买 nike

我的以下代码有问题。

添加信息菜单中有三个菜单项(客户、商品和员工)。单击它们(使用 addActionListener)应该会显示各种文本字段/单选按钮/组合框(需要填写信息)和一个提交按钮。

提交所需信息并单击提交按钮后,它应该将信息打印到弹出窗口。

我卡在最后一点,它应该再次调用 actionPerformed 方法并将值打印到弹出窗口。谁能帮忙?

EDITED##我已经编辑了我的代码。我的问题从第 1 行开始。 216 到行号。 225. 当我点击 Customer 菜单项的“submit3”按钮时,弹出窗口出现但不显示包含“txt1”内容的字符串的内容。如何将我的组件的值传递给 actionPerformed,以便它可以在新的弹出窗口中打印它们?

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class Retail extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
JMenuBar menuBar = new JMenuBar();
JMenu addmenu = new JMenu("Add Information");
JMenu delmenu = new JMenu("Delete Information");
JMenu savemenu = new JMenu("Save Information");
JMenuItem emp = new JMenuItem("Employee");
JMenuItem merc = new JMenuItem("Merchandise");
JMenuItem cust = new JMenuItem("Customer");
Container contentPane = getContentPane();
JPanel p2 = new JPanel();

public Retail()
{
super();
contentPane.setLayout(new BorderLayout());
JPanel p1 = new JPanel();
p1.setBorder(new TitledBorder("Select Menu"));
p1.setPreferredSize(new Dimension(500, 100));
contentPane.add(p1,BorderLayout.NORTH);

p2.setBorder(new TitledBorder("Entry Screen"));
p2.setPreferredSize(new Dimension(500,500));
contentPane.add(p2,BorderLayout.CENTER);
p2.setLayout(new BorderLayout());

p1.add(menuBar);
menuBar.add(addmenu);
menuBar.add(delmenu);
menuBar.add(savemenu);
addmenu.add(emp);
addmenu.addSeparator();
addmenu.add(merc);
addmenu.addSeparator();
addmenu.add(cust);
addmenu.addSeparator();


emp.addActionListener(this);
merc.addActionListener(this);
cust.addActionListener(this);

}
public void actionPerformed(ActionEvent e)
{
JButton submit1 = new JButton("Submit Employee Information");
JButton submit2 = new JButton("Submit Merchandise Information");
JButton submit3 = new JButton("Submit Customer Information");
if(e.getSource() == emp)
{
p2.removeAll();
p2.updateUI();
String[] states={"MA","AZ","CA"};
JLabel lb1 = new JLabel("First Name:");
JTextField txt1 = new JTextField(12);
JLabel lb2 = new JLabel("Last Name:");
JTextField txt2 = new JTextField(12);
JLabel lb3 = new JLabel("Address:");
JTextField txt3 = new JTextField(12);
JLabel lb4 = new JLabel("City:");
JTextField txt4 = new JTextField(12);
JLabel lb5 = new JLabel("State");
JComboBox cb1 = new JComboBox(states);
JLabel lb6 = new JLabel("ZipCode");
JTextField txt5 = new JTextField(12);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(8,1));
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(1,2));
JLabel lb7= new JLabel("Gender:");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(rb1);
bgroup.add(rb2);
JLabel lb8 = new JLabel("Submit Information:");
JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(8,1));
p3.add(lb1);
p3.add(lb2);
p3.add(lb3);
p3.add(lb4);
p3.add(lb5);
p3.add(lb6);
p3.add(lb7);
p3.add(lb8);

p5.add(txt1);
p5.add(txt2);
p5.add(txt3);
p5.add(txt4);
p4.add(rb1);
p4.add(rb2);
p5.add(cb1);
p5.add(txt5);
p5.add(p4);
p5.add(submit1);

p2.add(p3,BorderLayout.WEST);
p2.add(p5,BorderLayout.EAST);

submit1.addActionListener(this);
}

if(e.getSource() == merc)
{
p2.removeAll();
p2.updateUI();
String[] states={"MA","AZ","CA"};
JLabel lb1 = new JLabel("First Name:");
JTextField txt1 = new JTextField(12);
JLabel lb2 = new JLabel("Last Name:");
JTextField txt2 = new JTextField(12);
JLabel lb3 = new JLabel("Address:");
JTextField txt3 = new JTextField(12);
JLabel lb4 = new JLabel("City:");
JTextField txt4 = new JTextField(12);
JLabel lb5 = new JLabel("State");
JComboBox cb1 = new JComboBox(states);
JLabel lb6 = new JLabel("ZipCode");
JTextField txt5 = new JTextField(12);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(8,1));
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(1,2));
JLabel lb7= new JLabel("Gender");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
JLabel lb8 = new JLabel("Submit Information:");

JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(8,1));
p3.add(lb1);
p3.add(lb2);
p3.add(lb3);
p3.add(lb4);
p3.add(lb5);
p3.add(lb6);
p3.add(lb7);
p3.add(lb8);
p5.add(txt1);
p5.add(txt2);
p5.add(txt3);
p5.add(txt4);
p4.add(rb1);
p4.add(rb2);
p5.add(cb1);
p5.add(txt5);
p5.add(p4);
p5.add(submit2);

p2.add(p3,BorderLayout.WEST);
p2.add(p5,BorderLayout.EAST);

submit2.addActionListener(this);

}
if(e.getSource() == cust)
{
p2.removeAll();
p2.updateUI();
String[] states={"MA","AZ","CA"};
JLabel lb1 = new JLabel("First Name:");
JTextField txt1 = new JTextField(12);
JLabel lb2 = new JLabel("Last Name:");
JTextField txt2 = new JTextField(12);
JLabel lb3 = new JLabel("Address:");
JTextField txt3 = new JTextField(12);
JLabel lb4 = new JLabel("City:");
JTextField txt4 = new JTextField(12);
JLabel lb5 = new JLabel("State");
JComboBox cb1 = new JComboBox(states);
JLabel lb6 = new JLabel("ZipCode");
JTextField txt5 = new JTextField(12);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(8,1));
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(1,2));
JLabel lb7= new JLabel("Gender");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
JLabel lb8 = new JLabel("Submit Information:");
JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(8,1));
p3.add(lb1);
p3.add(lb2);
p3.add(lb3);
p3.add(lb4);
p3.add(lb5);
p3.add(lb6);
p3.add(lb7);
p3.add(lb8);

p5.add(txt1);
p5.add(txt2);
p5.add(txt3);
p5.add(txt4);
p4.add(rb1);
p4.add(rb2);
p5.add(cb1);
p5.add(txt5);
p5.add(p4);
p5.add(submit3);

p2.add(p3,BorderLayout.WEST);
p2.add(p5,BorderLayout.EAST);
final String s;
s = txt1.getText();
submit3.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent args0)
{
JOptionPane.showMessageDialog(rootPane,s);
}
});
}
}
public static void main(String[] args)
{

Retail frame = new Retail();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Retail Information");
frame.pack();
frame.setResizable(true);
frame.setVisible(true);
}


}

最佳答案

最后一个测试:

if(e.getSource()==submit1)

永远不会成功,因为 submit1 是您刚刚在 actionPerformed 开始时构建的 JButton,因此不能成为当前的源事件。

与其像这样构建新的布局组件,我建议您为 p2 使用 CardLayout 并在操作处理程序中翻转到适当的卡片。这样您就可以为所有按钮注册一次监听器,您将正确地收到所有事件的通知。

此外,您应该为每个 UI 组件注册单独的 ActionListeners,而不是使用一个巨大的 actionPerformed 来测试源。这使逻辑(和代码)更加清晰。

编辑

例如,而不是这个:

emp.addActionListener(this);
merc.addActionListener(this);
cust.addActionListener(this);

你可以这样做:

emp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// logic for click on emp button
}
});
merc.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// logic for click on merc button
}
});
// etc.

然后您就不需要为您的主类implements ActionListener

然后,如果您为 p2 使用 CardLayout,则可以在程序开始时将 Action 监听器附加到每个界面元素。这样,响应任何特定操作的逻辑就变得简单得多——只需更新适当的 UI 元素并切换要在 p2 中显示的“卡片”。参见 the docs for CardLayout有关最后一部分的更多信息。

关于java - 如何在 actionPerfomed() 中调用 actionPerformed(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13506467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com