首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >刷新Java Swing中的现有窗口

刷新Java Swing中的现有窗口
EN

Stack Overflow用户
提问于 2018-06-03 18:46:25
回答 3查看 35关注 0票数 0

我正在尝试构建一个简单的Java Swing程序,您可以在其中输入您的名字和姓氏。

有两个按钮savenewDialogsave按钮将键入的名称保存到一个名为Person的类中。newDialog按钮打开一个新窗口,其中已打印保存的名称。我的问题是,我希望旧窗口也刷新并显示文本字段中保存的名称。我怎么能这么做?谢谢大家!

代码语言:javascript
复制
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);
    }
}

个人类别:

代码语言:javascript
复制
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;
        }
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-06-03 18:50:53

最好的方法是让Person类实现ChangeListenerhttps://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());

票数 0
EN

Stack Overflow用户

发布于 2018-06-03 19:10:27

首先,感谢您的伟大建议,Guilherme Mussi。

我按以下方式更改代码。但现在这些名字不再被保存了。我不知道我做错了什么。

代码语言:javascript
复制
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);
}

}

个人类别:

代码语言:javascript
复制
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;
    }
}

}

票数 0
EN

Stack Overflow用户

发布于 2018-06-03 19:43:56

我修正了你的密码:

代码语言:javascript
复制
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));
        }
    }

}

帧类:

代码语言:javascript
复制
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);
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        fornameLabel.setText(p.getForename());
        surnameLabel.setText(p.getSurname());
    }
}

我让这些值存储在标签上。我想你会知道从现在开始该如何前进。如果你还有其他问题,请提出一个新的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50669808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档