【发布时间】:2022-06-30 22:03:06
【问题描述】:
我一直在尝试制作一个功能,其中用户按下 JFrame 中的按钮。按下按钮后,另一个 JFrame 中的所有文本字段、禁用按钮将重置,(文本字段为空,按钮已启用)但我遇到了一个错误,即我的代码不执行任何操作,或者无法访问另一个类的文本字段和按钮
主类:
public static void main(String[] args) {
UserInterface userinterface = new UserInterface();
userinterface.frame_main.setVisible(true);
}
第一帧:
public class UserInterface{
public JFrame frame_main = new JFrame();
JPanel pnl_main = new JPanel();
JLabel lbl_firstname = new JLabel("First Name: ");
JLabel lbl_lastname = new JLabel("Last Name: ");
JLabel lbl_midname = new JLabel("Middle Name: ");
JLabel lbl_customerno = new JLabel("Contact Number: ");
JLabel lbl_customeremail = new JLabel("Email Address: ");
JButton btn_submit = new JButton("Submit");
JButton btn_clear = new JButton("Clear All");
JTextField txt_firstname = new JTextField(15);
JTextField txt_lastname = new JTextField(15);
JTextField txt_midname = new JTextField(15);
JTextField txt_customerno = new JTextField(13);
JTextField txt_customeremail = new JTextField(15);
FlowLayout fl = new FlowLayout();
Font set_font = new Font("", Font.BOLD, 14);
public UserInterface(){
frame_main.setSize(300,300);
frame_main.setLocation(200,200);
frame_main.setTitle("Event Driven Program");
frame_main.setResizable(false);
frame_main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
txt_firstname.setFont(set_font);
txt_lastname.setFont(set_font);
txt_midname.setFont(set_font);
txt_customerno.setFont(set_font);
txt_customeremail.setFont(set_font);
pnl_main.add(lbl_firstname);
pnl_main.add(txt_firstname);
pnl_main.add(lbl_lastname);
pnl_main.add(txt_lastname);
pnl_main.add(lbl_midname);
pnl_main.add(txt_midname);
pnl_main.add(lbl_customerno);
pnl_main.add(txt_customerno);
pnl_main.add(lbl_customeremail);
pnl_main.add(txt_customeremail);
btn_submit.addActionListener(new actionButton1());
btn_submit.setEnabled(true);
pnl_main.add(btn_submit);
btn_clear.addActionListener(new actionButton2());
pnl_main.add(btn_clear);
frame_main.add(pnl_main);
}
public JButton getButton(){
return btn_submit;
}
class actionButton1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
String firstname = txt_firstname.getText();
String lastname = txt_lastname.getText();
String midname = txt_midname.getText();
String customerno = txt_customerno.getText();
String customeremail = txt_customeremail.getText();
getButton().setEnabled(false);
new CustomerForm(firstname, lastname, midname, customerno, customeremail).setVisible(true);
}
}
class actionButton2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
txt_firstname.setText("");
txt_lastname.setText("");
txt_midname.setText("");
txt_customerno.setText("");
txt_customeremail.setText("");
getButton().setEnabled(true);
}
}
}
第2帧(按钮问题所在):
public class CustomerForm extends JFrame{
JPanel pnl_info = new JPanel();
JTextArea output_area = new JTextArea(20, 20);
JFrame frame_info = new JFrame();
Font set_font = new Font("", Font.BOLD, 14);
FlowLayout fl = new FlowLayout();
JButton btn_okay = new JButton("Okay");
String firstname;
String lastname;
String midname;
String customerno;
String customeremail;
UserInterface ui = new UserInterface();
public CustomerForm(String firstname, String lastname, String midname, String customerno, String customeremail) {
this.setSize(300,600);
this.setLocation(500,300);
output_area.setBackground(Color.LIGHT_GRAY);
output_area.setEditable(false);
output_area.setFont(set_font);
btn_okay.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
CustomerForm.this.dispose();
ui.btn_submit.setEnabled(true);
ui.txt_firstname.setText("");
ui.txt_lastname.setText("");
ui.txt_midname.setText("");
ui.txt_customerno.setText("");
ui.txt_customeremail.setText("");
}
});
pnl_info.add(output_area);
pnl_info.add(btn_okay);
this.firstname = firstname;
this.lastname = lastname;
this.midname = midname;
this.customerno = customerno;
this.customeremail = customeremail;
output_area.append("\nFirst Name: " + firstname +
"\n\nLast Name: " + lastname +
"\n\nMiddle Name: " + midname +
"\n\nContact Number: " + customerno +
"\n\nEmail Address: " + customeremail);
this.add(pnl_info, BorderLayout.CENTER);
}
}
我认为问题在于我参考了第一帧,但我还不知道是什么导致了真正的问题希望有人可以帮助我,这将是我学习 Java 的一大推动力。
【问题讨论】: