【发布时间】:2018-09-29 11:55:21
【问题描述】:
我正在编写一个软件来测试用户的音符,但我会省略音乐细节。由于我的程序很大,我编写了一个单独的程序作为概念验证并解决我的问题。
简单的程序应该是这样工作的:在 main 方法中,随机选择一个数字(1 或 2)。该数字设置为 GUI 中 jlabel 中的文本。然后用户按下两个数字按钮之一。然后用户必须按另一个 j 按钮来确认他们的选择。主要方法比较问题和答案变量。如果数字相等,则 jlabel 中的文本变为“正确”。否则它会变为“不正确”。然后用户必须按下第四个 j 按钮才能获得下一个问题。然后重复该方法,用户应该能够根据需要进行尽可能多的练习。
包含我的主要方法的我的测试类
package test;
import java.util.Random;
public class Test {
protected static int question;
protected static int answer;
protected static boolean next = false;
protected static boolean sure = false;
protected static boolean exit = false;
protected static Random random = new Random();
public static void main(String[] args) {
GUI.gUI();
while(!exit){//while
question = random.nextInt(2) + 1;
GUI.lblNewLabel.setText("" + question);
while(!next){//next
while(!sure) {//sure
answer = Other.other();
}//sure
sure = false;
if(question == answer) {
GUI.lblNewLabel.setText("correct");
}
else {
GUI.lblNewLabel.setText("incorrect");
}
}//next
next = false;
}//exit
}
}
我的图形界面
package test;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class GUI extends JFrame {
private static final long serialVersionUID = 1L;
protected static int answerGUI;
protected static JLabel lblNewLabel = new JLabel("New label");
private JPanel contentPane;
/**
* Launch the application.
*/
public static void gUI() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
GUI frame = new GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
BTNListener btnlistener = new BTNListener();
lblNewLabel.setBounds(39, 25, 46, 14);
contentPane.add(lblNewLabel);
JButton btn1 = new JButton("No. 1");
btn1.setBounds(39, 158, 89, 23);
contentPane.add(btn1);
btn1.addActionListener(btnlistener);
JButton btn2 = new JButton("No. 2");
btn2.setBounds(157, 157, 89, 23);
contentPane.add(btn2);
btn2.addActionListener(btnlistener);
JButton btnAreYouSure = new JButton("Are you sure?");
btnAreYouSure.setBounds(38, 197, 99, 23);
contentPane.add(btnAreYouSure);
btnAreYouSure.addActionListener(btnlistener);
JButton btnNext = new JButton("Next");
btnNext.setBounds(159, 197, 89, 23);
contentPane.add(btnNext);
btnNext.addActionListener(btnlistener);
JButton btnExit = new JButton("Exit");
btnExit.setBounds(269, 198, 89, 23);
contentPane.add(btnExit);
btnExit.addActionListener(btnlistener);
}
protected class BTNListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand() == "No. 1") {
Other.answer = 1;
}
else if(e.getActionCommand() == "No. 2") {
Other.answer = 2;
}
if(e.getActionCommand() == "Are you sure?") {
Test.sure = true;
}
if(e.getActionCommand() == "Next") {
Test.next = true;
}
if(e.getActionCommand() == "Exit") {
Test.exit = true;
}
}
}
}
我的其他班级
package test;
public class Other {
protected static int answer;
protected static int other() {
return answer;
}
}
我决定我需要另一个类,所以 main 方法有另一个方法可以作为序列的一部分调用,尽管我认为这个方法可以写在 GUI 中。
我包含确认按钮的一个原因是因为在第一次传递之后,Other 类中的 answer 变量已经有了一个值,所以在第二次传递时,这个值会立即传递给 Test 中的 answer 变量,并且用户无法更改该值。
由于现在编写程序,jlabel 显示问题值,并且按下按钮似乎没有任何作用。我尝试使用 do...while 循环,它在无限循环之前工作了一次并且不允许用户回答。
【问题讨论】:
-
欢迎来到 SO。 “不起作用”太宽泛了。请准确描述你得到什么与你期望什么。此外,代码似乎太长而无法证明问题。请参考:minimal reproducible example
-
当您寻求帮助时,请做出必要的努力。否则你浪费你的时间,也浪费我们的时间。 MCVE 不仅与 M 有关,而且与 C 有关。发布的代码甚至无法编译。
-
我已经去掉了伪代码,恢复了原始代码。
标签: java swing jframe awt jbutton