gpt4 book ai didi

java 输入 JTextfield

转载 作者:行者123 更新时间:2023-12-02 10:31:21 25 4
gpt4 key购买 nike

我有一个问题。用户输入(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");
}
}
}

}

最佳答案

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);
}
}
}
}
}

关于java 输入 JTextfield,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53593396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com