簡體   English   中英

Java輸入JTextfield

[英]java input JTextfield

我有個問題。 用戶輸入(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);
            }
        }
    }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM