V. Govorov V. Govorov - 5 months ago 22
Java Question

Java- - Resize packed JFRame

When I use

frame.pack();
, then I see very small window:

enter image description here

Resize after pack make frame ugly.

How can I get big and packed frame?

Code:

public class Main {

//first tab
static JTextArea nameArea = new JTextArea("Nickname:");
static JTextArea ageArea = new JTextArea("Age:");
static JTextArea heightArea = new JTextArea("Height:");
static JTextArea weightArea = new JTextArea("Weight:");

static JTextField nameField = new JTextField();
static JTextField ageField = new JTextField();
static JTextField heightField = new JTextField();
static JTextField weightField = new JTextField();

public static void main(String[] args) {
JFrame frame = new JFrame("Character Builder");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(512, 384));
frame.setVisible(true);

JTabbedPane pane = new JTabbedPane();
frame.add(pane);

JPanel panel1 = new JPanel();
GridLayout layout = new GridLayout(4, 2);
layout.minimumLayoutSize(panel1);
panel1.setLayout(layout);
pane.addTab("Profile", panel1);

JButton button = new JButton("test");
panel1.add(nameArea);
panel1.add(nameField);
panel1.add(ageArea);
panel1.add(ageField);
panel1.add(heightArea);
panel1.add(heightField);
panel1.add(weightArea);
panel1.add(weightField);

frame.pack();
}


}

Answer

Change the preferred size of your components.

http://docs.oracle.com/javase/8/docs/api/java/awt/Window.html#pack--

EDIT: Okay, pack() only goes for the "out farest" component, your tab pane. The GridLayout on the other hand orientates itself on the minimum width and height, in this case obviously the width of Nickname: and the height of the text fields, which you can change, too. (EDIT2: After trying it out I found that still preferredSized is used, for whatever reason) See also:

http://docs.oracle.com/javase/8/docs/api/java/awt/GridLayout.html#minimumLayoutSize-java.awt.Container-

There is also the possibility to change the gaps between your components, if you want that.

EDIT2: This is all I can offer you.

    JPanel panel1 = new JPanel();
    GridLayout layout = new GridLayout(4, 2);
//    layout.minimumLayoutSize(panel1);
    layout.setHgap(1);
    layout.setVgap(10);
    panel1.setLayout(layout);
    pane.addTab("Profile", panel1);

    Dimension dmtfs = nameField.getPreferredSize(); // default preferred text field size
    nameField.setPreferredSize(new Dimension(dmtfs.width, dmtfs.height-10)); // too small, but will be caught by height of text area 
    ageField.setPreferredSize(new Dimension(dmtfs.width, dmtfs.height-10));
    heightField.setPreferredSize(new Dimension(dmtfs.width, dmtfs.height-10));
    weightField.setPreferredSize(new Dimension(dmtfs.width, dmtfs.height-10));
    Dimension dmtas = nameArea.getPreferredSize(); // default preferred text area size
    nameArea.setPreferredSize(new Dimension(dmtas.width+10, dmtas.height));
    // changing one is enough, because GridLayout goes for the biggest

The grid layout will try to make each component the same size and in the case of JTextField that means filling in all available space.

Comments