Java WindowBuilder如何使用不同的按钮显示计数器
我有一个使用eclipse在Windowbuilder中创建的程序。我希望程序生成一个随机数,用户必须猜测它是偶数还是奇数。我已经基于命令行创建了这个游戏,但是我正在尝试使用GUI来完成它。我的一个问题是,当我想结束游戏,让用户看到他们的退出按钮的得分。然而,当我试图显示他们正确和不正确的最终分数时,我做不到,因为这是一种不同的方法。因此,我希望在单击时使用一个单独的按钮来显示完全正确和错误。代码如下:Java WindowBuilder如何使用不同的按钮显示计数器,java,eclipse,windowbuilder,swingbuilder,Java,Eclipse,Windowbuilder,Swingbuilder,我有一个使用eclipse在Windowbuilder中创建的程序。我希望程序生成一个随机数,用户必须猜测它是偶数还是奇数。我已经基于命令行创建了这个游戏,但是我正在尝试使用GUI来完成它。我的一个问题是,当我想结束游戏,让用户看到他们的退出按钮的得分。然而,当我试图显示他们正确和不正确的最终分数时,我做不到,因为这是一种不同的方法。因此,我希望在单击时使用一个单独的按钮来显示完全正确和错误。代码如下: import java.awt.EventQueue; import javax.swi
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
public class RandomNumber {
private JFrame frame;
private JTextField textFieldMe;
private JTextField textFieldCpu;
private JTextField textFieldScore;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RandomNumber window = new RandomNumber();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public RandomNumber() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textFieldMe = new JTextField();
textFieldMe.setBounds(152, 62, 86, 20);
frame.getContentPane().add(textFieldMe);
textFieldMe.setColumns(10);
textFieldCpu = new JTextField();
textFieldCpu.setBounds(152, 118, 86, 20);
frame.getContentPane().add(textFieldCpu);
textFieldCpu.setColumns(10);
textFieldScore = new JTextField();
textFieldScore.setBounds(152, 217, 86, 20);
frame.getContentPane().add(textFieldScore);
textFieldScore.setColumns(10);
JButton btnGo = new JButton("GO!");/* this is the start button*/
btnGo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Random rand = new Random();
int correctCount = 0;
int incorrectCount = 0;
int ran = rand.nextInt(50) + 1;
String me = textFieldMe.getText();
if (ran % 2 == 0 && me.equals("e") || ran % 2 != 0 && me.equals("o")){
correctCount++;
}else if (ran % 2 == 0 && me.equals("o") || ran % 2 != 0 && me.equals("e")){
incorrectCount++;
}
textFieldCpu.setText(Integer.toString(ran));
int totalRight = correctCount;
int totalWrong = incorrectCount;
String right = Integer.toString(totalRight);
String wrong = Integer.toString(totalWrong);
}
});
btnGo.setBounds(149, 149, 89, 23);
frame.getContentPane().add(btnGo);
JButton btnQuit = new JButton("Quit"); /* i want this button to display the score*/
btnQuit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String right = Integer.toString(totalRight);
String wrong = Integer.toString(totalWrong);
textFieldScore.setText("Correct & wrong: " + right + wrong );
}
});
btnQuit.setBounds(321, 216, 89, 23);
frame.getContentPane().add(btnQuit);
JLabel lblNewLabel = new JLabel("Your Number");
lblNewLabel.setBounds(167, 36, 135, 14);
frame.getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("CPU Guess");
lblNewLabel_1.setBounds(162, 93, 71, 14);
frame.getContentPane().add(lblNewLabel_1);
JLabel lblScore = new JLabel("Score");
lblScore.setBounds(179, 192, 46, 14);
frame.getContentPane().add(lblScore);
}
}
通过将
正确
和错误
转换为类中的变量,我会尝试使每个方法都可见正确
和错误
。新的类看起来像
public class RandomNumber {
...
private int right, wrong;
...
private void initialize() {
...
btnGo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Random rand = new Random();
int ran = rand.nextInt(50) + 1;
String me = textFieldMe.getText();
if (ran % 2 == 0 && me.equals("e") || ran % 2 != 0 && me.equals("o")){
++right;
}else if (ran % 2 == 0 && me.equals("o") || ran % 2 != 0 && me.equals("e")){
++wrong;
}
textFieldCpu.setText("" + ran);
}
}
...
btnQuit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textFieldScore.setText("Correct : " + right + " Wrong: " wrong );
//will automatically convert ints to String
}
}
...
}
...
}
通过将
正确
和错误
转换为类中的变量,我会尝试使每个方法都可见正确
和错误
。新的类看起来像
public class RandomNumber {
...
private int right, wrong;
...
private void initialize() {
...
btnGo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Random rand = new Random();
int ran = rand.nextInt(50) + 1;
String me = textFieldMe.getText();
if (ran % 2 == 0 && me.equals("e") || ran % 2 != 0 && me.equals("o")){
++right;
}else if (ran % 2 == 0 && me.equals("o") || ran % 2 != 0 && me.equals("e")){
++wrong;
}
textFieldCpu.setText("" + ran);
}
}
...
btnQuit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textFieldScore.setText("Correct : " + right + " Wrong: " wrong );
//will automatically convert ints to String
}
}
...
}
...
}
尝试选择描述性名称,而不是像lblNewLabel这样的名称。尝试选择描述性名称,而不是像lblNewLabel这样的名称。谢谢,这很有效,但现在只算一次。我猜是因为它只循环一次。现在我正在考虑一个do-while循环。但是,我如何将退出按钮连接到while?何时使用while?现在每次点击它都会重复,不管我怎么想。我在错误的地点犯了正确的错误。所以它一直在重新设置自己。谢谢好的,谢谢,这很有效,但现在只算一次了。我猜是因为它只循环一次。现在我正在考虑一个do-while循环。但是,我如何将退出按钮连接到while?何时使用while?现在每次点击它都会重复,不管我怎么想。我在错误的地点犯了正确的错误。所以它一直在重新设置自己。谢谢