Java addActionListener不能处理某些按钮

时间:2015-11-10 22:27:03

标签: java swing actionlistener

我一直在做一个项目,这就是我的工作。我从数据库中获取数据并将它们放在一个对象数组中,这些对象是带有一些编辑的JPanel。所以这是问题所在。回到主程序中我写了一个循环,以便它将actionlisteners添加到这些JPanels中的按钮。但它不起作用。

package testgen;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class TestGen extends JFrame implements ActionListener {

private static JPanel container;
private static StudentIntro p2;
private static MainPanel p1;
private static InstructorIntro p3;
private final CardLayout cards;
private boolean isValid;
private final DBHandler DB;
private QPanel[] panels = new QPanel[10];
private final LoadingPanel n = new LoadingPanel();

public TestGen() {
    DB = new DBHandler();
    this.cards = new CardLayout();
    container = new JPanel();
    container.setLayout(cards);
    p1 = new MainPanel();
    p2 = new StudentIntro();
    p3 = new InstructorIntro();

    container.add(p1, "mainPanel");
    container.add(p2, "studentPanel");
    container.add(p3, "instructorPanel");
    container.add(n, "loading");

    p1.goNext.addActionListener(this);
    p2.back.addActionListener(this);
    p2.start.addActionListener(this);
    n.startTest.addActionListener(this);

    cards.show(container, "1");

}

public static void main(String[] args) {

    TestGen c = new TestGen();
    c.add(container);
    c.setSize(650, 550);
    c.setResizable(false);
    c.setLocationRelativeTo(null);
    c.setVisible(true);
    c.setTitle(" Fist Bump Quiz Generator");
    c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

private boolean checkValid(MainPanel p) {
    String id = p.getId();
    if (id.isEmpty()) {
        JOptionPane.showMessageDialog(null, "Invalid ID or Password!", "Try Again", JOptionPane.WARNING_MESSAGE);
        return false;
    }

    String sql = "SELECT passwords FROM `instructors` WHERE username ='" + id + "'";
    if (DB.checkConn() == false) {

        return false;
    }
    String right = DB.fetchPassword(sql);
    char[] password = p.getPassword();
    String inPass = "";
    for (int i = 0; i < password.length; i++) {
        inPass = inPass + password[i];
    }

    if (right.equals(inPass) == false) {
        JOptionPane.showMessageDialog(null, "Invalid ID or Password!", "Try Again", JOptionPane.WARNING_MESSAGE);
        return false;
    }

    return true;

}

private void addListners() {
    addL();
    for (int i = 0; i < 10; i++) {

        container.add(panels[i], "question" + (i + 1));

        if (i == 0) {
            if (panels[i].getQuesType().equals("TF")) {
                ((TFPanel) panels[i]).goPrev.setEnabled(false);
            } else if (panels[i].getQuesType().equals("Blank")) {
                ((FillBlank) panels[i]).goPrev.setEnabled(false);
            } else {
                ((ShortAns) panels[i]).goPrev.setEnabled(false);
            }

        }

        if (i == 9) {
            if (panels[i].getQuesType().equals("TF")) {
                ((TFPanel) panels[i]).goNext.setText("Submit Test");
            } else if (panels[i].getQuesType().equals("Blank")) {
                ((FillBlank) panels[i]).goNext.setText("Submit Test");
            } else {
                ((ShortAns) panels[i]).goNext.setText("Submit Test");
            }

        }

    }
}

private void quesPopulate() {
    Runnable r = new Runnable() {

        @Override
        public void run() {
            if (DB.checkConn(false) == true) {
                cards.show(container, "loading");
            }
            return;
        }
    };
    Thread t = new Thread(r);
    t.start();

    Runnable r2 = new Runnable() {

        @Override
        public void run() {
            if (DB.checkConn() == true) {
                panels = DB.fetchQuestion();
                addListners();
                n.startTest.setVisible(true);
                n.loadimg.setVisible(false);

            }
        }
    };
    Thread t2 = new Thread(r2);

    t2.start();

}

private void addL() {

    for (int i = 0; i < 10; i++) {
        switch (panels[i].getQuesType()) {
            case "TF":
                ((TFPanel) panels[i]).goPrev.addActionListener(this);
                ((TFPanel) panels[i]).goNext.addActionListener(this);
                System.out.println("Action Added " + i);
                break;
            case "Blank":
                ((FillBlank) panels[i]).goPrev.addActionListener(this);
                ((FillBlank) panels[i]).goNext.addActionListener(this);
                System.out.println("Action Added " + i);
                break;
            case "Short":
                ((ShortAns) panels[i]).goPrev.addActionListener(this);
                ((ShortAns) panels[i]).goNext.addActionListener(this);
                System.out.println("Action Added " + i);
                break;
        }
    }
}

@Override
public void actionPerformed(ActionEvent e) {

    try {
        if (e.getSource() == p1.goNext && p1.goNext.isEnabled() && "Login".equals(p1.goNext.getText())) {
            isValid = checkValid(p1);

            if (isValid) {
                cards.show(container, "instructorPanel");

            }

        } else if (e.getSource() == p1.goNext && p1.goNext.isEnabled() && p1.goNext.getText().equals("Get Started!")) {
            try {
                if (p1.getName().isEmpty()) {
                    throw new RuntimeException();

                } else {
                    cards.show(container, "studentPanel");
                    p2.setLabel(p1.getName());
                }

            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Please Enter your Name", "Try Again", JOptionPane.WARNING_MESSAGE);
            }

        } else if (e.getSource() == p2.back) {
            cards.show(container, "mainPanel");

        } else if (e.getSource() == n.startTest) {

            System.out.println("button Pressed");
            cards.show(container, "question2");

        } else if (e.getSource() == p2.start) {
            Runnable r2 = new Runnable() {

                @Override
                public void run() {
                    quesPopulate();

                }
            };

            Thread t2 = new Thread(r2);
            t2.start();

            Runnable r = new Runnable() {

                @Override
                public void run() {

                    addListners();

                }
            };
            Thread t = new Thread(r);

        } else if (e.getSource() == ((TFPanel) panels[9]).goNext) {
            int finals = 0;
            for (int j = 0; j < 10; j++) {
                panels[j].checkAnswer();
                if (panels[j].isScore() == true) {
                    finals++;
                }

            }

            System.out.println(finals);
        } else {
            for (int i = 0; i < 10; i++) {
                if (panels[i].getQuesType().equals("TF")) {
                    System.out.println("button Pressed");
                    if (e.getSource() == ((TFPanel) panels[i]).goPrev) {
                        cards.show(container, "question" + (i));
                    } else if (e.getSource() == ((TFPanel) panels[i]).goNext) {
                        cards.show(container, "question" + (i + 2));
                    }
                } else if (panels[i].getQuesType().equals("Blank")) {
                    System.out.println("button Pressed");
                    if (e.getSource() == ((FillBlank) panels[i]).goPrev) {
                        cards.show(container, "question" + (i));
                    } else if (e.getSource() == ((FillBlank) panels[i]).goNext) {
                        cards.show(container, "question" + (i + 2));
                    }

                } else if (panels[i].getQuesType().equals("Short")) {
                    System.out.println("button Pressed");
                    if (e.getSource() == ((ShortAns) panels[i]).goPrev) {
                        cards.show(container, "question" + (i));
                    } else if (e.getSource() == ((ShortAns) panels[i]).goNext) {
                        cards.show(container, "question" + (i + 2));
                    }

                }

            }
        }
    } catch (Exception ex) {

    }

}

} 编辑(添加格式化的完整类)我无法提供可运行的程序,因为它有多个类。我也从面板类外部使用ActionListener,因为我需要在按钮上切换卡片布局按。 在那种情况下的任何帮助?在此先感谢:)

0 个答案:

没有答案