我正在尝试使用JFrame
在Java中进行“简单”测验。基本上长话短说...当用户在问题2之后点击“下一步”按钮时,它不会显示下一个问题......
如何获取代码以进入问题3?
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class SimpleQuiz implements KeyListener, ActionListener
{
static final int WIDTH = 900, HEIGHT = 600;
static final Font FONT = new Font("Arial", Font.BOLD, 20);
static final Color DARKGREEN = new Color(0, 140, 0);
int correct = 0;
boolean start = false , Q1 = false , Q2 = false, Q3 = false;
JFrame window;
JMenuBar Menu;
JMenu startMenu;
JMenuItem GoAction;
JRadioButton radButton1;
JRadioButton radButton2;
JRadioButton radButton3;
JRadioButton radButton4;
JLabel question1;
JLabel question2;
JLabel question3;
JLabel score;
JButton next1;
JButton next2;
JButton finish;
JCheckBox checkBox1;
JCheckBox checkBox2;
JCheckBox checkBox3;
JCheckBox checkBox4;
JList listBox;
public static void main(String[] args)
{
new SimpleQuiz();
}
public SimpleQuiz()
{
window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(null);
window.setSize(WIDTH, HEIGHT);
window.setTitle("Quiz");
window.getContentPane().setBackground(Color.WHITE);
window.setLocationRelativeTo(null);
window.setResizable(false);
//
//
//Question 1
//
//
radButton1 = new JRadioButton();
radButton1.setFont(new Font("Arial", Font.BOLD, 14));
radButton1.setForeground(Color.BLACK);
radButton1.setSize(160, 30);
radButton1.setText("Los Angeles");
radButton1.setLocation(300, 180);
radButton1.setFocusable(false);
radButton1.setVisible(false);
window.add(radButton1);
radButton2 = new JRadioButton();
radButton2.setFont(new Font("Arial", Font.BOLD, 14));
radButton2.setForeground(Color.BLACK);
radButton2.setSize(160, 30);
radButton2.setText("Detroit");
radButton2.setLocation(300, 210);
radButton2.setFocusable(false);
radButton2.setVisible(false);
window.add(radButton2);
radButton3 = new JRadioButton();
radButton3.setFont(new Font("Arial", Font.BOLD, 14));
radButton3.setForeground(Color.BLACK);
radButton3.setSize(160, 30);
radButton3.setText("Shanghai");
radButton3.setLocation(300, 240);
radButton3.setFocusable(false);
radButton3.setVisible(false);
window.add(radButton3);
radButton4 = new JRadioButton();
radButton4.setFont(new Font("Arial", Font.BOLD, 14));
radButton4.setForeground(Color.BLACK);
radButton4.setSize(160, 30);
radButton4.setText("New York City");
radButton4.setLocation(300, 270);
radButton4.setFocusable(false);
radButton4.setVisible(false);
window.add(radButton4);
question1 = new JLabel();
question1.setSize(550, 30);
question1.setLocation(160, 120);
question1.setFont(new Font("Arial", Font.BOLD, 16));
question1.setText("Which one of these cities is not located in the United states of America:");
question1.setOpaque(true);
question1.setBackground(Color.WHITE);
question1.setForeground(Color.BLACK);
question1.setVisible(false);
window.add(question1);
next1 = new JButton();
next1.setFont(new Font("Arial", Font.BOLD, 28));
next1.setForeground(Color.BLACK);
next1.setSize(160, 30);
next1.setText("NEXT");
next1.setActionCommand("q2");
next1.setLocation(700, 500);
next1.setFocusable(false);
next1.setVisible(false);
next1.addActionListener(this);
window.add(next1);
//
//
//Question 2
//
//
checkBox1 = new JCheckBox();
checkBox1.setFont(new Font("Arial", Font.BOLD, 14));
checkBox1.setForeground(Color.BLACK);
checkBox1.setSize(160, 30);
checkBox1.setText("13");
checkBox1.setLocation(300, 180);
checkBox1.setFocusable(false);
checkBox1.setVisible(false);
window.add(checkBox1);
checkBox2 = new JCheckBox();
checkBox2.setFont(new Font("Arial", Font.BOLD, 14));
checkBox2.setForeground(Color.BLACK);
checkBox2.setSize(160, 30);
checkBox2.setText("79");
checkBox2.setLocation(300, 210);
checkBox2.setFocusable(false);
checkBox2.setVisible(false);
window.add(checkBox2);
checkBox3 = new JCheckBox();
checkBox3.setFont(new Font("Arial", Font.BOLD, 14));
checkBox3.setForeground(Color.BLACK);
checkBox3.setSize(160, 30);
checkBox3.setText("14");
checkBox3.setLocation(300, 240);
checkBox3.setFocusable(false);
checkBox3.setVisible(false);
window.add(checkBox3);
checkBox4 = new JCheckBox();
checkBox4.setFont(new Font("Arial", Font.BOLD, 14));
checkBox4.setForeground(Color.BLACK);
checkBox4.setSize(160, 30);
checkBox4.setText("87");
checkBox4.setLocation(300, 270);
checkBox4.setFocusable(false);
checkBox4.setVisible(false);
window.add(checkBox4);
question2 = new JLabel();
question2.setSize(550, 30);
question2.setLocation(160, 120);
question2.setFont(new Font("Arial", Font.BOLD, 16));
question2.setText("Select the prime number(s):");
question2.setOpaque(true);
question2.setBackground(Color.WHITE);
question2.setForeground(Color.BLACK);
question2.setVisible(false);
window.add(question2);
next2 = new JButton();
next2.setFont(new Font("Arial", Font.BOLD, 28));
next2.setForeground(Color.BLACK);
next2.setSize(160, 30);
next2.setText("EXT");
next2.setActionCommand("q3");
next2.setLocation(700, 500);
next2.setFocusable(false);
next2.setVisible(false);
next2.addActionListener(this);
window.add(next2);
//
//
//Question 3
//
//
listBox = new JList();
listBox.setFont(new Font("Arial", Font.BOLD, 14));
listBox.setForeground(Color.BLACK);
listBox.setSize(160, 30);
listBox.setLocation(300, 210);
listBox.setFocusable(false);
listBox.setVisible(false);
window.add(listBox);
question3 = new JLabel();
question3.setSize(550, 30);
question3.setLocation(160, 120);
question3.setFont(new Font("Arial", Font.BOLD, 16));
question3.setText("Of the people listed, who was not a US President:");
question3.setOpaque(true);
question3.setBackground(Color.WHITE);
question3.setForeground(Color.BLACK);
question3.setVisible(false);
window.add(question3);
finish = new JButton();
finish.setFont(new Font("Arial", Font.BOLD, 28));
finish.setForeground(Color.BLACK);
finish.setSize(160, 30);
finish.setText("FINISH");
finish.setActionCommand("end");
finish.setLocation(700, 500);
finish.setFocusable(false);
finish.setVisible(false);
finish.addActionListener(this);
window.add(finish);
//
//
//End
//
//
score = new JLabel();
score.setSize(550, 30);
score.setLocation(160, 120);
score.setFont(new Font("Arial", Font.BOLD, 16));
score.setText("your score is: " + correct + "/3");
score.setOpaque(true);
score.setBackground(Color.WHITE);
score.setForeground(Color.BLACK);
score.setVisible(false);
window.add(score);
//
//
//Extra
//
//
Menu = new JMenuBar();
startMenu = new JMenu("Start");
Menu.add(startMenu);
GoAction = new JMenuItem("Go");
GoAction.setActionCommand("q1");
GoAction.addActionListener(this);
startMenu.add(GoAction);
//exitMenuItem.addActionListener(this);
window.setVisible(true);
window.setJMenuBar(Menu);
//if (getActionCommand() == "BeginQuiz")
//System.out.println(Q1);
}
public void keyPressed(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("q1"))
{
start = true;
radButton1.setVisible(true);
radButton2.setVisible(true);
radButton3.setVisible(true);
radButton4.setVisible(true);
question1.setVisible(true);
next1.setVisible(true);
System.out.println("Q1");
}
if (e.getActionCommand().equals("q2"))
{
{
radButton1.setVisible(false);
radButton2.setVisible(false);
radButton3.setVisible(false);
radButton4.setVisible(false);
question1.setVisible(false);
next1.setVisible(false);
System.out.println("Q2");
checkBox1.setVisible(true);
checkBox2.setVisible(true);
checkBox3.setVisible(true);
checkBox4.setVisible(true);
question2.setVisible(true);
next2.setVisible(true);
}
if (e.getActionCommand().equals("q3"))
{
{
next2.setVisible(false);
checkBox1.setVisible(false);
checkBox2.setVisible(false);
checkBox3.setVisible(false);
checkBox4.setVisible(false);
question2.setVisible(false);
System.out.println("Q3");
question3.setVisible(true);
finish.setVisible(true);
}
if (e.getActionCommand().equals("end"))
{
{
question3.setVisible(false);
finish.setVisible(false);
score.setVisible(true);
finish.setVisible(true);
}
}
}
}
}
}
一如既往,谢谢你帮助我!
答案 0 :(得分:5)
你的if (action command equals q3)
if块被埋没在前一个if块中,因此当它处于真实状态时永远不会到达。
e.g。你有类似的东西:
if (e.getActionCommand().equals("q2"))
{
{ // this block is unnecessary
// bunch of stuff in here
}
// this block is buried within the if block above, and so will never be true
if (e.getActionCommand().equals("q3"))
{
{ // again this block is unnesseary
// more bunch of code
}
// again this block is buried within the previous two!
if (e.getActionCommand().equals("end"))
{
要解决当前问题,每个if块应该处于相同的块代码级别,而不是嵌套在前一个块中。
例如,一个简单的解决方法是改变它:
if (e.getActionCommand().equals("q2")) {
{
radButton1.setVisible(false);
radButton2.setVisible(false);
radButton3.setVisible(false);
radButton4.setVisible(false);
question1.setVisible(false);
next1.setVisible(false);
System.out.println("Q2");
checkBox1.setVisible(true);
checkBox2.setVisible(true);
checkBox3.setVisible(true);
checkBox4.setVisible(true);
question2.setVisible(true);
next2.setVisible(true);
}
if (e.getActionCommand().equals("q3")) {
{
next2.setVisible(false);
checkBox1.setVisible(false);
checkBox2.setVisible(false);
checkBox3.setVisible(false);
checkBox4.setVisible(false);
question2.setVisible(false);
System.out.println("Q3");
question3.setVisible(true);
finish.setVisible(true);
}
if (e.getActionCommand().equals("end")) {
{
question3.setVisible(false);
finish.setVisible(false);
score.setVisible(true);
finish.setVisible(true);
}
}
}
}
到此:
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("q1")) {
start = true;
radButton1.setVisible(true);
radButton2.setVisible(true);
radButton3.setVisible(true);
radButton4.setVisible(true);
question1.setVisible(true);
next1.setVisible(true);
System.out.println("Q1");
}
if (e.getActionCommand().equals("q2")) {
radButton1.setVisible(false);
radButton2.setVisible(false);
radButton3.setVisible(false);
radButton4.setVisible(false);
question1.setVisible(false);
next1.setVisible(false);
System.out.println("Q2");
checkBox1.setVisible(true);
checkBox2.setVisible(true);
checkBox3.setVisible(true);
checkBox4.setVisible(true);
question2.setVisible(true);
next2.setVisible(true);
}
if (e.getActionCommand().equals("q3")) {
next2.setVisible(false);
checkBox1.setVisible(false);
checkBox2.setVisible(false);
checkBox3.setVisible(false);
checkBox4.setVisible(false);
question2.setVisible(false);
System.out.println("Q3");
question3.setVisible(true);
finish.setVisible(true);
}
if (e.getActionCommand().equals("end")) {
question3.setVisible(false);
finish.setVisible(false);
score.setVisible(true);
finish.setVisible(true);
}
}
但更重要的是,您的代码非常重复,并且以不健康的方式将数据与代码混合在一起。我首先会集中精力创建一个符合OOP的Question类,并且只有在完成并测试之后,才能围绕这个类构建一个GUI。而不是交换组件,考虑交换组件显示的数据。这样可以更轻松地扩展和调试代码更多。
例如,我从这开始:
public class Question {
private String question;
private String correctAnswer;
private List<String> wrongAnswers = new ArrayList<>();
public Question(String question, String correctAnswer) {
this.question = question;
this.correctAnswer = correctAnswer;
}
public void addWrongAnswer(String wrongAnswer) {
wrongAnswers.add(wrongAnswer);
}
public boolean testAnswer(String possibleAnswer) {
return correctAnswer.equalsIgnoreCase(possibleAnswer);
}
public List<String> getAllRandomAnswers() {
List<String> allAnswers = new ArrayList<>(wrongAnswers);
allAnswers.add(correctAnswer);
Collections.shuffle(allAnswers);
return allAnswers;
}
public String getQuestion() {
return question;
}
public String getCorrectAnswer() {
return correctAnswer;
}
// toString, equals and hashCode need to be done too
}
然后我
ArrayList<Question>
,可以根据需要提出问题,这可以记录响应,正确与错误。