Reputation: 11
I have a problem. The user input(inputUser) must be compared with the random number. Its kinda like a gamble program. But I dont know how can I compare the input user string and the random number. And after that it is compared the output is in a dialog. The input of the user comes with a string and the random number comes with a int. I already tried to convert the int to the string. but for some reason it doesnt work.
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");
}
}
}
}
Upvotes: 0
Views: 37
Reputation: 121
There're some problems with the declaration of inputUser, just change the global declaration of it to JTextField
and remove the local declaration of it.
The code should looks like below:
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);
}
}
}
}
}
Upvotes: 1