由于JPannel默认布局是FlowLayout,直接添加多个JPannel的话会被覆盖
首先把布局设置为null:
jp2.setLayout(null);
然后设置需要添加的JPannel的位置和大小
jp1.setBounds(50,50,500,600);//起始横坐标,起始纵坐标,长,宽
测试:
import javax.swing.*;
import java.awt.*;
public class Demo1_1 extends JFrame{
JPanel jp1,jp2;
JTextField jt;
JButton jb;
public static void main(String[] args) {
new Demo1_1();
}
public Demo1_1(){
jp1 = new JPanel();
jp2 = new JPanel();
jt = new JTextField(10);
jb = new JButton("确定");
jp2.setLayout(null);
jp1.add(jt);
jp1.add(jb);
jp2.add(jp1);
jp1.setBackground(Color.black);
jp1.setBounds(50,50,500,600);
this.add(jp2);
this.setSize(1000,1000);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
-------------------------------------------------------------此内容为查询整理后总结