0

I did what's in the following Q&As but none worked for me.

Simply want to size the JFrame to a set size but can't seem to figure out how. Read some of the docs but I'm still missing something and hope someone here can help me. Thank you.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;


public class CAUnit8Ch18 extends JFrame{

  private JTextField txtfield; 
  private JLabel label;   

  public CAUnit8Ch18() {

        setTitle("Color Change Frame"); 
        //setSize(900,500); this does not seem to work
        setPreferredSize(new Dimension(500,300));
        pack();
        setVisible(true); 
        setDefaultCloseOperation(EXIT_ON_CLOSE); 
        createContents(); 
  } 


  private void createContents() {

    JPanel jp = new JPanel();
    label = new JLabel("What is your name:");
    txtfield = new JTextField(20);

    jp.setOpaque(false); 
    Container contentPane = getContentPane();
    Random rand = new Random(); 
    int n = rand.nextInt(4);


    switch(n){
    case 0:
        contentPane.setBackground(Color.RED);
        label.setForeground(Color.WHITE);
        break;
    case 1:
        contentPane.setBackground(Color.WHITE);
        break;
    case 2:
        contentPane.setBackground(Color.YELLOW);
        break;
    case 3:
        contentPane.setBackground(Color.GREEN);
        label.setForeground(Color.BLUE);
        break;
    case 4:
        contentPane.setBackground(Color.BLUE);
        label.setForeground(Color.WHITE);
        break;
    }

    jp.add(label); 
    jp.add(txtfield); 

    txtfield.addActionListener(new Listener());
    add(jp); 


  } 


  private class Listener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      String message = "Thanks for playing, " + txtfield.getText();

      txtfield.setVisible(false); 
      label.setText(message); 
    } 
  } 

  public static void main(String[] args) {

    JOptionPane.showMessageDialog(null,
            "The following window color will be randomly chosen from\nRed, White, Yellow, Green, Blue");

    new CAUnit7Ch17();
  } 

}
Community
  • 1
  • 1
martinbshp
  • 1,073
  • 4
  • 22
  • 36
  • Those article numbers tell us nothing. – user1803551 Dec 10 '15 at 11:36
  • *"Simply want to size the JFrame to a set size"* Given the frame 'chrome' will be different sizes on different Look and Feels, that means the size of the content pane must change to suit the fixed size frame! It is usually a better approach to specify a **preferred size for the content,** use layout padding and borders for white space, then `pack()` the frame and don't worry about the exact size it is! – Andrew Thompson Dec 10 '15 at 12:16
  • 1
    BTW - do you realize that the `main(..)` method of this class is not making an instance of `CAUnit8Ch18` class? Fix that first.. ;) – Andrew Thompson Dec 10 '15 at 12:20

3 Answers3

1

Well, you are using both frame.setSize() and frame.pack(). You should use one of them at one time.

Using setSize() you can give the size of frame you want but if you use pack(), it will automatically change the size of the frames according to the size of components in it. It will not consider the size you have mentioned earlier.

Try removing frame.pack() from your code or putting it before setting size and then run it.

Nina
  • 99
  • 12
  • Move pack() before setSize() but don't remove it! A call to pack() (frames) or validate() (applet) is necessary for correct layout out of the GUI. Or better still, add some contents to the frame so that the layout managers can work out how big it should be when packed (and remove any call to setting the size, preferred size, maximum or (possibly even) minimum size) – Nina Dec 10 '15 at 10:50
  • 1
    Or setResizable(false); That will prevent pack() and the user from resizing your JFrame. – ArcticLord Dec 10 '15 at 10:54
  • This is wrong. Look again at the given code and try your solution yourself. – user1803551 Dec 10 '15 at 11:39
1

Call setSize and not setPreferredSize and pack - this is a top-level container. Better yet, use a proper layout manager for the contents of the frame and then just call pack which will calculate the size for you.

Also, setVisible should be called only after adding the components.

user1803551
  • 12,965
  • 5
  • 47
  • 74
0

Thank you everyone for your responses. I resolved the issue by putting the setVisible(true) only after adding all the content of the frame. I was setting setVisible and then adding the panel and buttons when what I should do is add everything first and then setVisible to true.

martinbshp
  • 1,073
  • 4
  • 22
  • 36
  • That is what I told you in my answer. You should accept my answer (green checkmark) instead of posting your own which says the same. – user1803551 Dec 10 '15 at 14:07