![](/img/trans.png)
[英]java swing application - how to set the height (and width) of a component in a panel
[英]Java - How to set the height and width of the component using box layout
我正在使用BoxLayout创建applet。 在这个布局中,我有3个组件(即2个文本区域和一个按钮)。 我想设置按钮的高度和宽度。任何人都可以帮助我。
码
public class parsetextdata extends Applet
{
TextArea ta1,ta2;
Button parse;
public void init()
{
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
ta1 = new TextArea();
add(ta1);
parse = new Button();
parse.setLabel("parse");
parse.setBackground(Color.DARK_GRAY);
parse.setForeground(Color.WHITE);
add(parse);
ta2 = new TextArea();
ta2.setEditable(false);
ta2.setBackground(Color.GRAY);
ta2.setForeground(Color.WHITE);
add(ta2);
}
}
不要直接添加JButton
。 而是将其添加到JPanel
,然后将该JPanel
添加到applet的内容窗格中。 原因是applet内容窗格的布局管理器导致组件占用尽可能多的空间。 通过先将按钮添加到面板上,然后将面板添加到小程序的内容窗格中,将调整面板的大小,并且按钮将保持其首选大小。
编辑 -
我只是注意到您正在使用AWT组件。 因此,这里是组件翻译:
JButton
= Button
JPanel
= Panel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.