Rosenkreuz
Rosenkreuz

Reputation: 13

Need help to create java GUI

I'm trying to make a GUI for following code but am unsure how to call that class.

I'm just trying to make a simple GUI design to throw.

Also can anyone tell how to stop "Tebak Angka Antara 1 dan 1000" from appearing every time I guessed the number?

the code:

public class TebakAngka {

    public static void AcakAngka (String [] args) {
        Random rand = new Random();
        int AngkaAcak = rand.nextInt(1000);
        int Tebakan = 0;
        Scanner input = new Scanner (System.in);
        int tebakan;
        boolean win = false;

        while (win == false) {

        System.out.println("Tebak Angka Antara 1 dan 1000");
        tebakan = input.nextInt();
        Tebakan++;

        if (tebakan == AngkaAcak) {
            win = true;
        }
        else if (tebakan < AngkaAcak - 50) {
            System.out.println("Lebih Tinggi!");
        }
        else if (tebakan > AngkaAcak + 50){
            System.out.println("Lebih Rendah!");
        }
        else System.out.println("Sedikit lagi");
        }   

        System.out.println("Kamu Menang");
        System.out.println("Angkanya Adalah" + AngkaAcak);
        System.out.println("TebakanKe" + Tebakan);
    }
}

possible GUI ?

public class GUI {
private static JTextField textField;
private static JTextField textField_1;

public static void main(String args[]){
JFrame myFrame = new JFrame("Tebak Angka");
myFrame.setSize(400,300);
myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
myFrame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(157, 156, 90, 20);
myFrame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("Submit");
btnNewButton.setBounds(157, 187, 89, 23);
myFrame.getContentPane().add(btnNewButton);
JTextArea txtrTextArea = new JTextArea();
txtrTextArea.setText("Input History and clues goes here");
txtrTextArea.setBounds(73, 25, 264, 101);
myFrame.getContentPane().add(txtrTextArea);
JLabel lblNumOfTries = new JLabel("Num of Tries");
lblNumOfTries.setBounds(10, 207, 60, 14);
myFrame.getContentPane().add(lblNumOfTries);
textField_1 = new JTextField();
textField_1.setBounds(80, 204, 22, 20);
myFrame.getContentPane().add(textField_1);
textField_1.setColumns(10);
myFrame.setVisible(true);
}
}

Upvotes: 0

Views: 85

Answers (1)

ChiefTwoPencils
ChiefTwoPencils

Reputation: 13940

The first part of your question is pretty broad because the class (code) you provided isn't meant to be handled by GUI components like labels, buttons, and input dialogues. It will be difficult to answer it in any reasonable way, IMHO.

As far as the last part...

Also can anyone tell how to stop "Tebak Angka Antara 1 dan 1000" from appearing every time I guessed the number?

Yes, simply move System.out.println("Tebak Angka Antara 1 dan 1000"); outisde the while loop you have just above it.

Instead of...

...
while (win == false) {
    System.out.println("Tebak Angka Antara 1 dan 1000");
    ...

you should...

...
System.out.println("Tebak Angka Antara 1 dan 1000");
while (win == false) { 
...

A little nit-pick would be while(win == false). It's a bit redundant because win is already a boolean and evaluates directly. while(!win), if win = false; initially is a bit cleaner.

Upvotes: 2

Related Questions