有两个按钮
left = new JButton("prev");
right = new JButton("next");
我像这样将它们添加到jframe
mainframe.add(left,BorderLayout.WEST);
mainframe.add(right,BorderLayout.EAST);
但是有高度与主机的高度相同。如何设置自己的宽度和高度?
以及如何设置他们的位置(不仅在北,西,东,南)?
如何设置自己的宽度和高度
不要那样做,而只是使用NORTH
or本身添加到外部(父)布局的orSOUTH
来限制大小。JPanel
EAST
WEST
很像这样:
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();
}
});
}
}