新手尝试:先在各个面板上分别开发界面,再将各个面板分别加入顶层容器contentpane.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class window extends JFrame{
//一定要extends JFrame,否则setLayout(null)后setBounds没用效果,为空白窗口
static final int WIDTH=1200;
static final int HEIGHT=900;
public static void main(String[] args) {
//主窗体
JFrame f=new JFrame();
f.setTitle("温度开关检测系统 ");
f.setSize(WIDTH,HEIGHT);
//顶层面板设置为contentpane
JPanel contentPane=new JPanel();
f.setContentPane(contentPane);
//五个下拉列表装入p1面板上
String[] s1={" COPM1","COPM2"};
String[] s2={" 9658","9659"};
String[] s3={" 8"," 9"};
String[] s4={" 0"," 1"};
String[] s5={" 1"," 2"};
JPanel p1=new JPanel();
p1.setLayout(null);
JComboBox combol1=new JComboBox(s1);
combol1.setBorder(BorderFactory.createTitledBorder("串口号"));
combol1.setBounds(110, 10, 180, 50);
JComboBox combol2=new JComboBox(s2);
combol2.setBorder(BorderFactory.createTitledBorder("波特率"));
combol2.setBounds(110, 70, 180, 50);
JComboBox combol3=new JComboBox(s3);
combol3.setBorder(BorderFactory.createTitledBorder("数据位"));
combol3.setBounds(110, 130, 180, 50);
JComboBox combol4=new JComboBox(s4);
combol4.setBorder(BorderFactory.createTitledBorder("校验位"));
combol4.setBounds(110, 190, 180, 50);
JComboBox combol5=new JComboBox(s5);
combol5.setBorder(BorderFactory.createTitledBorder("停止位"));
combol5.setBounds(110, 250, 180, 50);
p1.add(combol1);
p1.add(combol2);
p1.add(combol3);
p1.add(combol4);
p1.add(combol5);
//5个下拉列表装p1后 加入顶层面板上
contentPane.add(p1);
//五个按钮装在p2面板上
JPanel p2=new JPanel();
JButton[] jbArray=new JButton[] {new JButton("打开串口"),new JButton("开始测量"),new JButton("结束测量"),new JButton("保存数据"),new JButton("退出系统")};
p2.setLayout(null);
for(int i=0;i<jbArray.length;i++){
jbArray[i].setBounds(110, 90+40*i,180,30);
p2.add(jbArray[i]);
}
//按钮添加到顶层容器上
contentPane.add(p2);
//湖南工业大学 logo加入 p3面板
JPanel p3=new JPanel(){
Image img=Toolkit.getDefaultToolkit().getImage("D://1.png");
public void paint(Graphics g){
g.drawImage(img, 10, 10, 380, 180, this);
}
};
contentPane.add(p3);
//数据接收文字加入p4面板
JPanel p4=new JPanel();
p4.setLayout(null);
JLabel label=new JLabel("数据接收:");
int size=15;
label.setFont(new Font("Serif",Font.BOLD,size));
label.setBounds(30,0,300,50);
p4.add(label);
contentPane.add(p4);
//表格加入p5面板
JPanel p5=new JPanel();
String[] Names={"编号","时间","温度"," ","编号","时间","温度"," ","编号","时间","温度"};
Object[][] playerInfo={
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
{new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String(),new String()},
};
JTable table=new JTable(playerInfo,Names);
//设置表格的初始大小为750,750
table.setPreferredScrollableViewportSize(new Dimension(750,750));
//表格加入scrollpane
JScrollPane scrollpane=new JScrollPane(table);
//设置居中布局
p5.add(scrollpane,BorderLayout.CENTER);
//p5面板加入顶层容器
contentPane.add(p5);
//组合几个面板
contentPane.setLayout(null);
//给几个面板设置边框
p1.setBorder(BorderFactory.createLoweredBevelBorder());
p2.setBorder(BorderFactory.createLoweredBevelBorder());
p3.setBorder(BorderFactory.createLoweredBevelBorder());
p4.setBorder(BorderFactory.createLoweredBevelBorder());
p5.setBorder(BorderFactory.createLoweredBevelBorder());
//设置各个面板的精确位置
p1.setBounds(0, 0,400, 350);
p2.setBounds(0, 300,400, 350);
p3.setBounds(0, 650,400, 200);
p4.setBounds(400, 0,800, 50);
p5.setBounds(400, 50,800, 800);
f.setVisible(true);
//GridLayout gird=new GridLayout(5,1);
//p1.setLayout(gird);
//p1.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 0));设置布局的两种方法
}
}
结果图: