Java 为什么可以';当我运行程序时,我不能检索我的按钮吗?
所以,我的程序是一个在运行时添加按钮的用户。当他/她单击“保存”按钮时,程序将保存到文件中。但当我再次运行它时,按钮不见了。我尝试使用XMLEncoder和XMLDecoder序列化按钮,但当我运行程序时,它没有保存任何内容,而是重新开始我如何正确地序列化它,以便在启动程序时,按钮都在那里?如有任何帮助,将不胜感激。Java 为什么可以';当我运行程序时,我不能检索我的按钮吗?,java,swing,button,serialization,Java,Swing,Button,Serialization,所以,我的程序是一个在运行时添加按钮的用户。当他/她单击“保存”按钮时,程序将保存到文件中。但当我再次运行它时,按钮不见了。我尝试使用XMLEncoder和XMLDecoder序列化按钮,但当我运行程序时,它没有保存任何内容,而是重新开始我如何正确地序列化它,以便在启动程序时,按钮都在那里?如有任何帮助,将不胜感激。 以下是我的代码片段: public class saveButton { //JFrame and JPanels have been declared earl
以下是我的代码片段:
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();
}
}
}
解码对象后,需要适当地强制转换对象,然后将组件添加到容器中 这是一个非常基本的示例,每次单击随机按钮时,都会在面板上生成随机数目的按钮。单击“保存”时,面板将保存到磁盘,单击“加载”时,面板将从磁盘加载并重新应用到容器中
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++;
}
}
}
}
导入java.awt.BorderLayout;
导入java.awt.EventQueue;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Window;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.beans.xml解码器;
导入java.beans.xmlcoder;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公开课考试{
公共静态void main(字符串[]args){
新测试();
}
公开考试(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
专用面板;
公共测试窗格(){
setLayout(新的BorderLayout());
JPanel actions=新的JPanel();
JButton random=新JButton(“随机”);
JButton save=新JButton(“save”);
JButton load=新JButton(“load”);
动作。添加(随机);
操作。添加(保存);
动作。添加(加载);
添加(操作,边界布局。南部);
random.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
如果(窗格!=null){
移除(窗格);
}
窗格=新的随机按钮窗格();
pane.randomise();
添加(窗格);
Window Window=SwingUtilities.windowForComponent(TestPane.this);
window.pack();
window.setLocationRelativeTo(空);
}
});
save.addActionListener(新建ActionListener()){
@凌驾
已执行的公共无效操作(操作事件e){
如果(窗格!=null){
try(OutputStream os=newfileoutputstream(新文件(“Save.dat”)){
尝试(XMLEncoder编码器=新的XMLEncoder(os)){
编码器写入对象(窗格);
移除(窗格);
窗格=空;
}
}捕获(IOEXP异常){
exp.printStackTrace();
}
}
}
});
load.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
如果(窗格!=null){
移除(窗格);
窗格=空;
}
try(InputStream=newfileinputstream(新文件(“Save.dat”)){
try(XMLDecoder解码器=新的XMLDecoder(is)){
Object value=decoder.readObject();
if(随机按钮平面的值实例){
窗格=(随机按钮窗格)值;
窗格。重新验证();
添加(窗格);
}
}
}捕获(IOEXP异常){
exp.printStackTrace();
}
Window Window=SwingUtilities.windowForComponent(TestPane.this);
window.pack();
window.setLocationRelativeTo(空);
}
});
}
}
公共静态类RandomButtonPane扩展了JPanel{
公共网页(){
setLayout(新的GridBagLayout());
}
公共空间随机化(){
整数计数=((int)(Math.random()*100))+1;
GridBagConstraints gbc=新的GridBagConstraints();
gbc.gridx=0;
gbc.gridy=0;
for(int index=0;index