0
我試圖讓我的標籤和組合框靠得更近,並將我的textarea放置在框架的底部。我的組合框和標籤彼此相鄰,但距離錯誤,我不能看到我的文本區域。然後,當我調整框架大小時,所有內容都顯示出來了,而我的combobox和textarea大小恰到好處,只是間距不對。我認爲這是一個重量問題,但是當我調整重量數字時,我沒有任何改變。謝謝您的幫助!GribBaglayout無法正確顯示
下面是代碼:
//add menu items
menu1.add(save);
menu1.add(close);
menu.add(menu1);
menu.add(menu2);
//Creates tabbed items
tab.addTab("Scan", null, tab1, "Start a scan!");
tab.addTab("Timed Scan", null, tab2, "Start a timed Scan");
//add GUI items
panel.setLayout(new BorderLayout());
panel.add(menu, BorderLayout.NORTH);
panel.add(tab);
JButton IPscan = new JButton("IP");
JTextArea response = new JTextArea();
Dimension textSize = new Dimension(400,100);
Dimension comboSize = new Dimension(100,25);
response.setPreferredSize(textSize);
//Tab1
GridBagLayout gridBagLayout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
// c.weightx = .1;
c.weighty = .5;
tab1.setLayout(gridBagLayout);
response.setLineWrap(true);
response.setWrapStyleWord(true);
response.setText("Scan results:");
c.gridx = 0;
c.gridy = 4;
tab1.add(response, c);
c.anchor = GridBagConstraints.NORTHWEST;
c.gridx = 0;
c.gridy = 0;
tab1.add(choseScan, c);
scanOp.setPreferredSize(comboSize);
c.gridx = 1;
tab1.add(scanOp,c);
//tab2
tab2.setLayout(gridBagLayout);
c.gridx = 1;
c.gridy = 1;
tab2.add(scan1, c);
frame.add(panel);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
Dimension d = new Dimension(500,500);
frame.setPreferredSize(d);
frame.pack();
//shows everything
frame.setVisible(true);
return 5;
}
您可能會因嘗試設置尺寸和首選尺寸而傷害自己。而是讓組件和佈局爲你完成所有這些工作,否則你可能會限制你的GUI的大小以至於不能顯示(這可能是你的問題)。還要考慮創建併發佈一個[最小,完整和可驗證的示例程序](http://stackoverflow.com/help/mcve),您可以將代碼壓縮到仍然編譯和運行的最小位,並且沒有外部依賴關係,沒有額外的代碼與您的問題無關,但仍然顯示您的問題。 – 2014-08-31 19:17:53