Results 1 to 4 of 4
- 02-21-2011, 02:58 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 22
- Rep Power
- 0
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(); } }
- 02-21-2011, 03:06 AM #2
Use an appropriate layout manager.
- 02-21-2011, 03:08 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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)
-
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
-
HTTP Status 404 - There is no Action mapped for action name showmainframe...
By vaibhavspawar in forum Advanced JavaReplies: 3Last Post: 08-19-2010, 08:27 AM -
Action(s) is looking for testers
By app.jbbres.com in forum Reviews / AdvertisingReplies: 0Last Post: 06-26-2010, 11:51 PM -
how to convert one format to another format
By mahipal_reddy621 in forum New To JavaReplies: 1Last Post: 12-02-2008, 10:21 AM -
jsp:forward action
By Java Tip in forum Java TipReplies: 0Last Post: 12-24-2007, 10:04 AM -
jsp:param action
By Java Tip in forum Java TipReplies: 0Last Post: 12-24-2007, 10:03 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks