簡體   English   中英

Java-如何使用框式布局設置組件的高度和寬度

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM