0
public class Main extends JFrame {
private JButton classA[] = new JButton[12];
private JButton classB[] = new JButton[12];
private JLabel emptylbls[] = new JLabel[11];
private boolean seatA_avail[] = new boolean[12];
private boolean seatB_avail[] = new boolean[12];
public Main() {
btnHandler handler = new btnHandler();
for (int i=0; i<classA.length; i++) {
classA[i] = new JButton("A-" + (i+1));
classA[i].addActionListener(handler);
}
for (int i=0; i<classB.length; i++) {
classB[i] = new JButton("B-" + (i+1));
classB[i].addActionListener(handler);
}
for (int i=0; i<emptylbls.length; i++) {
emptylbls[i] = new JLabel("");
}
Container pane = getContentPane();
pane.setLayout(new GridLayout(7,5));
pane.add(classA[0]);
pane.add(classA[1]);
pane.add(emptylbls[0]);
pane.add(classA[2]);
pane.add(classA[3]);
pane.add(classA[4]);
pane.add(classA[5]);
pane.add(emptylbls[1]);
pane.add(classA[6]);
pane.add(classA[7]);
pane.add(classA[8]);
pane.add(classA[9]);
pane.add(emptylbls[2]);
pane.add(classA[10]);
pane.add(classA[11]);
pane.add(emptylbls[3]);
pane.add(emptylbls[4]);
pane.add(emptylbls[5]);
pane.add(emptylbls[6]);
pane.add(emptylbls[7]);
pane.add(classB[0]);
pane.add(classB[1]);
pane.add(emptylbls[8]);
pane.add(classB[2]);
pane.add(classB[3]);
pane.add(classB[4]);
pane.add(classB[5]);
pane.add(emptylbls[9]);
pane.add(classB[6]);
pane.add(classB[7]);
pane.add(classB[8]);
pane.add(classB[9]);
pane.add(emptylbls[10]);
pane.add(classB[10]);
pane.add(classB[11]);
setTitle("Airline Reservation System");
setVisible(true);
setSize(500,600);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private class btnHandler implements ActionListener {
String names_classA[] = new String[12];
String contacts_classA[] = new String[12];
String names_classB[] = new String[12];
String contacts_classB[] = new String[12];
public void actionPerformed(ActionEvent e) {
JButton currentBut = (JButton) e.getSource();
for (int i=0;i<classA.length;i++){
if (classA[i].equals(currentBut) && seatA_avail[i] == false) {
names_classA[i] = JOptionPane.showInputDialog("Enter customer's name:");
contacts_classA[i] = JOptionPane.showInputDialog("Enter customer's contact no.:");
currentBut.setText("<html><p align=center>(Reserved)<br></br>" + currentBut.getText() + "</p></html>");
seatA_avail[i] = true;
}
else if (classA[i].equals(currentBut) && seatA_avail[i] == true) {
JOptionPane.showMessageDialog(null,"This seat is reserved to:\n" +
"Name: " + names_classA[i] +
"\nContact No: " + contacts_classA[i]);
}
else if (classB[i].equals(currentBut) && seatB_avail[i] == false) {
names_classB[i] = JOptionPane.showInputDialog("Enter customer's name:");
contacts_classB[i] = JOptionPane.showInputDialog("Enter customer's contact no.:");
currentBut.setText("<html><p align=ceNter>(Reserved)<br></br>" + currentBut.getText() + "</p></html>");
seatB_avail[i] = true;
}
else if (classB[i].equals(currentBut) && seatB_avail[i] == true) {
JOptionPane.showMessageDialog(null,"This seat is reserved to:\n" +
"Name: " + names_classB[i] +
"\nContact No: " + contacts_classB[i]);
}
}
}
}
public static void main(String[] args) {
Main obj = new Main();
}
}
我的代碼沒有運行,eclipse告訴我在我創建gui程序的主要方法上有些問題。我不知道我的代碼有什麼問題,但它也告訴我,obj變量不被使用。這是我完整的代碼。創建我的gui程序結果在主要方法上出錯
在此先感謝!
請您貼上您的堆棧跟蹤 – PrR3
我運行你的代碼沒有任何錯誤,請嘗試創建新的項目,並複製粘貼示例 – hudi
代碼工作得很好,但是如果Eclipse的配置就不用值下降錯誤... –