我有问题。用户输入(inputUser)必须与随机数进行比较。有点像赌博程序。 但是我不知道如何比较输入的用户字符串和随机数。然后将其比较,输出将显示在对话框中。 用户的输入带有字符串,随机数带有整数。我已经尝试将int转换为字符串。但由于某种原因,它不起作用。
package gamble;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import java.util.Random;
import java.util.Scanner;
public class Gamble extends JFrame {
public JLabel inputUser;
public JPanel panel;
Font myFont = new Font("Serif", Font.BOLD, 25);
Font rulesFont = new Font("Serif", Font.BOLD, 15);
public static void main(String[] args) {
Gamble GUI = new Gamble();
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.setSize(600, 600);
GUI.setResizable(false);
GUI.setVisible(true);
GUI.setLocationRelativeTo(null);
}
public Gamble(){
super("NUMERO");
JPanel panel = new JPanel();
panel.setLayout(null);
add(panel);
//nieuwe label
JLabel label = new JLabel("Raad het getal");
label.setLayout(null);
label.setBounds(250,10, 300, 30);
label.setFont(myFont);
panel.add(label);
//nieuwe label
JLabel rules = new JLabel("Gok een nummer tot en met 5");
rules.setBounds(225,40,300,30);
rules.setFont(rulesFont);
panel.add(rules);
//nieuw textfield
JTextField inputUser = new JTextField(100);
inputUser.setBounds(275,100,100,30);
inputUser.setFont(rulesFont);
inputUser.setBackground(Color.LIGHT_GRAY );
panel.add(inputUser);
thehandler handler = new thehandler();
inputUser.addActionListener(handler);
}
private class thehandler implements ActionListener {
public void actionPerformed(ActionEvent event){
Random rand = new Random(); //random number
int n = rand.nextInt(5) + 1; //random number wordt gemaakt
int j = Integer.parseInt(inputUser.getText());
if (event.getSource()== inputUser){
if(n == j){
JOptionPane.showMessageDialog(null, "test");
}
}
else {
JOptionPane.showMessageDialog(null, "dfd");
}
}
}
}
答案 0 :(得分:1)
inputUser的声明存在一些问题,只需将其全局声明更改为JTextField
并删除其本地声明即可。
代码应如下所示:
class Gamble extends JFrame {
public JTextField inputUser;
public JPanel panel;
Font myFont = new Font("Serif", Font.BOLD, 25);
Font rulesFont = new Font("Serif", Font.BOLD, 15);
public static void main(String[] args) {
Gamble GUI = new Gamble();
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.setSize(600, 600);
GUI.setResizable(false);
GUI.setVisible(true);
GUI.setLocationRelativeTo(null);
}
public Gamble(){
super("NUMERO");
JPanel panel = new JPanel();
panel.setLayout(null);
add(panel);
//nieuwe label
JLabel label = new JLabel("Raad het getal");
label.setLayout(null);
label.setBounds(250,10, 300, 30);
label.setFont(myFont);
panel.add(label);
//nieuwe label
JLabel rules = new JLabel("Gok een nummer tot en met 5");
rules.setBounds(225,40,300,30);
rules.setFont(rulesFont);
panel.add(rules);
//nieuw textfield
inputUser = new JTextField(100);
inputUser.setBounds(275,100,100,30);
inputUser.setFont(rulesFont);
inputUser.setBackground(Color.LIGHT_GRAY );
panel.add(inputUser);
thehandler handler = new thehandler();
inputUser.addActionListener(handler);
}
private class thehandler implements ActionListener {
public void actionPerformed(ActionEvent event){
Random rand = new Random(); //random number
int n = rand.nextInt(5) + 1; //random number wordt gemaakt
int j = Integer.parseInt(inputUser.getText());
if (event.getSource()== inputUser){
if(n == j){
JOptionPane.showMessageDialog(null, "Yep, you're right");
}
else {
JOptionPane.showMessageDialog(null, "Nope nope nope, the number is " + n);
}
}
}
}
}