Results 1 to 3 of 3
  1. #1
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default 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.

    Java 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);
          }
    }
    Java 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);
            }
        }
        
    }
    Java 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

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    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:
    Java 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;

  3. #3
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    Thanks Hardwired. Layouts are still giving me some trouble.

Similar Threads

  1. GUI components not display
    By Eranga in forum AWT / Swing
    Replies: 2
    Last Post: 03-12-2008, 03:16 AM
  2. Trouble with Buffer Sizing
    By Jeff in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 02:43 PM
  3. HTML on Swing Components
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-27-2007, 10:51 AM
  4. Java Components are not displayed(sometimes)
    By archanajathan in forum AWT / Swing
    Replies: 3
    Last Post: 11-05-2007, 09:34 AM
  5. Gui Components
    By Marty in forum New To Java
    Replies: 1
    Last Post: 05-28-2007, 04:04 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
  •