Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFrame pane赢得';不要在重新打开按钮时单击另一个按钮_Java_Swing_Button_Jframe_Bluej - Fatal编程技术网

Java JFrame pane赢得';不要在重新打开按钮时单击另一个按钮

Java JFrame pane赢得';不要在重新打开按钮时单击另一个按钮,java,swing,button,jframe,bluej,Java,Swing,Button,Jframe,Bluej,所以我有一个问题,我需要一个乘客只选择一个座位。当第一次运行它时,它很好。然而在第二、第三、第四等方面。。。次,但用户无法选择其他选择。有什么想法吗 import java.awt.event.*; import java.awt.*; import java.io.*; import java.util.*; import javax.swing.*; import java.lang.reflect.Constructor; public class APlaneSeats { JFra

所以我有一个问题,我需要一个乘客只选择一个座位。当第一次运行它时,它很好。然而在第二、第三、第四等方面。。。次,但用户无法选择其他选择。有什么想法吗

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);
    }
}
}
导入java.awt.event.*;
导入java.awt.*;
导入java.io.*;
导入java.util.*;
导入javax.swing.*;
导入java.lang.reflect.Constructor;
公共级飞机{
JFrame=新JFrame(“飞行”);
JPanel面板=新的JPanel();
int行=8;
int列=2;
公共静态void main(字符串[]args){
新的aplaneats();
}
公共场所{
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
panel.setLayout(新的GridLayout(行、列));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
座位();
框架。添加(面板);
框架。设置尺寸(250150);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公众席{
JPanel lSeats=装载座椅();
如果(lSeats==null){
对于(int row=0;row        SwingUtilities.getWindowAncestor(button).dispose();
        panel.add(button);
    }
});
        SwingUtilities.getWindowAncestor(button).dispose();
    }
});
panel.add(button);
    if (lSeats == null) {
        //...
    } else {
        frame.remove(panel);
        panel = lSeats;
        frame.add(panel);
    }
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
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 java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class APlaneSeats {

    JFrame frame = new JFrame("Flight");
    JPanel panel = new JPanel();
    int rows = 8;
    int columns = 2;

    private Map<String, Boolean> bookings;

    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() {

        bookings = loadSeats();

        for (String key : bookings.keySet()) {

            JToggleButton button = new JToggleButton(new BookingAction(key, bookings.get(key)));
            panel.add(button);

        }

    }

    private Map<String, Boolean> loadSeats() {
        bookings = new HashMap<>(rows * columns);
        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 row = 0; row < rows; row++) {
            for (int column = 1; column < columns + 1; column++) {
                bookings.put(rowChar[row] + column, false);
            }
        }
        File seats = new File("Seats.properties");
        if (seats.exists()) {
            Properties p = new Properties();
            try (InputStream is = new FileInputStream(seats)) {
                p.load(is);
                for (Object key : p.keySet()) {
                    bookings.put(key.toString(), Boolean.parseBoolean(p.getProperty(key.toString())));
                }
            } catch (IOException exp) {
                exp.printStackTrace();
                JOptionPane.showMessageDialog(null, "An error was encountered. \n\n" + exp.toString(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
        return bookings;
    }

    private void saveSeats() {
        Properties p = new Properties();
        for (String key : bookings.keySet()) {
            p.put(key, bookings.get(key).toString());
        }
        try (OutputStream os = new FileOutputStream("Seats.properties")) {
            p.store(os, "Bookings");
        } catch (IOException exp) {
            exp.printStackTrace();
            JOptionPane.showMessageDialog(null, "An error was encountered. \n\n" + exp.toString(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    protected void setBooked(String name) {

        bookings.put(name, true);
        saveSeats();
        frame.dispose();

    }

    public class BookingAction extends AbstractAction {

        public BookingAction(String name, boolean booked) {
            super(booked ? "Booked" : name);
            putValue(SELECTED_KEY, booked);
            setEnabled(!booked);
        }

        @Override
        public void actionPerformed(ActionEvent actionEvent) {

            setBooked((String) getValue(NAME));

        }

    }
}