Java 在Swing中使用setter和getter时的一个基本逻辑问题
我正在写一个基本程序来模拟用户和计算机之间的对话。我正在尝试使用setter和getter来更改另一个类中textField中的文本。单击按钮,文本字段中不会显示任何内容。这是我的密码:Java 在Swing中使用setter和getter时的一个基本逻辑问题,java,swing,Java,Swing,我正在写一个基本程序来模拟用户和计算机之间的对话。我正在尝试使用setter和getter来更改另一个类中textField中的文本。单击按钮,文本字段中不会显示任何内容。这是我的密码: public class DialogueWindow extends JFrame { SuperDialogue SD = new SuperDialogue(); JTextField textField = new JTextField(); JButton Answer1 =
public class DialogueWindow extends JFrame {
SuperDialogue SD = new SuperDialogue();
JTextField textField = new JTextField();
JButton Answer1 = new JButton();
public DialogueWindow() {
initUI();
}
public void initUI() {
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(null);
JButton Answer1 = new JButton();
Answer1.setBounds(102, 149, 113, 30);
Answer1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
textField.setText(SD.getReply1());
}
});
panel.add(Answer1);
textField = new JTextField();
textField.setBounds(56, 74, 174, 45);
panel.add(textField);
setTitle("Dialogue");
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class SuperDialogue {
private String answer;
public String getReply1(){
return this.answer;
}
public void setReply1(String a1){
this.answer = a1;
}
}
public class Conversation1 extends SuperDialogue {
public void Convo(){
String firstLine = "hello";
setReply1(firstLine);
DialogueWindow DW = new DialogueWindow();
DW.setVisible(true);
DW.setSize(300,300);
}
}
public class Main {
public static void main(String[] args) {
Conversation1 c1 = new Conversation1();
c1.Convo();
}
}
JFrame类中的SuperDialogue与在main中创建的SuperDialogue不同
SuperDialogue SD = new SuperDialogue();
此行正在创建一个单独的
超级对话框
,该对话框的值不相同。此对话框从未设置,因此getReply1()不返回任何内容。JFrame类中的SuperDialogue与在main中创建的SuperDialogue不同
SuperDialogue SD = new SuperDialogue();
此行正在创建一个单独的超级对话框
,该对话框的值不相同。此参数从未设置,因此getReply1()不返回任何内容