Results 1 to 5 of 5

Thread: guess game GUI

  1. #1
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default guess game GUI

    I'm sure you guys have seen this a thousand times but I cannot find the answer anywhere. I am doing the guess game / high low game for a class and we need to be able to have the user enter a number and based on their number, changed the background color of the JFrame to red or blue depending on if they are high or low. I think I can figure out how to change the color but I cannot find how to take the users inputted text from the text field and make the program compare the number with the random number by hitting the "ok" button.

    If that didn't make any sense, I want the values of the text field to be passed when the "ok" button is hit. Here is the code I have so far:
    Java Code:
    import java.text.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class OfficeAreaCalculator extends JFrame{
    	
    	private JFrame mainFrame;
    	private JButton CalculateButton;
    	private JButton exitButton;
    	private JTextField lengthField;
    	private JTextField widthField;
    	private JTextField areaField;
    	private JLabel lengthLabel;
    	private JLabel widthLabel;
    	private JLabel areaLabel;
    	
    	public OfficeAreaCalculator(){
    		mainFrame = new JFrame("Office Area Calculator");
    		
    		CalculateButton = new JButton("Calculate area");
    		exitButton = new JButton("Exit");
    		lengthLabel = new JLabel("Enter the lenth of the office: ");
    		widthLabel = new JLabel("Enter the width of the office: ");
    		areaLabel = new JLabel("Office area: ");
    		lengthField = new JTextField(5);
    		widthField = new JTextField(5);
    		areaField = new JTextField(5);
    		
    		Container c = mainFrame.getContentPane();
    		c.setLayout(new FlowLayout());
    		
    		c.add(lengthLabel);
    		c.add(lengthLabel);
    		c.add(widthLabel);
    		c.add(widthLabel);
    		c.add(areaLabel);
    		c.add(areaField);
    		c.add(CalculateButton);
    		c.add(exitButton);
    		
    		CalculateButton.setMnemonic('C');
    		exitButton.setMnemonic('x');
    		
    		mainFrame.setSize(250,150);
    		
    		mainFrame.addWindowListener(new WindowAdapter(){
    			public void windowClosing(WindowEvent e){System.exit(0);}
    				});
    		
    		calculateButtonHandler chandler = new calculateButtonHandler();
    		CalculateButton.addActionListener(ehandler);
    		
    		ExitButtonHandler ehandler = new ExitButtonHandler();
    		exitButton.addActionListener(ehandler);
    		
    		FocusHandler fhandler = new FocusHandler();
    		lengthField.addFocusListener(fhandler);
    		widthField.addFocusListener(fhandler);
    		areaField.addFocusListener(fhandler);
    		
    		mainFrame.show();
    	}
    		
    class calculateButtonHandler implements ActionListener{
    	
    	public void actionPerformed(ActionEvent e){
    		DecimalFormat num = new DecimalFormat(",###.##");
    		double width, length, area;
    		String instring;
    		
    		instring = lengthField.getText();
    		if (instring.equals("")){
    			instring = "0";
    			lengthField.setText("0");
    		}
    		length = Double.parseDouble(instring);
    		
    		instring = widthField.getText();
    		if (instring.equals("")){
    			instring = "0";
    			widthField.setText("0");
    		}
    		
    		width = Double.parseDouble(instring);
    		area = length * width;
    		areaField.setText(num.format(area));
    		
    	}
    	
    class ExitButtonHandler implements ActionListener{
    	
    	public void actionPerformed(ActionEvent e){
    		System.exit(0);
    	}
    }
    
    class FocusHandler implements FocusListener{
    	
    	
    	public void focusGained(FocusEvent e){
    		if(e.getSource() == lengthField || e.getSource() == widthField){
    			areaField.setText("");
    		}
    		else if (e.getSource() == areaField){
    			areaField.setNextFocusableComponent(CalculateButton);
    			CalculateButton.grabFocus();
    		}
    	}
    
    	
    	public void focusLost(FocusEvent e){
    		if(e.getSource() == widthField){
    			widthField.setNextFocusableComponent(CalculateButton);
    		}
    	}
    }
    
    public static void main(String args[]){
    	
    	new OfficeAreaCalculator();
    }
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    What has the posted code got to do with a guessing game? Are you expecting us to modify it for you?

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can extract information from a textfield with this line
    Java Code:
    textField.getText();
    Then you want to handle the events when ok is clicked with an action handler. Something like this:
    Java Code:
    JButton button = new JButton("x");
    button.addActionListener(new ActionListener(){
      System.out.println("X button pressed");
    });
    This uses an anonymous inner class and prints "X button pressed" when the button is clicked. These two pieces of advice should be more then enough.

  4. #4
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    Thank you Sunde. I've been looking everywhere and could not find the answer. I will start working on it in the morning and I'm sure this will help.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Make sure you have access to the tutorials whenever you are doing projects that use new features. Here is a link to the entire index of tutorials

    The Really Big Index

    Also, if you are working with a text field, googling "java 6 JTextField", will bring you to the API which will list all the methods a class has and a quick description of them. You can do this with any java class for lots of resources.

Similar Threads

  1. Need help programming a game like "Guess Who"
    By Mr.VicSage in forum New To Java
    Replies: 2
    Last Post: 12-07-2010, 10:17 AM
  2. Guess Character
    By pinkdreammsss in forum Java Applets
    Replies: 14
    Last Post: 05-05-2010, 07:28 PM
  3. Replies: 3
    Last Post: 12-20-2009, 03:22 PM
  4. guess number game
    By mistah in forum New To Java
    Replies: 10
    Last Post: 11-23-2008, 03:37 AM
  5. sample of guess high and low game
    By pouria62 in forum AWT / Swing
    Replies: 1
    Last Post: 10-26-2008, 12:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •