潇洒的茶壶 · 国家概况_中华人民共和国外交部· 2 月前 · |
豪爽的香烟 · 斯柯达ENYAQ COUPE ...· 1 年前 · |
跑龙套的凉茶 · 价格虚高缺乏竞争力 高合汽车实力存疑-手机新浪汽车· 1 年前 · |
大鼻子的椅子 · 重生之神帝归来_抖抖音· 1 年前 · |
害羞的滑板 · 常见的 ChatGPT ...· 1 年前 · |
我正在尝试构建一个简单的Java Swing程序,您可以在其中输入您的名字和姓氏。
有两个按钮
save
和
newDialog
。
save
按钮将键入的名称保存到一个名为Person的类中。
newDialog
按钮打开一个新窗口,其中已打印保存的名称。我的问题是,我希望旧窗口也刷新并显示文本字段中保存的名称。我怎么能这么做?谢谢大家!
public class PersonFrame extends JFrame {
Person p = new Person();
JTextField tf1 = new JTextField(Person.getForename());
JTextField tf2 = new JTextField(Person.getSurname());
JButton save = new JButton("Save");
JButton newDialog = new JButton("NewDialog");
public PersonFrame() {
setTitle("Frame");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel myPanel = new JPanel();
myPanel.setLayout(new GridLayout(3,2));
myPanel.add(new JLabel("Forename"));
myPanel.add(new JLabel("Surname"));
myPanel.add(tf1);
myPanel.add(tf2);
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p.setForename(tf1.getText());
p.setSurname(tf2.getText());
newDialog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.updateComponentTreeUI(myPanel);
PersonFrame personFrame = new PersonFrame();
personFrame.pack();
personFrame.setVisible(true);
myPanel.add(save);
myPanel.add(newDialog);
this.getContentPane().add(myPanel);
public static void main(String[] args) throws InterruptedException {
PersonFrame personFrame = new PersonFrame();
personFrame.pack();
personFrame.setVisible(true);
}
个人类别:
public class Person {
private static String forename;
private static String surname;
public void setForename(String forename) {
Person.forename = forename;
public void setSurname(String nachname) {
Person.surname = nachname;
public static String getSurname() {
if(Person.surname == null){
return null;
} else {
return Person.surname;
public static String getForename() {
if(Person.forename == null) {
return null;
} else {
return Person.forename;
}
最好的方法是让Person类实现
ChangeListener
:
https://docs.oracle.com/javase/7/docs/api/javax/swing/event/ChangeListener.html
然后,每一个使用个人信息的地方都会打电话给
person.addChangeListener(this);
。
在person类的每个setter中,您应该调用
stateChanged
通知侦听器
Person
已经更改。
每次窗口/屏幕接收到来自changeListener的事件时,它们都应该刷新适当的组件(对于ex,在JLabel中,您可以调用
label.setText(person.getSurname());
。
首先,感谢您的伟大建议,Guilherme Mussi。
我按以下方式更改代码。但现在这些名字不再被保存了。我不知道我做错了什么。
public class PersonFrame extends JFrame{
Person p = new Person();
JTextField tf1 = new JTextField();
JTextField tf2 = new JTextField();
JButton save = new JButton("Save");
JButton newDialog = new JButton("NewDialog");
public PersonFrame() {
setTitle("Frame");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel myPanel = new JPanel();
myPanel.setLayout(new GridLayout(3,2));
myPanel.add(new JLabel("Forename"));
myPanel.add(new JLabel("Surname"));
tf1.setText(Person.getForename());
tf2.setText(Person.getSurname());
myPanel.add(tf1);
myPanel.add(tf2);
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p.setForename(tf1.getText());
p.setSurname(tf2.getText());
newDialog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.updateComponentTreeUI(myPanel);
PersonFrame personFrame = new PersonFrame();
personFrame.pack();
personFrame.setVisible(true);
myPanel.add(save);
myPanel.add(newDialog);
this.getContentPane().add(myPanel);
public static void main(String[] args) throws InterruptedException {
PersonFrame personFrame = new PersonFrame();
personFrame.pack();
personFrame.setVisible(true);
}
}
个人类别:
public class Person {
private static String forename;
private static String surname;
public void setForename(String forename) {
ChangeListener changingForename = new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
Person.forename = forename;
public void setSurname(String surname) {
ChangeListener changingSurename = new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
Person.surname = surname;
public static String getSurname() {
if(Person.surname == null){
return null;
} else {
return Person.surname;
public static String getForename() {
if(Person.forename == null) {
return null;
} else {
return Person.forename;
}
}
我修正了你的密码:
package temp;
import java.util.ArrayList;
import java.util.List;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Person {
private String surname;
private String forename;
private List<ChangeListener> changeListeners = new ArrayList<>();
public void addChangeListener(ChangeListener listener) {
changeListeners.add(listener);
public String getSurname() {
return surname;
public void setSurname(String surname) {
this.surname = surname;
for (ChangeListener listener : changeListeners) {
listener.stateChanged(new ChangeEvent(this));
public String getForename() {
return forename;
public void setForename(String forename) {
this.forename = forename;
for (ChangeListener listener : changeListeners) {
listener.stateChanged(new ChangeEvent(this));
}
帧类:
package temp;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class PersonFrame extends JFrame implements ChangeListener {
Person p = new Person();
JTextField tf1 = new JTextField();
JTextField tf2 = new JTextField();
JButton save = new JButton("Save");
JButton newDialog = new JButton("NewDialog");
JLabel fornameLabel = new JLabel("Forename");
JLabel surnameLabel = new JLabel("Surname");
public PersonFrame() {
setTitle("Frame");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel myPanel = new JPanel();
myPanel.setLayout(new GridLayout(3,2));
myPanel.add(fornameLabel);
myPanel.add(surnameLabel);
tf1.setText(p.getForename());
tf2.setText(p.getSurname());
myPanel.add(tf1);
myPanel.add(tf2);
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p.setForename(tf1.getText());
p.setSurname(tf2.getText());
p.addChangeListener(this);
newDialog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.updateComponentTreeUI(myPanel);
PersonFrame personFrame = new PersonFrame();
personFrame.pack();
personFrame.setVisible(true);
myPanel.add(save);
myPanel.add(newDialog);
this.getContentPane().add(myPanel);
public static void main(String[] args) throws InterruptedException {
PersonFrame personFrame = new PersonFrame();
personFrame.pack();
personFrame.setVisible(true);