简体   繁体   中英

setText dosen´t work (in if) (Javax.swing)

Hello gays i am trying to write my first Programm with a gui graphic but i can´t find my mistake. When you start the aplication averything works but at thie and of the game the last text is not showing up. The if loop is exicutet (can you see with the system.out...) but the text.setText("is not working:/")

Here is my code i would be realy happy if anybody could help me with that.

import java.util.Scanner;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class main {
    static int randomNum = (int)(Math.random()* 101);
    static int count = 0;
    static JLabel text = new JLabel("try to guess the right number");
    static JTextField textfield = new JTextField();
    
public static void main(String[] args) {
    openUi();
    }
    public static void openUi() {
        
        JFrame frame = new JFrame("guess the right number");
        frame.setSize(400, 300);
        frame.setLocation(100, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JFrame.setDefaultLookAndFeelDecorated(true);
        

        text.setBounds(50, 50, 200, 30);
        textfield.setBounds(50, 100, 200 , 30);

        JButton button = new JButton("guess");
        button.setBounds(50, 150, 200, 30);
        
        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String textFtextfield = textfield.getText();
                    Integer guessNum = Integer.parseInt(textFtextfield);
                    Round(guessNum);
                } catch (Exception error) {
                    text.setText("a number!!!!!!! (0 betwee and 100)");
                }
                
            }

        });
        

        frame.add(text);
        frame.add(textfield);
        frame.add(button);
        frame.setLayout(null);
        frame.setVisible(true);
        

    }
public static void Round(Integer guessNum){
    if (randomNum == guessNum) {
        text.setText("richtig");
        
        System.out.println("esgeht nicht aber du hast gewonnen");
        
    } else {
        count++;
    }if (randomNum < guessNum) {
        text.setText("try again the number is below");
        
    } else {
        text.setText("try again the number is higher");
    }
    textfield.setText("");
    }




}

Your problem is in this code, a basic issue with boolean logic.

public static void Round(Integer guessNum){
    if (randomNum == guessNum) {
        text.setText("richtig");
        System.out.println("esgeht nicht aber du hast gewonnen");
    } else {
        count++;
    } if (randomNum < guessNum) {
        text.setText("try again the number is below");
    } else {
        text.setText("try again the number is higher");
    }
    textfield.setText("");
}

The last if/else will be called even if the guess is correct, and this is messing you up.

Better would be something along these lines where you nest the offending if/else within the first else:

public static void Round(Integer guessNum) {
    if (randomNum == guessNum) {
        text.setText("richtig");
    } else {

        // this code only gets called if the guess is wrong

        count++;
        if (randomNum < guessNum) {
            //....
        } else {
            // ....
        }
    }
}

Or even cleaner still, you could do:

public static void Round(Integer guessNum) {
    if (randomNum < guessNum) {
        // ....
    } else if (randomNum < guessNum) {
        // ...
    } else {
        text.setText("richtig");
    }
}

More importantly, to solve similar problems in the future, you will want learn to do mental walk-throughs of your code, asking yourself "does this code make sense?" and "what is this code currently doing?" as you walk through it -- also known as " Rubber Duck Debugging ".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM