gpt4 book ai didi

java - 如何让JFrame存储数据而不刷新?

转载 作者:太空宇宙 更新时间:2023-11-04 10:26:17 24 4
gpt4 key购买 nike

即使在关闭 jframe 以打开单独的 jframe 后,如何将信息存储在 jframe 中,以便重新打开第一个 jframe 时,仍然列出相同的信息?

所以基本上我有一个带有搜索按钮和配置文件按钮的 jFrame。查看配置文件按钮会将您带到存储所有“搜索历史记录”的不同 Jframe。但是,每次您返回主页并再次打开配置文件时,配置文件都会刷新并再次开始。我希望该按钮再次打开完全相同的窗口,而不是 GUI 的新窗口。我怎么做?

最佳答案

如果我答对了你的问题,你需要的是 secondFrame.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

但是,不建议使用 2 个 JFrame。您可以使用 JDialog 作为“第二帧”。

此外,如果您在按钮的操作监听器中初始化它,则它将无法工作,因为每次单击该按钮时,它都会创建一个新的对话框。所以你必须先初始化它。看一下这个例子:

package test;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TestFrame extends JFrame {
private JDialog secondFrame;

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new TestFrame().setVisible(true);
});
}

public TestFrame() {
super("Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout());
initializeSecondFrame();

JButton button = new JButton("Show \"Second Frame\".");
button.addActionListener(e -> {
secondFrame.setVisible(true);
});
getContentPane().add(button);
setSize(300, 300);
}

private void initializeSecondFrame() {
secondFrame = new JDialog(this);
// When "X" button is presed, dialog does nothing.
secondFrame.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
secondFrame.getContentPane().setLayout(new FlowLayout());
JTextField textField = new JTextField(10);
secondFrame.getContentPane().add(textField);
secondFrame.setSize(300, 300);
}
}

该按钮所做的唯一事情是使对话框(已启动)可见。现在,如果您在对话框(也称为第二帧)中编辑文本字段,然后将其关闭,然后再次按按钮,文本字段将包含其文本。

关于java - 如何让JFrame存储数据而不刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50488503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com