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

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

面板JPanel】 面板就是一个容器 每一个容器都可以有一个自己的独立的布局和组件,这些容器之间也不会互相干扰

//导入Java类
import javax.swing.*;
import java.awt.*;
public class Demo extends JFrame{
public Demo(){
setBounds(100,100,500,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container c=getContentPane();
c.setLayout(new GridLayout(2,2)); //创建新的组件 JPanel面板
JPanel p1=new JPanel(new GridLayout(1,3,10,10));//布局可写在组件内
//p1.setLayout(new GridLayout(1,3,10,10));
JPanel p2=new JPanel(new BorderLayout());
JPanel p3=new JPanel(new GridLayout(1,2,10,10));
JPanel p4=new JPanel(new GridLayout(2,1,10,10)); //给每个面板设置边框
p1.setBorder(BorderFactory.createTitledBorder("面板1"));//设置面板
p2.setBorder(BorderFactory.createTitledBorder("面板2"));
p3.setBorder(BorderFactory.createTitledBorder("面板3"));
p4.setBorder(BorderFactory.createTitledBorder("面板4")); //给每个面板添加JButton组件
//设置四个按钮的原因:我们网格布局是1,3 网格布局的优势:多添加或少添加组件行不变,列变
p1.add(new JButton("p1"));
p1.add(new JButton("p1"));
p1.add(new JButton("p1"));
p1.add(new JButton("p1")); p2.add(new JButton("p2"),BorderLayout.CENTER);
p2.add(new JButton("p2"),BorderLayout.EAST);
p2.add(new JButton("p2"),BorderLayout.WEST);
p2.add(new JButton("p2"),BorderLayout.SOUTH);
p2.add(new JButton("p2"),BorderLayout.NORTH); p3.add(new JButton("p3")); p3.add(new JButton("p3")); p4.add(new JButton("p4")); p4.add(new JButton("p4"));
//可设置美观的 例如背景颜色
p4.setBackground(Color.BLUE);
c.add(p1);c.add(p2);c.add(p3);c.add(p4);
setVisible(true);
} public static void main(String[] args) {
new Demo();
}
}

滚动面板 JScrollPane

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

//导入Java类
import javax.swing.*;
import java.awt.*;
public class Demo extends JFrame{
public Demo(){
setBounds(100,100,100,150);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container c=getContentPane();
JTextArea t=new JTextArea();//创建文本域
JScrollPane s=new JScrollPane(t);//创建滚动面板 将t(文本域添加到组建中)
c.add(s);//将滚动面板添加到容器中 setVisible(true);
}
public static void main(String[] args) {
new Demo();
   }
}
上一篇:localtime和localtime_r


下一篇:a标签无跳转的死链接