[英]Java - How to set the height and width of the component using box layout
I am creating the applet using the BoxLayout. 我正在使用BoxLayout创建applet。 In this layout i have 3 components(ie, 2 text areas and one button).
在这个布局中,我有3个组件(即2个文本区域和一个按钮)。 I want to set the height and width of the button.Please can anybody help me.
我想设置按钮的高度和宽度。任何人都可以帮助我。
code 码
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);
}
}
Do not add the JButton
directly. 不要直接添加
JButton
。 Instead, add it to a JPanel
, and then add that JPanel
to the applet's content pane. 而是将其添加到
JPanel
,然后将该JPanel
添加到applet的内容窗格中。 The reason for this is the layout manager of the applet's content pane is causing the components to take up as much space as possible. 原因是applet内容窗格的布局管理器导致组件占用尽可能多的空间。 By adding the button to the panel first, and then adding the panel to the applet's content pane, the panel will be resized and the button will keep it's preferred size.
通过先将按钮添加到面板上,然后将面板添加到小程序的内容窗格中,将调整面板的大小,并且按钮将保持其首选大小。
EDIT - 编辑 -
I just noticed that you're using AWT components. 我只是注意到您正在使用AWT组件。 Therefore, here are the component translations:
因此,这里是组件翻译:
JButton
= Button
JButton
= Button
JPanel
= Panel
JPanel
= Panel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.