0

JFrame 不适应我设置的大小。有人知道为什么吗?我已经使用维度类来指定框架的大小......

    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

导入语句和实例变量不在代码示例中。

4

1 回答 1

0

将 setPreferredSize 调用替换为 setSize。

这个组件没问题。对于其他的swing组件,尽量不使用setSize、setPreferedSize,而是使用layout。GridBagLayout 很好。

干杯

于 2013-07-23T18:08:51.030 回答