WindowBuilder 如何使用不同的按钮来显示一个计数器
WindowBuilder how to use a different button to display a counter
我有一个程序,我正在使用 Eclipse 在 Windowbuilder 中创建。我希望程序生成一个随机数,用户必须猜测它是偶数还是奇数。我已经基于命令行创建了游戏,但我想做的是使用 GUI 来完成。我遇到的一个问题是当我想结束游戏并让用户使用退出按钮查看他们的分数时。但是,当我尝试显示他们正确和错误次数的最终分数时,我不能,因为它采用不同的方法。所以我想要一个单独的按钮在点击时显示总的是非。这是代码:
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);
}
}
我会尝试通过将 right
和 wrong
转换为 class 中的变量,使每个方法都可见。新的 class 看起来像
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
}
}
...
}
...
}
我有一个程序,我正在使用 Eclipse 在 Windowbuilder 中创建。我希望程序生成一个随机数,用户必须猜测它是偶数还是奇数。我已经基于命令行创建了游戏,但我想做的是使用 GUI 来完成。我遇到的一个问题是当我想结束游戏并让用户使用退出按钮查看他们的分数时。但是,当我尝试显示他们正确和错误次数的最终分数时,我不能,因为它采用不同的方法。所以我想要一个单独的按钮在点击时显示总的是非。这是代码:
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);
}
}
我会尝试通过将 right
和 wrong
转换为 class 中的变量,使每个方法都可见。新的 class 看起来像
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
}
}
...
}
...
}