So this is my code below. I have created a Menu bar in a panel which has three Menus, the first one being "Add Information". In Add Information, I have added three Menu items: Employee, Merchandise and Customer. Upon clicking an appropriate Menu item, a form should appear in another panel below the original panel containing the Menubar. This form should take input of either Employee, Merchandise or Customer depending on which Menuitem was clicked. After filling the form, there is a submit button (within the form) clicking on which should create a new window/pop-up/dialog box which should print the details that were entered in the form. My problem starts from line no. 216 to line no. 225. When I click on the button "submit3" of the Customer menuitem, the pop-up appears but does not show the contents of the string that contains contents of "txt1". How do I pass the values of my components to actionPerformed so that it can print them in a new pop-up window?
所以这是我的代码。我在面板中创建了一个菜单栏,其中有三个菜单,第一个是“添加信息”。在添加信息中,我添加了三个菜单项:员工,商品和客户。单击相应的菜单项后,表单应显示在包含菜单栏的原始面板下方的另一个面板中。此表单应根据单击的Menuitem输入Employee,Merchandise或Customer。填写表单后,有一个提交按钮(在表单内)点击该按钮应该创建一个新的窗口/弹出/对话框,它应该打印在表单中输入的详细信息。我的问题从第一行开始。 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);
}
}
2 个解决方案
#1
2
Your code has several issues, but the source of your problem is the timing of when you create the String, s. You are creating it when you create the JPanel that you display, not when the submit button has been pressed. One quick stop-gap solution is to make your JTextFields final, and then extract the text from them inside of the submit JButton's ActionListener.
您的代码有几个问题,但问题的根源是您何时创建String,s。您在创建显示的JPanel时创建它,而不是在按下提交按钮时创建它。一个快速的间隙解决方案是使您的JTextField成为最终,然后从提交JButton的ActionListener中提取它们的文本。
e.g.,
if (e.getSource() == cust) {
p2.removeAll();
p2.updateUI();
String[] states = { "MA", "AZ", "CA" };
JLabel lb1 = new JLabel("First Name:");
final JTextField txt1 = new JTextField("Foo", 12);
// ....
// final String s;
// s = txt1.getText();
submit3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent args0) {
String s = txt1.getText();
JOptionPane.showMessageDialog(rootPane, s);
}
});
}
Having said this, if this were my code, I'd re-design it using a better overall plan.
话虽如此,如果这是我的代码,我会使用更好的整体计划重新设计它。
Suggestions include:
- Subdivide this large class into logical sub-classes.
- Use CardLayout to swap views.
- Add a little MVC or Model-View-Control structure to separate out the program logic from the GUI.
- Use variable names that make sense so that my code became self-commenting. For instance, instead of txt1, I'd name my JTextField firstNameField or something similar.
- Use AbstractActions for each of my JButtons rather than one huge ActionListener for all to share.
将这个大类细分为逻辑子类。
使用CardLayout交换视图。
添加一个MVC或模型 - 视图 - 控制结构,以从GUI中分离出程序逻辑。
使用有意义的变量名称,以便我的代码成为自我评论。例如,代替txt1,我将我的JTextField命名为firstNameField或类似名称。
对我的每个JButtons使用AbstractActions而不是一个巨大的ActionListener供所有人共享。
#2
2
Maybe you can make JTextField txt1
, an instance variable which means that you put it in class scope variable Instead of placing the JTextField txt1
inside the actionPerformedMethod
You can move the declaration like this:
也许你可以创建JTextField txt1,一个实例变量,这意味着你将它放在类范围变量中而不是将JTextField txt1放在actionPerformedMethod中你可以像这样移动声明:
public class Retail extends JFrame implements ActionListener
{
private JTextField txt1 = new JTextField(12);
Now you can refer the txt1
like this
现在您可以像这样引用txt1
submit3.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent args0)
{
String text = Retail.this.txt1.getText();
JOptionPane.showMessageDialog(rootPane,s);
}
});
#1
2
Your code has several issues, but the source of your problem is the timing of when you create the String, s. You are creating it when you create the JPanel that you display, not when the submit button has been pressed. One quick stop-gap solution is to make your JTextFields final, and then extract the text from them inside of the submit JButton's ActionListener.
您的代码有几个问题,但问题的根源是您何时创建String,s。您在创建显示的JPanel时创建它,而不是在按下提交按钮时创建它。一个快速的间隙解决方案是使您的JTextField成为最终,然后从提交JButton的ActionListener中提取它们的文本。
e.g.,
if (e.getSource() == cust) {
p2.removeAll();
p2.updateUI();
String[] states = { "MA", "AZ", "CA" };
JLabel lb1 = new JLabel("First Name:");
final JTextField txt1 = new JTextField("Foo", 12);
// ....
// final String s;
// s = txt1.getText();
submit3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent args0) {
String s = txt1.getText();
JOptionPane.showMessageDialog(rootPane, s);
}
});
}
Having said this, if this were my code, I'd re-design it using a better overall plan.
话虽如此,如果这是我的代码,我会使用更好的整体计划重新设计它。
Suggestions include:
- Subdivide this large class into logical sub-classes.
- Use CardLayout to swap views.
- Add a little MVC or Model-View-Control structure to separate out the program logic from the GUI.
- Use variable names that make sense so that my code became self-commenting. For instance, instead of txt1, I'd name my JTextField firstNameField or something similar.
- Use AbstractActions for each of my JButtons rather than one huge ActionListener for all to share.
将这个大类细分为逻辑子类。
使用CardLayout交换视图。
添加一个MVC或模型 - 视图 - 控制结构,以从GUI中分离出程序逻辑。
使用有意义的变量名称,以便我的代码成为自我评论。例如,代替txt1,我将我的JTextField命名为firstNameField或类似名称。
对我的每个JButtons使用AbstractActions而不是一个巨大的ActionListener供所有人共享。
#2
2
Maybe you can make JTextField txt1
, an instance variable which means that you put it in class scope variable Instead of placing the JTextField txt1
inside the actionPerformedMethod
You can move the declaration like this:
也许你可以创建JTextField txt1,一个实例变量,这意味着你将它放在类范围变量中而不是将JTextField txt1放在actionPerformedMethod中你可以像这样移动声明:
public class Retail extends JFrame implements ActionListener
{
private JTextField txt1 = new JTextField(12);
Now you can refer the txt1
like this
现在您可以像这样引用txt1
submit3.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent args0)
{
String text = Retail.this.txt1.getText();
JOptionPane.showMessageDialog(rootPane,s);
}
});