面板 JPanel,滚动面板 JScrollPane,文本域JTextArea

时间:2023-03-10 00:39:56
面板 JPanel,滚动面板 JScrollPane,文本域JTextArea

容器中可以有多个JPanel面板,一个JPanel面板中可以有多个控件。

滚动面板 JScrollPane中只能有一个控件。

面板 JPanel,滚动面板 JScrollPane,文本域JTextArea     面板 JPanel,滚动面板 JScrollPane,文本域JTextArea

public class Demo extends JFrame {
public Demo() {
setBounds(100, 100, 600, 200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new GridLayout(1, 2, 10, 10));
//创建2个面板
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1, 3));
JPanel p2 = new JPanel(new BorderLayout());
p2.setBackground(Color.BLUE);
//设置面板边框,标题
p1.setBorder(BorderFactory.createTitledBorder("面板1"));
p2.setBorder(BorderFactory.createTitledBorder("面板2"));
p1.add(new JButton("b1"));
p1.add(new JButton("b1"));
p1.add(new JButton("b1"));
p1.add(new JButton("b1"));
p2.add(new JButton("b2"), BorderLayout.EAST);
p2.add(new JButton("b2"), BorderLayout.WEST);
p2.add(new JButton("b2"), BorderLayout.SOUTH);
p2.add(new JButton("b2"), BorderLayout.NORTH);
p2.add(new JButton("b2"));
c.add(p1);
c.add(p2);
setVisible(true);
} public static void main(String[] args) {
new Demo();
}
}
public class Demo extends JFrame {
public Demo() {
setBounds(100, 100, 200, 100);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container c = getContentPane();
JTextArea area=new JTextArea();//文本域
JScrollPane sp=new JScrollPane(area);//将文本域添加到滚动面板中
c.add(sp);
setVisible(true);
} public static void main(String[] args) {
new Demo();
}
}