組件我試圖讓GridBagLayout的兩個板爲40%和框架的60%,同時能夠讓他們的內部組件,它是被麻煩。的Java的GridBagLayout裏面
當我不要放置在面板裏面的按鈕,它的工作原理就像我想它。
不太清楚我在做什麼錯了,我已經試過移動按鈕,創建於GridBagLayout的面板,其中的創作,但它仍然沒有奏效。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test{
public void display(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900,650);
frame.setVisible(true);
JPanel test = new JPanel();
test.setLayout(new GridBagLayout());
GridBagConstraints c= new GridBagConstraints();
JPanel left= new JPanel();
JPanel right= new JPanel();
c.fill = GridBagConstraints.VERTICAL - GridBagConstraints.HORIZONTAL;
c.weightx = 0.4;
c.gridx = 1;
c.weighty = 1;
test.add(left,c);
c.weightx = .6;
c.gridx = 2;
test.add(right,c);
JButton button= new JButton("A button");
left.add(button,c);//If I do not add this, then it shows how I want it to be
frame.add(test);
}
}
什麼是'createVector'? – 2012-01-15 01:34:48
不好意思,把它改名爲button,來自別的東西。 – user1062898 2012-01-15 01:37:05
當我運行它時,看起來像兩個40/60%分割的面板,有或沒有按鈕。這到底是什麼問題? – Ash 2012-01-15 02:08:40