Hi all, I'm taking an introductory class into programming and often I can't get help and don't have a lot of time to check around for answers, so I'm turning to this awesome place. I hope to post code issues and code requests here and get help. I am quite new to Java to please be basic, for now. If it help, II understand TI basic so you can explain it in that sense.
No issues now.
Code:
No issues now.
Code:
/**
* Program displays a GUI window with the number 5 preselected. Users imput #, the programs responds, high, low, or exact depending on the guess.
*
* @Josh Pham
* @1.0
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Guess_the_Number implements ActionListener
{
JPanel Main;
JFrame Frame;
Container Container;
JTextField Guess;
String GuessString;
JButton Run;
JLabel Instructions,Hint;
int Answer,GuessInt;
public Guess_the_Number()
{
Frame = new JFrame("Guess the Number");
Frame.setBounds(200,300,300,200);
Container = Frame.getContentPane();
Main = new JPanel();
Instructions = new JLabel("Enter a # between 1-10");
Hint = new JLabel("");
Run = new JButton("This is my guess");
Run.addActionListener(this);
Answer = 5; //This is not the Answer, don't try it!!
Guess = new JTextField(3);
Container.add(Main);
Main.add(Instructions);
Main.add(Guess);
Main.add(Run);
Main.add(Hint);
Frame.show();
}
public void actionPerformed(ActionEvent event)
{
if((event.getSource() == Run))
{
GuessString = Guess.getText();
GuessInt = Integer.parseInt(GuessString);
if(GuessInt == Answer)
{
Hint.setText("Cheater. You know the answer!");
Hint.setForeground(new Color(0,120,0));
}
if(GuessInt > Answer)
{
Hint.setText(" Failure; Too high! ");
Hint.setForeground(new Color(120,0,0));
}
if(GuessInt < Answer)
{
Hint.setText(" Failure; Too low! ");
Hint.setForeground(new Color(0,0,120));
}
}
}
}