JFrame不适应定义的大小

时间:2013-07-23 14:40:37

标签: java swing size jframe

JFrame不适应我设置的大小。任何人都知道为什么?我已经使用Dimension Class来指定框架的大小......

    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

导入声明和实例变量不在代码示例中。

1 个答案:

答案 0 :(得分:0)

用setSize替换setPreferredSize调用。

这个组件没问题。对于其他swing组件,尽可能尝试不使用setSize,setPreferedSize,而是使用布局。 GridBagLayout非常好。

干杯