2015-11-19 23 views
-1

我正在使用Swing編程一個簡單的Java計算器,並且我遇到了GridBagLayout問題。用GridBagLayout顯示問題

下面是我已經切我的部件:

my window

enter image description here

因此,大家可以看到,COS善壇%日誌開方SQR和PI按鈕不應該是遠從其他人那裏。

此外,這是當我減少了窗口的大小時,程序啓動會發生什麼:

enter image description here

的按鈕顯示我希望他們能夠被顯示出來,而是顯示在JTable中的方法古怪。

以下是關於主窗口的一些信息:

首選尺寸:600 * 600 的JTable:無大小信息設置好的

和每個組件都有自己的權重屬性中的GridBagConstraints設置好的爲0的對象(我認爲這可能來自這個

編輯:一些代碼,這是我用來初始化我的按鈕枚舉,所以你可以看到的GridBagConstraints

//new GridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, 
//int anchor, int fill, Insets insets, int ipadx, int ipady); 
ZERO("0", 96, new GridBagConstraints(5,5,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     ONE("1", 97, new GridBagConstraints(4,4,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     TWO("2", 98, new GridBagConstraints(5,4,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     THREE("3", 99, new GridBagConstraints(6,4,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     FOUR("4", 100, new GridBagConstraints(4,3,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     FIVE("5", 101, new GridBagConstraints(5,3,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     SIX("6", 102, new GridBagConstraints(6,3,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     SEVEN("7", 103, new GridBagConstraints(4,2,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     EIGHT("8", 104, new GridBagConstraints(5,2,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     NINE("9", 105, new GridBagConstraints(6,2,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     DOT(".", 110, new GridBagConstraints(6,5,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     PLUS("+", 107, new GridBagConstraints(7,2,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     MINUS("-", 109, new GridBagConstraints(7,3,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     DIVIDE("/", 111, new GridBagConstraints(7,5,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     MULTIPLY("*", 106, new GridBagConstraints(7,4,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     MODULO("%", 0, new GridBagConstraints(7,6,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     EQUAL("=", 10, new GridBagConstraints(4,5,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     CLEAR("C", 67, new GridBagConstraints(7,1,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     BACKSPACE("Bck", 8, new GridBagConstraints(7,0,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     CLEARHISTORY("Clear history", 72, new GridBagConstraints(0,7,4,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     COSINUS("cos", 72, new GridBagConstraints(4,6,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     SINUS("sin", 72, new GridBagConstraints(5,6,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     TANGENT("tan", 72, new GridBagConstraints(6,6,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     SQUAREROOT("sqrt", 72, new GridBagConstraints(5,7,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     SQUARE("sqr", 72, new GridBagConstraints(6,7,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     LOGARITHM("log", 72, new GridBagConstraints(4,7,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), // 
     PLUSMINUS("±", 72, new GridBagConstraints(7,7,1,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)); 

,這是代碼,我初始化查看:

 CALCULATIONSHISTORYLABEL(new GridBagConstraints(0,0,4,1,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), 
     CALCULATIONSHISTORYTABLE(new GridBagConstraints(0,1,4,6,0,0,GridBagConstraints.CENTER, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), 
     CURRENTCALCULATIONLABEL(new GridBagConstraints(4,0,3,1,0,0,GridBagConstraints.LINE_END, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), 
     CURRENTRESULTLABEL(new GridBagConstraints(4,1,3,1,0,0,GridBagConstraints.LINE_END, 
       GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)); 

/*...*/ 

    public View() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setTitle("simpleCount"); 
     setPreferredSize(new Dimension(800, 800)); 
     setLayout(new GridBagLayout()); 
     calculationsHistoryView = new JTable(); 

     GridBagConstraints c = new GridBagConstraints(); 
     add(new JLabel("Calculations History"), ComponentPlacement.CALCULATIONSHISTORYLABEL.gbc); 
     add(new JScrollPane(calculationsHistoryView), ComponentPlacement.CALCULATIONSHISTORYTABLE.gbc); 

     add(currentCalculationView, ComponentPlacement.CURRENTCALCULATIONLABEL.gbc); 
     add(currentResultView, ComponentPlacement.CURRENTRESULTLABEL.gbc); 
     for (int i = 0; i < Button.values().length; i++) { 
      add(buttonsView.getButtons()[i], Button.values()[i].gbc); 
     } 
     pack(); 
     setVisible(true); 
     requestFocus(); 
    } 

誰能幫助我?

在此先感謝您的回答。

+1

爲了更快得到更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 –

+1

是否將所有組件的高度和寬度設置爲零?假設你試圖平均分配一切。 – JungJoo

回答

-1

我想可能有兩個問題:

  1. 儘量伸展寬度和滾動表的高度爲0.1至1.0(大於零),並設置拉伸策略設置爲GridBagLayout.BOTH
  2. 設置minPrefferedHeight()minPrefferedWidth()JScrollPane這是表容器。
+0

請參閱[我是否應避免使用Java Swing中的set(Preferred | Maximum | Minimum)Size方法?](http://stackoverflow.com/q/7229226/418556)(是)。剩下的其餘部分似乎更適合發表評論。 –

+0

選擇是你的。一些swing組件有'奇怪'的行爲,對於這些情況,java api提供了很多方法,我們不得不用它們來憎恨它們;) –

+0

您錯過了該主題的主要觀點。答案中提到,在極少數情況下,您需要更改這些值,根據需要,通常更好的設計是**'@ Override' **相關方法。調用'set..Size'方法時,我們通常只是隨機猜測所需的數字。 –

3

不要在整個框架中使用單個佈局管理器。

默認的佈局管理器是框架的BorderLayout,我會堅持這一點。

然後你就可以使用創建「按鈕面板」與您的GridBagLayout並添加面板:

frame.add(buttonPanel, BorderLayout.LINE_END); 

然後,您可以創建另一個「左面板」,這又可能會使用一個BorderLayout的。然後你可以

  1. 將頂部標籤添加到BorderLayout。PAGE_START
  2. 表添加到BorderLayout.CENTER
  3. 添加底部標籤BorderLayout.PAGE_END

終於在左側面板加入到框架:

frame.add(leftPanel, BorderLayout.CENTER); 

現在所有的額外空間在框架將去桌子。