创建我的gui程序会导致main方法出错

时间:2013-09-23 13:07:09

标签: java swing

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变量。这是我的完整代码。

提前致谢!

1 个答案:

答案 0 :(得分:0)

我刚刚运行你的程序并没有错。也许你没有正确导入。确保您在 Main 类的顶部有以下内容。

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Main extends JFrame {
    ....
    ....
}