Results 1 to 4 of 4
  1. #1
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default Format of box changes after first action

    I have finally got the program functioning, but can not seem to figure out how to get the buttons in my box to stay on the same line after the first action is taken. Also how can I get the guess box to show clear after each guess?

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    
    
    public class JFrameGuess extends JFrame implements ActionListener
    {
    	final int LENGTH = 600;
    	final int HEIGHT = 200;
    	private int newAnswer = 0, oldAnswer;
    	private static int count = 1;
    	private Container gameBoard = getContentPane();
    	JLabel gameInfo = new JLabel("I am thinking of a number between 1 and 1000." +
    											"  Can you guess my number?");
    	JTextField guess = new JTextField(4);
    	JButton startGame = new JButton("Start Game");
    	JButton exit = new JButton("Exit");
    	Random ranNum = new Random();
    	final int LIMIT = 1000;
    	private int comNumber = (ranNum.nextInt(LIMIT) + 1);
    	
    	public JFrameGuess()
    		{
    			super("Program #5 - Number Guessing Game");
    			setSize(LENGTH, HEIGHT);
    			setVisible(true);
    			gameBoard.setLayout(new FlowLayout());
    			gameBoard.setBackground(Color.CYAN);
    			gameBoard.add(gameInfo);
    			gameBoard.add(guess);
    			gameBoard.add(startGame);
    			gameBoard.add(exit);
    			gameInfo.setOpaque(true);
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			startGame.addActionListener(this);
    			exit.addActionListener(this);
    		}
    	
    	public void actionPerformed(ActionEvent e)
    		{
    			Object source = e.getSource();
    			
    			if(source == startGame)
    				{
    					oldAnswer = newAnswer;
    					newAnswer = Integer.parseInt(guess.getText());
    					compareValues();
    					repaint();
    				}
    				
    			else
    				System.exit(0);			
    		}
    	
    	public void compareValues()
    		{
    		
    				if(newAnswer < comNumber)
    					if(count > 1 && oldAnswer < comNumber)
    						displayColderLessThan();
    					else
    						displayWarmerLessThan();
    				
    				else if(newAnswer > comNumber)
    					if(count > 1 && oldAnswer > comNumber)
    						displayColderGreaterThan();
    					else
    						displayWarmerGreaterThan();
    				
    				else 
    					displayWinner(comNumber);			
    		}
    	
    	public void displayWarmerLessThan()
    		{
    				gameBoard.setVisible(true);
    				gameBoard.setBackground(Color.RED);
    				gameInfo.setText(newAnswer + " is TOO LOW!  But you are getting WARMER." +
    													"  Enter guess number " + (count + 1) + ".");
    				count++;
    									
    		}
    	public void displayWarmerGreaterThan()
    		{
    				gameBoard.setVisible(true);
    				gameBoard.setBackground(Color.RED);				
    				gameInfo.setText(newAnswer + " is TOO HIGH!  But you are getting WARMER." +
    													"  Enter guess number " + (count + 1) + ".");
    				count++;
    		}
    	
    	public void displayColderLessThan()
    		{
    				gameBoard.setVisible(true);
    				gameBoard.setBackground(Color.BLUE);				
    				gameInfo.setText(newAnswer + " is TOO LOW!  But you are getting COLDER." +
    													"  Enter guess number " + (count + 1) + ".");
    				
    				count++;
    			
    		}
    				
    	public void displayColderGreaterThan()
    		{
    				gameBoard.setVisible(true);
    				gameBoard.setBackground(Color.BLUE);
    				gameInfo.setText(newAnswer + " is TOO HIGH!  Ane you are getting COLDER." + 
    													"  Enter guess number " + (count + 1) + ".");
    				count++;
    		}
    		
    	public void displayWinner(int num)
    		{
    			gameBoard.setVisible(true);
    			gameBoard.setBackground(Color.YELLOW);
    			
    			gameInfo.setText("Congratulations " + num + " is the right number.  " +
    									"It took you " + (count + 1) + " attempts to guess my number.");
    
    			
    		}
    
    	
    	public static void main(String[] args)
    		{
    					
    			JFrameGuess GuessingGame = new JFrameGuess();
    			
    		}
    }

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

    Default

    Use an appropriate layout manager.

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Use the JTextField setText() method to clear the box. Simply pass it an empty String . FlowLayout is most likely causing your button problems. Have a look at implementing other more flexible layouts, here is one:

    How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Put the JLabel and JTextField in one JPanel and the JButtons in another JPanel. Both JPanels use FlowLayout by default. Then add the two JPanels to a third JPanel that uses GridLayout(0, 1), a grid layout that has one column and a variable number of rows, and then add this to the JFrame's contentPane.

    edit: I'm advising you to nest JPanels with simple layouts as I suggest above. Someone else is recommending GridBagLayout, and I strongly advise you to avoid this one for now as there are a lot of pitfalls when using it, especially if you are new to using layouts.

Similar Threads

  1. Replies: 3
    Last Post: 08-19-2010, 08:27 AM
  2. Action(s) is looking for testers
    By app.jbbres.com in forum Reviews / Advertising
    Replies: 0
    Last Post: 06-26-2010, 11:51 PM
  3. how to convert one format to another format
    By mahipal_reddy621 in forum New To Java
    Replies: 1
    Last Post: 12-02-2008, 10:21 AM
  4. jsp:forward action
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-24-2007, 10:04 AM
  5. jsp:param action
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-24-2007, 10:03 AM

Posting Permissions

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