所以我遇到了这个问题,我需要乘客只选择 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/