单击6次后,CardLayout按钮就会更改

时间:2019-01-29 21:08:53

标签: java swing jbutton actionlistener cardlayout

我正在尝试创建测验。测验从读取文件开始。该文件有6个问题。每个问题都会在cardlayout中获得自己的卡片。每张卡都有一个指向下一个CardLayout的按钮。问题1-5应该有一个“下一个”按钮,该按钮链接到下一张卡片。问题6应该有一个显示“获取结果”的按钮,该按钮将链接到显示可能结果卡之一的卡。 (这些仍在进行中,现在我只是在尝试创建按钮并使用.previous()方法对其进行测试)。截至目前,每张卡都有一个下一个按钮,似乎尚未达到添加“获取结果”按钮的声明。看一看。

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class UserInterface extends JPanel {
    public static final Object QUESTION = "question";
    // fill label with blank text to expand it
    private JLabel resultLabel = new JLabel(String.format("%150s", " "));

    // CardLayout to allow swapping of question panels
    private CardLayout cardLayout = new CardLayout();
    private JPanel centerPanel = new JPanel(cardLayout);
    private List<String> answers = new ArrayList<>();
    private int currentCard = 0; 
    private QuestionsContainer containers = new QuestionsContainer();

    public UserInterface(QuestionsContainer container) throws FileNotFoundException {
        centerPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        for (Questions question : container.getQuestions()) {
            centerPanel.add(createQPanel(question), null);
            currentCard++;

            JPanel bottomPanel = new JPanel(new BorderLayout());

            if ((currentCard == containers.questionsLength() - 1){
                bottomPanel.add(new JButton(new AbstractAction("Next") {
                   @Override
                   public void actionPerformed(ActionEvent e) {
                    cardLayout.next(centerPanel);

                }
            }),
                BorderLayout.LINE_START);
                add(bottomPanel, BorderLayout.LINE_START);
                bottomPanel.add(resultLabel);

                setLayout(new BorderLayout());
                add(bottomPanel, BorderLayout.PAGE_END);
                add(centerPanel);
        }

                JPanel bottomPanel1 = new JPanel();
                bottomPanel1.add(new JButton(new AbstractAction("Get Results") {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("hello");
                        cardLayout.previous(centerPanel);
// both of these are just to see if this statement is reached
                        bottomPanel.validate();
                        bottomPanel.repaint();
                }
            }),
                BorderLayout.LINE_START);
                add(bottomPanel1, BorderLayout.LINE_START);
                bottomPanel1.add(resultLabel);

                setLayout(new BorderLayout());
                add(bottomPanel1, BorderLayout.PAGE_END);
                add(centerPanel);
        }    
    }

    private JPanel createQPanel(Questions question) {

        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
        radioPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
        radioPanel.add(new JLabel(question.getQuestion()), BorderLayout.PAGE_START);
        ButtonGroup buttonGroup = new ButtonGroup();
        ItemListener myItemListener = new MyItemListener(this);
        for (String answer : question.getAnswer()) {
            JRadioButton answerButton = new JRadioButton(answer);

            answerButton.putClientProperty(QUESTION, question);
            answerButton.addItemListener(myItemListener);
            buttonGroup.add(answerButton);
            radioPanel.add(answerButton);
        }

        JPanel qPanel = new JPanel(new BorderLayout());
        qPanel.add(radioPanel);
        return qPanel;
    }

    public void displayResult(String selectedText) {
        resultLabel.setText(selectedText);
    }
    public void displayFinalResults(){

    }
    public static void createAndShowGui() throws FileNotFoundException {

        UserInterface mainPanel = new UserInterface(new QuestionsContainer());

        JFrame frame = new JFrame("User Interface");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

public class QuestionsContainer {

    private List<Questions> questions = new ArrayList<>();
    private int questionCount = 0;  

    QuestionsContainer() throws FileNotFoundException {

        File file = new File("src/house1.txt"); 
        try (Scanner reader = new Scanner(file)) {
            String question = "";
            List<String> answers = new ArrayList<>();

            while (reader.hasNextLine()){


                String line = reader.nextLine();
                if (line.startsWith("QUESTION: ")) {
                    question = line.substring(10);
                } else if (line.startsWith("ANSWER: ")){
                    String answer = line.substring(8);
                    answers.add(answer);
                } else if (line.isEmpty()) {
                    questions.add(new Questions(question, answers));
                    question = "";
                    answers = new ArrayList<>();
                    questionCount++;
                }
            }
        }
    }

    public List<Questions> getQuestions() {
        return questions;
    }
    public int questionsLength(){
        return questions.size();
    }

我尝试使bottomPanel成为自己的返回bottomPanel的方法。这没有达到预期的结果,因为它是在构造函数中调用的,我不认为currentCard变量每次都会累加。 现在,此代码可以很好地读取所有问题,也可以很好地读取所有答案。但是它会在每张卡上创建一个下一个按钮,而不是仅在前5个上。如果在一个奇怪的地方或一个可疑的打印呼叫中有一个变量,那很可能是我之前忘记删除/注释掉的先前测试中的剩余代码。

1 个答案:

答案 0 :(得分:1)

您似乎在考虑过程过程,而不是事件驱动过程。 currentCard的状态将在每次循环期间更新,它的状态不会在两次迭代之间“存储”。

这意味着,作为BorderLayout的副作用,将仅显示添加到容器中的最后一个组件。

相反,您需要改变思维方式,以便在触发ActionListener时,您可以更新状态并确定应执行的操作,例如

public UserInterface(QuestionsContainer container) throws FileNotFoundException {
    setLayout(new BorderLayout());
    centerPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
    for (Questions question : container.getQuestions()) {
        centerPanel.add(createQPanel(question), null);
    }
    add(centerPanel);

    JPanel navigationPane = new JPanel(new GridBagLayout());
    navigationPane.setBorder(new EmptyBorder(8, 8, 8, 8));
    JButton navButton = new JButton("Next");
    navButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            if (evt.getActionCommand().equals("Next")) {
                currentCard++;
                if (currentCard == container.questionsLength()) {
                    ((JButton) evt.getSource()).setText("Get results");
                }
                cardLayout.next(centerPanel);
            } else {
                System.out.println("Print the results");
            }
        }
    });
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.EAST;
    gbc.weightx = 1;
    navigationPane.add(navButton, gbc);
    add(navigationPane, BorderLayout.SOUTH);
}