Java 保存在JFrame中所做的更改
我有一个简单的Java 保存在JFrame中所做的更改,java,swing,jframe,Java,Swing,Jframe,我有一个简单的JFrame,用户使用jpopmpmenu更改它的背景色。当用户退出应用程序时,我想保存在后台所做的更改 我曾尝试使用WindowAdapter类中的windowClosing()方法来处理此问题,但当我再次启动应用程序时,我看不到先前所做的更改。我不知道是什么问题。我将感谢任何帮助。这是我的密码 /*i have removed unnnecessary codes*/ public class Popupframe extends JFrame{ private JRadio
JFrame
,用户使用jpopmpmenu
更改它的背景色。当用户退出应用程序时,我想保存在后台所做的更改
我曾尝试使用WindowAdapter
类中的windowClosing()
方法来处理此问题,但当我再次启动应用程序时,我看不到先前所做的更改。我不知道是什么问题。我将感谢任何帮助。这是我的密码
/*i have removed unnnecessary codes*/
public class Popupframe extends JFrame{
private JRadioButtonMenuItem[] items;
private final Color[] colorvalues={Color.BLUE,Color.YELLOW,Color.RED};
static Color bgcolor=Color.CYAN;
JRadioButtonMenuItem[] cheek;
public Popupframe() {
super("using popups");
String[] colors = {"Blue","Yellow","Red"};
setBackground(bgcolor);
addMouseListener(new Handler());
}
private class Handler extends MouseAdapter implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
for(int i=0; i<items.length; i++) {
if(event.getSource()==items[i]) {
getContentPane().setBackground(colorvalues[i]);
bgcolor=colorvalues[i];
}
}
}
}
public static void main(String[] args) {
Popupframe frame=new Popupframe();
frame.setSize(width,height);
frame.setDefaultCloseOperation(Popupframe.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int ok=JOptionPane.showConfirmDialog(frame,"are sure?","close",JOptionPane.WARNING_MESSAGE);
if(ok==JOptionPane.OK_OPTION) {
bgcolor=frame.getContentPane().getBackground();
System.out.println(bgcolor);
System.exit(0);
}
}
});
frame.setVisible(true);
}
/*我已删除了不必要的代码*/
公共类Popupframe扩展了JFrame{
私有JRadioButtonMenuItem[]项;
私有最终颜色[]colorvalues={Color.BLUE,Color.YELLOW,Color.RED};
静态颜色bgcolor=Color.CYAN;
JRadioButtonMenuItem[]脸颊;
公共弹出框(){
超级(“使用弹出窗口”);
字符串[]颜色={“蓝色”、“黄色”、“红色”};
挫折背景(bgcolor);
addMouseListener(新处理程序());
}
私有类处理程序扩展MouseAdapter实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件){
对于(int i=0;i您需要在系统之前将颜色代码保存到一个文件中(如设置文件或共享首选项)。退出(0)
并在main
中读取它并设置该颜色代码。然后它就可以正常工作。您需要在系统退出(0)之前将颜色代码保存到一个文件中(如设置文件或共享首选项)
并在主界面中读取它,然后设置该颜色代码。然后它工作正常。您需要在系统之前将颜色代码保存到一个文件中(如设置文件或共享首选项)。退出(0)
并在主界面中读取它,然后设置该颜色代码。然后它工作正常。您需要将颜色代码保存到一个文件中(如设置文件或共享首选项)在系统之前。退出(0)
并在主
中读取它,然后设置该颜色代码。这样它就可以正常工作。您没有保存颜色。颜色是可序列化的,因此您可以将对象保存在程序根目录中。将此代码放入您的WindowClosing方法中:
//serialize the Color
try (
OutputStream file = new FileOutputStream("myBgColor.ser");
OutputStream buffer = new BufferedOutputStream(file);
ObjectOutput output = new ObjectOutputStream(buffer);
){
output.writeObject(bgColor);
}
catch(IOException ex){
log.log(Level.SEVERE, "Cannot perform output.", ex);
}
重新加载应用程序时,需要恢复颜色。在PopupFrame()构造函数中,在调用setBackground(color)之前,在此处输入以下代码:
//deserialize the Color file
try(
InputStream file = new FileInputStream("myBgColor.ser");
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream (buffer);
){
//deserialize the List
bgColor = (Color)input.readObject();
}
catch(ClassNotFoundException ex){
fLogger.log(Level.SEVERE, "Cannot perform input. Class not found.", ex);
}
这应该可以解决问题。您没有持久化颜色。颜色是可序列化的,因此您可以将对象保存在程序根目录中。将此代码放入WindowClosing方法中:
//serialize the Color
try (
OutputStream file = new FileOutputStream("myBgColor.ser");
OutputStream buffer = new BufferedOutputStream(file);
ObjectOutput output = new ObjectOutputStream(buffer);
){
output.writeObject(bgColor);
}
catch(IOException ex){
log.log(Level.SEVERE, "Cannot perform output.", ex);
}
重新加载应用程序时,需要恢复颜色。在PopupFrame()构造函数中,在调用setBackground(color)之前,在此处输入以下代码:
//deserialize the Color file
try(
InputStream file = new FileInputStream("myBgColor.ser");
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream (buffer);
){
//deserialize the List
bgColor = (Color)input.readObject();
}
catch(ClassNotFoundException ex){
fLogger.log(Level.SEVERE, "Cannot perform input. Class not found.", ex);
}
这应该可以解决问题。您没有持久化颜色。颜色是可序列化的,因此您可以将对象保存在程序根目录中。将此代码放入WindowClosing方法中:
//serialize the Color
try (
OutputStream file = new FileOutputStream("myBgColor.ser");
OutputStream buffer = new BufferedOutputStream(file);
ObjectOutput output = new ObjectOutputStream(buffer);
){
output.writeObject(bgColor);
}
catch(IOException ex){
log.log(Level.SEVERE, "Cannot perform output.", ex);
}
重新加载应用程序时,需要恢复颜色。在PopupFrame()构造函数中,在调用setBackground(color)之前,在此处输入以下代码:
//deserialize the Color file
try(
InputStream file = new FileInputStream("myBgColor.ser");
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream (buffer);
){
//deserialize the List
bgColor = (Color)input.readObject();
}
catch(ClassNotFoundException ex){
fLogger.log(Level.SEVERE, "Cannot perform input. Class not found.", ex);
}
这应该可以解决问题。您没有持久化颜色。颜色是可序列化的,因此您可以将对象保存在程序根目录中。将此代码放入WindowClosing方法中:
//serialize the Color
try (
OutputStream file = new FileOutputStream("myBgColor.ser");
OutputStream buffer = new BufferedOutputStream(file);
ObjectOutput output = new ObjectOutputStream(buffer);
){
output.writeObject(bgColor);
}
catch(IOException ex){
log.log(Level.SEVERE, "Cannot perform output.", ex);
}
重新加载应用程序时,需要恢复颜色。在PopupFrame()构造函数中,在调用setBackground(color)之前,在此处输入以下代码:
//deserialize the Color file
try(
InputStream file = new FileInputStream("myBgColor.ser");
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream (buffer);
){
//deserialize the List
bgColor = (Color)input.readObject();
}
catch(ClassNotFoundException ex){
fLogger.log(Level.SEVERE, "Cannot perform input. Class not found.", ex);
}
这应该可以解决问题。1)为了更快地获得更好的帮助,请发布一个(最小完整的可验证示例)。2)它在该源中的何处尝试保存或恢复颜色?3)请参阅一个工作示例。@Andrew Thompson我已编辑了该问题。bgcolor是对颜色的引用(我现在在问题中已显示)@尽管我没有保存颜色,我想JVM会记得我之前所做的更改。但是在您的提醒和其他帮助下,我已经在closer上将颜色对象(bgcolor)保存在一个文件中,并从文件中读取它(我将fileinputstream(“filename.ser”)作为main方法中的第一条语句).evetything有效!1)为了更快地获得更好的帮助,请发布一个(最小完整的可验证示例)。2)它在该源中的何处尝试保存或还原颜色?3)请参阅以获取一个有效示例。@安德鲁·汤普森我已编辑了该问题。bgcolor是对颜色的引用(我现在在问题中已显示)@尽管我没有保存颜色,我想JVM会记得我之前所做的更改。但是在您的提醒和其他帮助下,我已经在closer上将颜色对象(bgcolor)保存在一个文件中,并从文件中读取它(我将fileinputstream(“filename.ser”)作为main方法中的第一条语句).evetything有效!1)为了更快地获得更好的帮助,请发布一个(最小完整的可验证示例)。2)它在该源中的何处尝试保存或还原颜色?3)请参阅以获取一个有效示例。@安德鲁·汤普森我已编辑了该问题。bgcolor是对颜色的引用(我现在在问题中已显示)@尽管我没有保存颜色,我想JVM会记得我之前所做的更改。但是在您的提醒和其他帮助下,我已经在closer上将颜色对象(bgcolor)保存在一个文件中,并从文件中读取它(我将fileinputstream(“filename.ser”)作为main方法中的第一条语句).evetything有效!1)为了更快地获得更好的帮助,请发布一个(最小完整的可验证示例)。2)它在该源中的何处尝试保存或还原颜色?3)请参阅以获取一个有效示例。@安德鲁·汤普森我已编辑了该问题。bgcolor是对颜色的引用(我现在在问题中已显示)@尽管我没有保存颜色,我想JVM会记得我之前所做的更改。但是在您的提醒和其他帮助下,我已经在closer上将颜色对象(bgcolor)保存在一个文件中,并从文件中读取它(我将fileinputstream(“filename.ser”)作为第一条语句)