2016-03-03 67 views
0

所以,我的程序是一个用户在运行时添加按钮。当他/她点击“保存”按钮时,程序被保存到一个文件中。但是当我再次运行它时,按钮不见了。我尝试使用XMLEncoder和XMLDecoder序列化我的按钮,但是当我运行我的程序时,它没有保存任何内容,它重新开始。 我会如何正确地序列化这个,以便当我启动我的程序时,按钮在那里?任何帮助,将不胜感激。
这里是我的代码片段:为什么我运行程序时无法检索按钮?

public class saveButton 
    { 
    //JFrame and JPanels have been declared earlier 

    class ClickListener implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      str = JOptionPane.showInputDialog("What is the name of the new button?"); 
      JButton b = new JButton(str); 
      frame.add(b); 
      try 
      { 
       XMLEncoder encdr = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("file.ser"))); 
       encdr.writeObject(new JButton(str)); 
       encdr.close(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    } 

    ActionListener addButtonClicked = new ClickListener(); 
    b.addActionListener(addButtonClicked); 

    class ClickListenerTwo implements ActionListener 
    { 
     public void actionPerformed(ActionEvent f) 
     { 
      try 
      { 
       XMLDecoder d = new XMLDecoder(new BufferedInputStream(new FileInputStream("file.ser"))); 
       Object result = d.readObject(); 
       d.close(); 
      } 
      catch (IOException decoder) 
      { 
       decoder.printStackTrace(); 
      } 
     } 

    } 
+0

序列化你的按钮是不是要走来这里的路上。首先,你为什么试图序列化它们?定位?还是为了别的? –

+0

我在序列化它们,以便稍后运行我的程序时可以检索它们。 –

+0

你的解码器不会做任何事情,它应该将按钮添加回屏幕 – MadProgrammer

回答

4

一旦你解码对象,需要适当地投对象,然后将组件添加到容器中。

这是一个非常基本的例子,每次点击随机按钮时会在面板上生成随机数量的按钮。当您单击保存,面板被保存到硬盘,当你点击负载,它从磁盘加载面板并重新应用它的容器

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Window; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.beans.XMLDecoder; 
import java.beans.XMLEncoder; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private RandomButtonPane pane; 

     public TestPane() { 
      setLayout(new BorderLayout()); 

      JPanel actions = new JPanel(); 

      JButton random = new JButton("Random"); 
      JButton save = new JButton("Save"); 
      JButton load = new JButton("Load"); 

      actions.add(random); 
      actions.add(save); 
      actions.add(load); 

      add(actions, BorderLayout.SOUTH); 

      random.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        if (pane != null) { 
         remove(pane); 
        } 

        pane = new RandomButtonPane(); 
        pane.randomise(); 
        add(pane); 

        Window window = SwingUtilities.windowForComponent(TestPane.this); 
        window.pack(); 
        window.setLocationRelativeTo(null); 
       } 
      }); 

      save.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        if (pane != null) { 
         try (OutputStream os = new FileOutputStream(new File("Save.dat"))) { 
          try (XMLEncoder encoder = new XMLEncoder(os)) { 
           encoder.writeObject(pane); 
           remove(pane); 
           pane = null; 
          } 
         } catch (IOException exp) { 
          exp.printStackTrace(); 
         } 
        } 
       } 
      }); 

      load.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        if (pane != null) { 
         remove(pane); 
         pane = null; 
        } 

        try (InputStream is = new FileInputStream(new File("Save.dat"))) { 
         try (XMLDecoder decoder = new XMLDecoder(is)) { 
          Object value = decoder.readObject(); 
          if (value instanceof RandomButtonPane) { 
           pane = (RandomButtonPane)value; 
           pane.revalidate(); 
           add(pane); 
          } 
         } 
        } catch (IOException exp) { 
         exp.printStackTrace(); 
        } 
        Window window = SwingUtilities.windowForComponent(TestPane.this); 
        window.pack(); 
        window.setLocationRelativeTo(null); 
       } 
      }); 
     } 

    } 

    public static class RandomButtonPane extends JPanel { 

     public RandomButtonPane() { 
      setLayout(new GridBagLayout()); 
     } 

     public void randomise() { 
      int count = ((int) (Math.random() * 100)) + 1; 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 
      for (int index = 0; index < count; index++) { 
       if (index % 10 == 0) { 
        gbc.gridx = 0; 
        gbc.gridy++; 
       } 
       add(new JButton(Integer.toString(index)), gbc); 
       gbc.gridx++; 
      } 
     } 
    } 

} 
+0

这将帮助我,现在我知道要使用什么。 –

+0

我看到你有一个“加载”按钮,当我运行该程序时,是否有一种方法可以加载按钮? –

+0

当然,只需将载入按钮“ActionListener”中的代码放入方法中并调用它即可 – MadProgrammer

相关问题