设置按钮的大小和位置 [英] set size and position for button
本文介绍了设置按钮的大小和位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有两个按钮
left = new JButton("prev");
right = new JButton("next");
我像这样将它们添加到 jframe
I add them to jframe like this
mainframe.add(left,BorderLayout.WEST);
mainframe.add(right,BorderLayout.EAST);
但是高度与主机的高度相同.如何设置自己的宽度和高度?
But there have got height the same as mainframe's height. How to set my own width and height?
以及如何设置他们的位置(不仅在北、西、东、南)?.
And how to set their position(not only in the NORTH,WEST,EAST,SOUTH)?.
推荐答案
如何设置自己的宽度和高度
How to set my own width and height
不要这样做,而只是使用 NORTH
或 SOUTH
的 JPanel
来限制大小,该 JPanel
本身被添加到 <外部(父)布局的 code>EAST 或 WEST
.
Don't do that, instead just constrain the size using the NORTH
or SOUTH
of a JPanel
that itself is added to the EAST
or WEST
of the outer (parent) layout.
很像这样:
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();
}
});
}
}
这篇关于设置按钮的大小和位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文