关于 Java 布局



我还在学习Java和我有简单的代码

import javax.swing.*;
Public class Test extends JFrame{
JLabel name = new JLabel("Name");
JTextArea ta = new JTextArea();
Test(){
    this.setSize(435, 400);
    this.setTitle("Test");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLayout(null);
    name.setBounds(10, 10, 50, 20);
    this.add(name);
    ta.setBounds(90, 10, 300, 200); 
    ta.setLineWrap(true);
    JScrollPane scroll = new JScrollPane(ta);
    this.add(scroll);
}
public static void main(String [] args){
    new Test().setVisible(true);
}
}

我应该选择什么布局? 我希望文本区域显示在名称标签旁边

如果它只是一个标签和一个文本区域,只需使用一个简单的GridLayout

Test(){
    setLayout(new GridLayout(1, 2));   // sets a Grid Layout with 1 row and 2 columns
    add(name);
    JScrollPane scroll = new JScrollPane(ta);
    ta.setLineWrap(true);
    add(scroll);

}
public static void main(String[] args) {
    JFrame frame = new Test();
    frame.setSize(435, 400);
    frame.setTitle("Test");
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setVisible(true);
}

最新更新