Guess My Number [Java]

Posts 14 of 4 · Page 1 of 1
Guess My Number [Java]
It's poorly done, I created this a few months ago.

Main.java
Code:
package com.gui;

import javax.swing.JFrame;
/**
 *
 * @Author Ignorance
 */
public class Main {

    public static void main(String[] args) {
        
        GUI Jframes = new GUI();
        Jframes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Jframes.setSize(225, 100);
        Jframes.setVisible(true);
    }
}
GUI.java
Code:
package com.gui;

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

/**
 *
 * @Author Ignorance
 */
public class GUI extends JFrame {

    //Varribles
    private JLabel Label1;
    private JLabel Label2;
    private JButton Button1;
    private JButton Button2;
    private JButton Button3;
    private JTextField TextField1;
    int MyNumber = 15;

    public GUI() {
        super("Guess my Number");
        setLayout(new FlowLayout());

        Label1 = new JLabel("Guess My Number");
        Label1.setToolTipText("Just Guess it..");
        Label2 = new JLabel("");
        Button1 = new JButton("Enter");
        Button1.setToolTipText("Press when ready.");
        Button2 = new JButton("Clear");
        Button2.setToolTipText("Clear Text the Text Box.");
        Button3 = new JButton("Close");
        Button3.setToolTipText("Close the application.");
        TextField1 = new JTextField("(Guess)");
        add(Label1);
        add(TextField1);
        add(Button1);
        add(Button2);
        Button2.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button2ActionPerformed(evt);
            }
        });
        add(Button3);
        add(Label2);
        Button1.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button1ActionPerformed(evt);
            }
        });
        Button3.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button3ActionPerformed(evt);
            }
        });

    }

    private void Button2ActionPerformed(java.awt.event.ActionEvent evt) {
        TextField1.setText("");
        Label1.setText("");
    }

    private void Button3ActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }

    private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {
        int YourGuess = Integer.parseInt(this.TextField1.getText());
        if (YourGuess < MyNumber) {
            Label2.setText("Too Low!");
        } else if (YourGuess > MyNumber) {
            Label2.setText("Too High!");
        } else if (YourGuess == MyNumber) {
            Label2.setText("Correct!");
        }
    }
}
Is this a release, or are you asking for tips?
Nice piece of code, but what the hell is your purpose????
Need help? tips? fix? or just showing/sharing. You have to let people know why you put this code up.
Posts 14 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?