Results 1 to 4 of 4

Thread: Need help resizing form components

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    25

    Need help resizing form 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

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Need help resizing form components

    Try using the FlowLayout. It did it for me
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    25

    Re: Need help resizing form components

    Thanks I will give it a try

  4. #4
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: Need help resizing form components

    not sure whether you have solved this but you could also try the SpringLayout, it also does the job quite nicely imo.
    Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered
    If someone helped you today then please consider rating their post.

Posting Permissions

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



Click Here to Expand Forum to Full Width