Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-19-2007, 11:19 PM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
Help needed with sizing components
I have completed most of the application I created however I cannot get the GUI components to show up correctly. I want to have the text in JLabel on the top centered in the form. The 2nd component is a JTextField which will only need to be big enough to hold numbers between 1 and 100. It currently takes up the whole width of the form. I have tried a number of things to resize it without success. The 3rd component is a button panel which actually appearing correctly.

Code:
import javax.swing.*; import java.awt.*; public class GuessNumber { public static void main(String[] args) { Guess guess = new Guess(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(guess.getContent()); f.setSize(500, 150); f.setVisible(true); f.setLocation(275, 275); } }
Code:
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.*; import java.awt.*; public class Guess implements ActionListener{ JButton okButton; JButton playAgainButton; JButton exitButton; JTextField answerTextField; JLabel msgLabel; int randomNum; int currentMessageState = 1; int guessCount = 1; public JPanel getContent() { RandomNumber random = new RandomNumber(); randomNum = random.getNumber(); System.out.println(randomNum); String message = "Please enter what you think the random number is below."; // creates JLabel that asks user to guess what the random // number is and enter it below msgLabel = new JLabel(message, JLabel.CENTER); msgLabel.setSize(400, 150); msgLabel.setFont(new Font("Times New Roman", Font.BOLD, 14)); answerTextField = new JTextField(); Dimension d = answerTextField.getPreferredSize(); d.width = 75; answerTextField.setPreferredSize(d); answerTextField.setFont(new Font("Times New Roman", Font.PLAIN, 14)); answerTextField.requestFocusInWindow(); okButton = new JButton("Ok"); okButton.setSize(30, 30); okButton.setFont(new Font("Times New Roman", Font.PLAIN, 14)); okButton.addActionListener(this); playAgainButton = new JButton("Play Again"); playAgainButton.setFont(new Font("Times New Roman", Font.PLAIN, 14)); playAgainButton.addActionListener(this); exitButton = new JButton("Exit"); exitButton.setFont(new Font("Times New Roman", Font.PLAIN, 14)); exitButton.addActionListener(this); JPanel buttonPanel = new JPanel(); buttonPanel.add(okButton); buttonPanel.add(playAgainButton); buttonPanel.add(exitButton); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS)); panel.add(msgLabel); panel.add(answerTextField); panel.add(buttonPanel); return panel; } public void actionPerformed(ActionEvent e){ if(e.getSource() == okButton){ String userGuess = answerTextField.getText(); int guess = Integer.parseInt(userGuess); answerTextField.setEditable(false); if(guess == randomNum){ JOptionPane.showMessageDialog(null, "You guessed correctly.\n" + "Tries needed to guess correctly: " + guessCount + "\n"); guessCount++; answerTextField.setEditable(false); } if(guess < randomNum){ JOptionPane.showMessageDialog(null, "The guess was too low, guess higher."); guessCount++; answerTextField.setEditable(true); answerTextField.setText(""); answerTextField.requestFocusInWindow(); } if(guess > randomNum){ JOptionPane.showMessageDialog(null, "The guess was too high, guess lower."); guessCount++; answerTextField.setEditable(true); answerTextField.setText(""); answerTextField.requestFocusInWindow(); } } if(e.getSource() == playAgainButton){ answerTextField.setEditable(true); answerTextField.setText(""); answerTextField.requestFocusInWindow(); guessCount = 1; Guess newgame = new Guess(); newgame.getContent(); } if(e.getSource() == exitButton){ System.exit(0); } } }
Code:
import java.util.Random; public class RandomNumber { int randomNum; public RandomNumber() { } public int getNumber() { setNumber(); return randomNum; } private void setNumber(){ int max = 100; Random random = new Random(); randomNum = random.nextInt(max +1); } }
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-20-2007, 12:23 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
BoxLayout opens you up to alignment problems when using different types of components.
And to sizing issues: often you have to set some or all of the preferredSize, minimumSize and maximumSizes to get the desired display.
Let's see what this does:
Code:
JPanel panel = new JPanel(); panel.setLayout(//new BoxLayout(panel, BoxLayout.PAGE_AXIS)); new BorderLayout()); panel.add(msgLabel, BorderLayout.NORTH); JPanel center = new JPanel(new GridBagLayout()); center.add(answerTextField, new GridBagConstraints()); panel.add(center, BorderLayout.CENTER); panel.add(buttonPanel, BorderLayout.SOUTH); return panel;
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-20-2007, 06:40 AM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
Thanks Hardwired. Layouts are still giving me some trouble.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
GUI components not display Eranga AWT / Swing 2 03-12-2008 04:16 AM
Trouble with Buffer Sizing Jeff New To Java 3 02-07-2008 03:43 PM
HTML on Swing Components Java Tip Java Tips 0 11-27-2007 11:51 AM
Java Components are not displayed(sometimes) archanajathan AWT / Swing 3 11-05-2007 10:34 AM
Gui Components Marty New To Java 1 05-28-2007 06:04 AM


All times are GMT +3. The time now is 03:13 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org