使用javaBeans持久性机制保存Java

使用javaBeans持久性机制保存Java

本文介绍了使用javaBeans持久性机制保存Java GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JavaBeans持久性机制将GUI保存到本地磁盘.我面临的问题是一次保存两帧.这是我的代码.

I want to use JavaBeans Persistence mechanism to save my GUI to local disk. The problem that I faced is to save two frames at one time. Here is my code.

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import javax.swing.*;

public class BeansTest {
  private static JFileChooser chooser;
private JFrame frame;

public static void main(String[] args){
    chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));
    BeansTest test = new BeansTest();
    test.init();
}

public void init(){
    frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("PersistentFrameTest");
    frame.setSize(400,200);

    JButton registryButton = new JButton("Registry");
    frame.add(registryButton);
    registryButton.addActionListener(EventHandler.create(ActionListener.class, this, "registry"));

    JButton saveButton = new JButton("Save");
    frame.add(saveButton);
    saveButton.addActionListener(EventHandler.create(ActionListener.class, this, "save"));

    frame.setVisible(true);
}


public void registry(){

 Registry re = new Registry();
}

public void save()
{
    if(chooser.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
    {
        try{
            File file = chooser.getSelectedFile();
            XMLEncoder encoder = new XMLEncoder(new FileOutputStream(file));
            encoder.writeObject(frame);
            encoder.close();
        }
        catch(IOException e)
        {
            JOptionPane.showMessageDialog(null, e);
        }
    }
}

}



public class Registry {

public Registry(){

    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.setSize(400,200);

    JLabel nameL = new JLabel("Name:");
    JTextField nameF = new JTextField(8);

    frame.add(nameL);
    frame.add(nameF);


    frame.setVisible(true);

}
}

当我按下saveButton时,我想同时保存两个帧.现在我只能保存主机.请帮我解决这个问题.非常感谢.

I want to save two frames at the same when I press the saveButton. Now I can only save the main frame. Please help me solve this problem. Many Thanks.

推荐答案

由于无法访问Registry类中的JFrame,因此需要在该类中添加吸气剂.然后,假设Registry创建依赖于ActionListener,则在保存那个帧之前,需要检查您的注册表句柄re是否已实例化.在代码中:

As the JFrame in the Registry class is not accessible you will need to add a getter to this class. Then, given that the Registry creation is dependent on an ActionListener, you will need to check that your Registry handle re has been instantiated before saving that frame. In code:

添加到Registry:

public JFrame getFrame() {
   return frame;
}

添加到BeansTest.save():

if (re.getFrame() != null) {
   encoder.writeObject(re.getFrame());
}

此处的某些变量将需要移至全局范围.我认为您将从自己弄清楚这些内容中受益.

Some variables here will need to be moved to global scope. I think that you will benefit from figuring out these bits yourself.

这篇关于使用javaBeans持久性机制保存Java GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:08