2013-07-23 29 views
0

JFrame不適應我設置的大小。任何人都知道爲什麼?我已使用Dimension Class來指定框架的大小...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 

導入區和實例var不在代碼示例中。

+1

你必須調用'frame.pack()'你把前可見,並使用'preferredSize'屬性,而不是'size' – nachokk

+2

一個更好的想法是不要惹大小*或*自然有一個組件的首選大小。有關詳細信息,請參閱[本問答](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi)。 –

+0

謝謝安德魯!現在我使用DesignGridLayout API,這比起擺動佈局管理器來說更舒適。 – MKorsch

回答

0

用setSize替換setPreferredSize調用。

這個組件沒關係。對於其他的swing組件,儘量不要使用setSize,setPreferedSize,而是使用佈局。 GridBagLayout是非常好的。

乾杯

+0

我剛剛改變了整個方法。現在我使用DesignGridLayout API,這比起擺動佈局管理器來說更舒適。 – MKorsch