0

The JFrame does not adapt to the size i've set. Anyone an idea why? I've used the Dimension Class to specify the size of the frame...

    public void createBaseFrame() {

    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        e.printStackTrace();
    }

    frame = new JFrame("Windows Shutdown Timer v" + CURRENT_VERSION);
    frame.setLayout(new GridLayout(3, 1, 5, 5));

    topPanel = new JPanel();
    centerPanel = new JPanel();
    bottomPanel = new JPanel();

    topPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
    centerPanel.setBorder(BorderFactory
            .createLineBorder(Color.DARK_GRAY, 1));
    bottomPanel.setBorder(BorderFactory
            .createLineBorder(Color.DARK_GRAY, 1));

    menuBar = new JMenuBar();

    datei = new JMenu("Datei");
    template = new JMenu("Vorlagen");
    about = new JMenu("Über");

    dateiClose = new JMenuItem("Programm beenden");
    templateSave = new JMenuItem("Vorlage speichern");
    templateExport = new JMenuItem("Vorlage exportieren");
    templateImport = new JMenuItem("Vorlage importieren");
    aboutInformation = new JMenuItem("Informationen");
    aboutVisitWebsite = new JMenuItem("Webseite besuchen");

    timerSetting = new JLabel("Zeit in Minuten:");
    possibleActions = new JLabel("Aktion wählen:");
    remainingTime = new JLabel(
            "Verbleibende Zeit bis zum herunterfahren: 10:23 Minuten");

    timeSpinner = new JSpinner();

    String[] possibleActionsData = { "herunterfahren", "abmelden",
            "neu starten", "sperren" };
    actionBox = new JComboBox<String>(possibleActionsData);

    timerButton = new JButton("Timer starten");
    timerButton.setSize(new Dimension(150, 40));

    datei.add(dateiClose);
    template.add(templateSave);
    template.add(templateImport);
    template.add(templateExport);
    about.add(aboutVisitWebsite);
    about.add(aboutInformation);

    menuBar.setSize(500, 5);
    menuBar.add(datei);
    menuBar.add(template);
    menuBar.add(about);

    topPanel.add(menuBar);

    centerPanel.setLayout(new GridLayout(2, 2, 0, 0));
    centerPanel.add(timerSetting);
    centerPanel.add(timeSpinner);
    centerPanel.add(possibleActions);
    centerPanel.add(actionBox);

    bottomPanel.setLayout(new GridLayout(1, 2, 0, 0));
    bottomPanel.add(remainingTime);
    bottomPanel.add(timerButton);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(500, 250));
    frame.setLocationRelativeTo(null);

    frame.add(topPanel);
    frame.add(centerPanel);
    frame.add(bottomPanel);

    frame.setResizable(false);
    frame.setVisible(true);

} // end createBaseFrame()

} // end class BaseFrame

The import satements and instance var's are not in the code example.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MKorsch
  • 3,822
  • 3
  • 16
  • 21
  • 1
    you have to call `frame.pack()` before you put it visible, and use `preferredSize` property instead of `size` – nachokk Jul 23 '13 at 14:44
  • 2
    A better idea is not to mess with the size *or* preferred size of components that naturally have one. See [this Q&A](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) for details. – Andrew Thompson Jul 23 '13 at 14:54
  • Thanks Andrew! Now i use the DesignGridLayout API whichs is way more comfortable than just the swing layout managers. – MKorsch Jul 24 '13 at 08:14

1 Answers1

0

Replace the setPreferredSize call by setSize.

It's ok for this component. For other swing components, try, as far as possible, not to use setSize, setPreferedSize, but use layouts instead. GridBagLayout is juste nice.

Cheers

David
  • 1,138
  • 5
  • 15
  • I've just changed the whole approach. Now i use the DesignGridLayout API whichs is way more comfortable than just the swing layout managers. – MKorsch Jul 24 '13 at 08:14