尝试在JButton上启动程序

时间:2017-04-26 01:20:12

标签: java jbutton

我正在尝试创建一个简单的程序,询问用户一个问题,然后将他们的答案与存储的正确答案进行比较。问题似乎是当我点击Ready JButton时程序的主循环没有运行。

我尝试将main方法更改为另一个非默认名称,并在actionPerformed()方法中添加对它的调用,它似乎确实执行了至少一次循环,但后来导致无法关闭单击按钮后,没有任务管理器的applet。我不知道这是否意味着它会无限循环或什么。

我确信除了这个问题之外,还有更多要修复的代码,但是如果不首先清除它,我就无法取得任何进展。如果我创建GUI的方式有问题,请告诉我。我试图将它从我做过的工作中解决得很好,所以我不知道这是不是问题。

感谢您提供的任何帮助。

以下是代码:

import java.awt.*;
import javax.swing.*;

public class Langarden_App extends JApplet{
    private int width = 800, height = 600;
    private LangardenPanel langPanel;

    public void init(){
        langPanel = new LangardenPanel();
        getContentPane().add(langPanel);
        setSize(width, height);
    }
}

具有逻辑的类

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.*;

public class LangardenPanel extends JPanel{

    private static JButton submit;
    private static JButton ready = new JButton("Ready");
    private static JLabel instruct;
    private JTextField input = new JTextField(100);
    private static String inString;
    private static ArrayList<String> questions;
    private static ArrayList<String> answers;
    private static Random rand = new Random();

    public LangardenPanel(){
        questions = new ArrayList<String> (Arrays.asList("¿Qué es la palabra para 'inveirno' en ingles?", "¿Qué es la forma de 'yo' del verbo 'venir'?"));
        answers = new ArrayList<String> (Arrays.asList("winter", "voy"));
        instruct = new JLabel("Welcome to Langarden! Press Submit to begin. You have one minute and three attempts for each question.");
        submit = new JButton("Submit");
        this.setLayout(new BorderLayout());
        add(BorderLayout.SOUTH, ready);
        add(BorderLayout.NORTH, instruct);
        add(BorderLayout.CENTER, input);
        ready.addActionListener(new SubListener());
    }

    public static void main(String[] args){
        try{
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e){
        }
        System.out.println("Setting text");
        instruct.setText("Alright! Let's Go!");
        try{
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e){
        }
        do{
            System.out.println("Running loop");
            int qnum = rand.nextInt(questions.size());
            instruct.setText(questions.get(qnum));
            for (int i=0; i<3; i++){
                try {
                    TimeUnit.SECONDS.sleep(60);
                } catch (InterruptedException e) {
                }
                if(checkAnswer(qnum, inString)){
                    instruct.setText("Correct!");
                    break;
                } else {
                    instruct.setText("Try again...\n" + questions.get(qnum));
                }
            }
            instruct.setText("The correct answer was " + answers.get(qnum));
            try{
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e){
            }
            questions.remove(qnum);
            answers.remove(qnum);
            instruct.setText("Would you like to continue? Enter No and click Submit to exit.");
        } while (!inString.equalsIgnoreCase("No") && questions.size() != 0);
        instruct.setText("Congratulations, you have completed this module!");
    }

    private static boolean checkAnswer(int qnum, String inString) {
        if (answers.get(qnum).equalsIgnoreCase(inString))
            return true;
        return false;
    }

    private class SubListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.out.println("Button Pressed!");
            if(e.getSource() == ready){
                add(BorderLayout.SOUTH, submit);
                submit.addActionListener(new SubListener());
            } else {
                inString = input.getText();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

  1. 摆脱主要方法。如果它作为applet运行,那么该程序没有业务主要方法。
  2. 所有字段设为非静态字段。是全部
  3. 摆脱while-true循环。如果这样运行,这将破坏你的Swing事件线程,使你的GUI冻结和无助。使用Swing Timer作为“伪”循环。有关详情,请查看Swing Timer Tutorial
  4. 每次向容器添加组件时,都应该在同一容器上调用revalidate()repaint(),以便容器的布局管理器可以布局新组件,以便操作系统可以重新绘制任何“脏”的像素。
  5. 不是像你一样添加新的JButton,而是使用CardLayout交换组件要好得多。可以在此处找到该教程:CardLayout tutorial