java - JFrame Pane 在重新打开时不允许单击另一个按钮

标签 java swing button jframe bluej

所以我遇到了这个问题,我需要乘客只选择 1 个座位。第一次运行的时候没问题。然而,在第二次、第三次、第四次等...次加载时,用户无法选择其他选项。有任何想法吗?

import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.lang.reflect.Constructor;

public class APlaneSeats {

JFrame frame = new JFrame("Flight");
JPanel panel = new JPanel();
int rows = 8;
int columns = 2;
public static void main(String[] args) {
    new APlaneSeats();
}

public APlaneSeats() {
    EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {

                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                panel.setLayout(new GridLayout(rows, columns));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                Seatings();
                frame.add(panel);
                frame.setSize(250,150);

                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });
}

public void Seatings(){


    JPanel lSeats= loadSeats();

    if(lSeats == null){
        for (int row = 0; row < rows; row++) {
            String[] rowChar = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
            for (int column = 1; column < columns + 1; column++) {

                final JToggleButton button = new JToggleButton(rowChar[row] + column);
                button.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent actionEvent) {
                            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
                            boolean selected = abstractButton.getModel().isSelected();
                            if (selected) {
                                // System.out.println(selected);
                                button.setText("Booked");
                            } else {
                                button.setText(" ");
                            }

                            //JPanel bComp = (JPanel)SwingUtilities.getWindowAncestor(button).getComponent(0) ;
                            //JButton[] buttonArray = Arrays.copyOf(bComp, bComp.length, JButton[].class);
                            saveSeats(panel);

                            SwingUtilities.getWindowAncestor(button).dispose();
                        }
                    });
                panel.add(button);

            }
        }
        panel.setSize(250,150);
    }
    else{
        panel = lSeats;

        for (int row = 0; row < rows; row++) {
            String[] rowChar = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
            for (int column = 1; column < columns + 1; column++) {

                final JToggleButton button = new JToggleButton(rowChar[row] + column);
                button.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent actionEvent) {
                            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
                            boolean selected = abstractButton.getModel().isSelected();
                            if (selected) {
                                // System.out.println(selected);
                                button.setText("Booked");
                            } else {
                                button.setText(" ");
                            }

                            //JPanel bComp = (JPanel)SwingUtilities.getWindowAncestor(button).getComponent(0) ;
                            //JButton[] buttonArray = Arrays.copyOf(bComp, bComp.length, JButton[].class);
                            saveSeats(panel);

                            SwingUtilities.getWindowAncestor(button).dispose();
                            panel.add(button);
                        }
                    });

            }
        }

    }

}

private JPanel loadSeats(){
    ObjectInputStream inputFile;
    try{ //To locate and open the file
        inputFile = new ObjectInputStream(new BufferedInputStream(new FileInputStream("seats.rsy")));
    }catch(FileNotFoundException fnf){
        return null;
    }catch(IOException io){
        JOptionPane.showMessageDialog(null,"An error was encountered. \n\n"+io.toString(),"Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }

    try{
        JPanel loadedObject = (JPanel)inputFile.readObject(); //Loads the contents of the file.
        inputFile.close(); //Closes the stream
        return loadedObject;
    }catch(IOException io){
        JOptionPane.showMessageDialog(null,"An error was encountered. \n\n"+io.toString(),"Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }catch(ClassNotFoundException cnf){
        JOptionPane.showMessageDialog(null,"An error was encountered. \n\n"+cnf.toString(),"Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }
}

private void saveSeats(JPanel sSeats){
    try{ 
        ObjectOutputStream outputFile = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("seats.rsy"))); //Opens the ObjectOutputStream and a FileOutputStream (whcih is used to write to files).
        outputFile.writeObject(sSeats); //Writes the object into the file
        outputFile.flush();     //Flushes any data from the buffer to the file
        outputFile.close();     //Closes the stream since the output is done
    }catch(IOException io){
        JOptionPane.showMessageDialog(null,"An error was encountered. \n\n"+io.toString(),"Error", JOptionPane.ERROR_MESSAGE);
    }
}
}

最佳答案

您需要将第二次添加的按钮移到监听器之外:

        SwingUtilities.getWindowAncestor(button).dispose();
        panel.add(button);
    }
});

应该是这样的:

        SwingUtilities.getWindowAncestor(button).dispose();
    }
});
panel.add(button);

关于java - JFrame Pane 在重新打开时不允许单击另一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28539387/

相关文章:

swing - scala 的鼠标事件有效吗?如何?

java - 如何设置TableLayout的单元格垂直和水平对齐方式

kotlin - 点击后传递参数

java - 在Java中的不同时刻播放多种声音

java - 哪个更适合异常处理?

java - 将数组排序为三个不同的 ArrayLinearList

java - JOGL 2.0 不支持 GLCanvas ,Texture ,Animator ,但是 jogl 1.0 呢?

java - 部署WAR到tomcat时出现404

java - 设置 JTextArea 字体

javascript - 为什么以下按钮/javascript 代码不起作用?