如何設置我自己的寬度和高度
不要做,而不是僅僅使用自身添加到的的EAST
或WEST
一個JPanel
的NORTH
或SOUTH
限制大小外(父)佈局。
就像這樣:
import java.awt.*;
import javax.swing.*;
class BorderGUI {
BorderGUI() {
JPanel gui = new JPanel(new BorderLayout(2,2));
JPanel westConstrain = new JPanel(new BorderLayout(2,2));
// LINE_START will be WEST for l-r languages, otherwise EAST
gui.add(westConstrain, BorderLayout.LINE_START);
JPanel westControls = new JPanel(new GridLayout(0,1,2,2));
for (int ii=1; ii<3; ii++) {
westControls.add(new JButton("" + ii));
}
westConstrain.add(westControls, BorderLayout.PAGE_START);
JPanel eastConstrain = new JPanel(new BorderLayout(2,2));
gui.add(eastConstrain, BorderLayout.LINE_END);
JPanel eastControls = new JPanel(new GridLayout(0,1,2,2));
for (int ii=1; ii<4; ii++) {
eastControls.add(new JButton("" + ii));
}
// show at the bottom
eastConstrain.add(eastControls, BorderLayout.PAGE_END);
gui.add(new JScrollPane(new JTextArea(6,10)), BorderLayout.CENTER);
JOptionPane.showMessageDialog(null, gui);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new BorderGUI();
}
});
}
}
請參閱[這](http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html)。 – mre 2012-04-08 18:01:27