更改标签和文本框的位置

时间:2011-04-04 14:35:37

标签: java eclipse swing user-interface

现在标签位于文本框的顶部。我希望能够设置文本框和标签的位置,以便标签位于文本框的左侧。

        this.container = this.getContentPane(); 
        this.container.setLayout(new FlowLayout()); 

        this.searchText = new JLabel("Enter text to be searched: "); 

        this.charText = new JLabel("Enter a character: "); 

        this.target = new JTextArea(3,30); 

2 个答案:

答案 0 :(得分:1)

将JPanel与FlowLayout一起使用。

JPanel myPanel = new JPanel();

myPanel.add(searchText);
myPanel.add(target);

container.add(myPanel,FlowLayout.LEFT);

请注意,如果您希望以您想要的方式显示两个标签和两个文本区域,则应创建两个面板。有关FlowLayout的更多信息:

Flow Layout

在任何情况下,您都应该使用编辑器进行更高级的定位。

答案 1 :(得分:1)

您可以像这样使用GridLayout

    this.container = new JPanel(new GridLayout(1, 2) );     

    this.searchText = new JLabel("Enter text to be searched: "); 

    this.charText = new JLabel("Enter a character: "); 

    this.target = new JTextArea(3,30); 

    container.add(searchText);
    container.add(target);
相关问题