2012-05-11 99 views
2

我已經爲項目嘲笑了一個快速的GUI,我可以看到標籤和文本字段,但出於某種原因,窗口不會重新調整大小。JFrame沒有調整大小以適合JPanel

這裏是基本的破敗:

創建JFrame和添加的JPanel:

JFrame frame1 = new JFrame("Hotel Reservation App"); 
    frame1.getContentPane().add(rViewPan, BorderLayout.CENTER); 
    frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

    frame1.pack(); 
    frame1.setVisible(true); 

這是rViewPan JPanel的代碼:

private void initComponents() { 

    setLayout(new BorderLayout()); 

    arrivalDateTF = new JTextField(); 


    departureDateTF = new JTextField(); 
    roomCategoryTF = new JTextField(); 
    roomQtyTF = new JTextField(); 

    JTextField[] textFields = { arrivalDateTF, departureDateTF, roomCategoryTF, roomQtyTF }; 
    JLabel[] textLabels = { new JLabel("1 : "), new JLabel("2 : "), 
      new JLabel("3 : "), new JLabel("4 : ") 
    }; 

    JPanel displayPan = new JPanel(); 
    GridBagLayout gridBagLay = new GridBagLayout(); 
    GridBagConstraints gridBagC = new GridBagConstraints(); 

    displayPan.setLayout(gridBagLay); 
    SwingUtilities.addTextElementsAsRows(textLabels, textFields, gridBagLay, displayPan); 

    gridBagC.gridwidth = GridBagConstraints.REMAINDER; 
    gridBagC.anchor = GridBagConstraints.EAST; 
    gridBagC.weightx = 1.0; 

    displayPan.add(new JLabel(" "), gridBagC); 

    submitB = new JButton("Soumettre"); 
    displayPan.add(submitB, gridBagC); 

    SwingUtilities.addStdBorder(displayPan, "Reservation"); 
    add(displayPan, BorderLayout.CENTER); 

} 

我基本上得到一個窗口大小適當,但字段水平隱藏。

+0

爲了更快得到更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

3

..窗口沒有按其包含的大小重新調整大小。

該變體是。

TestGui

如果你不能弄清楚如何使你的GUI的行爲,我建議你&張貼SSCCE。

import java.awt.*; 
import javax.swing.*; 

// TODO don't extend JPanel, just create an instance 
class TestGui extends JPanel { 

    JTextField arrivalDateTF; 
    JTextField departureDateTF; 
    JTextField roomCategoryTF; 
    JTextField roomQtyTF; 
    JButton submitB; 

    TestGui() { 
     initComponents(); 
    } 

    private void initComponents() { 
     setLayout(new BorderLayout()); 

     arrivalDateTF = new JTextField(6); 
     departureDateTF = new JTextField(6); 
     roomCategoryTF = new JTextField(8); 
     roomQtyTF = new JTextField(2); 

     JTextField[] textFields = { arrivalDateTF, departureDateTF, roomCategoryTF, roomQtyTF }; 
     JLabel[] textLabels = { new JLabel("1 : "), new JLabel("2 : "), 
      new JLabel("3 : "), new JLabel("4 : ") 
     }; 

     JPanel displayPan = new JPanel(); 
     GridBagLayout gridBagLay = new GridBagLayout(); 
     GridBagConstraints gridBagC = new GridBagConstraints(); 

     displayPan.setLayout(gridBagLay); 
//  SwingUtilities.addTextElementsAsRows(textLabels, textFields, gridBagLay, displayPan); 
     for (int ii=0; ii<textFields.length; ii++) { 
      displayPan.add(textLabels[ii]); 
      displayPan.add(textFields[ii]); 
     } 

     gridBagC.gridwidth = GridBagConstraints.REMAINDER; 
     gridBagC.anchor = GridBagConstraints.EAST; 
     gridBagC.weightx = 1.0; 

     displayPan.add(new JLabel(" "), gridBagC); 

     submitB = new JButton("Soumettre"); 
     displayPan.add(submitB, gridBagC); 

//  SwingUtilities.addStdBorder(displayPan, "Reservation"); 
     add(displayPan, BorderLayout.CENTER); 
    } 

    public static void main(String[] args){ 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       JFrame frame1 = new JFrame("Hotel Reservation App"); 
       frame1.getContentPane().add(new TestGui(), BorderLayout.CENTER); 
       frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

       frame1.pack(); 
       frame1.setVisible(true); 
      } 
     }); 
    } 
} 
+0

謝謝安德魯,我已經設法讓它處於正常工作狀態。出於某種原因,我有一個私人重定義的setPreferredSize版本,它沒有做任何事情,所以這個調用是毫無意義的。在向主BorderLayout的其他部分添加更多內容之後,它開始行爲正確。 –

+0

*「我有一個私人重定義版本的setPreferredSize」*如果您選擇發佈,它將會在SSCCE中。 ;) 很高興你把事情解決了。我現在可以提供的最好建議是,如果您認爲您需要撥打或覆蓋組件的首選大小,請先到SO尋求幫助。 :) –

+0

@Andrew Thompson你知道從哪裏成爲定製OS SwingUtilities2,意思是代碼行SwingUtilities.addTextElementsAsRows – mKorbel