首先上桌面图

java jframe swing 设置窗口最大 javaswing界面_布局

下面是代码

//下列代码块设置的是主框体
JFrame f = new JFrame("控制台");
//设置关闭主窗体即关闭程序
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置主框体大小
f.setSize(650, 600);
//设置主框体布局为border布局(即设置容器布局 10表示BorderLayout布局水平间距,5表示BorderLayout布局垂直间距)
f.setLayout(new BorderLayout(10,5));
//这里设置主框体与显示的窗口的各个方向的间距
((JPanel)f.getContentPane()).setBorder(BorderFactory.createEmptyBorder(5,10,10,10));

//这里设置主框体北部布局,包含服务器状态的显示,启停按钮
JPanel jPanelNorth = new JPanel();
//特别注意:一旦组件被添加到某布局中,就需要按下行代码进行设置
jPanelNorth.setPreferredSize(new Dimension(630,40));
//设置流布局形式,并且水平间距为10,
jPanelNorth.setLayout(new FlowLayout(FlowLayout.LEFT,10,0));
//将jPanelNorth面板放到主框体的北部
f.add(jPanelNorth,"North");

//设置显示服务器状态的JLable
JLabel jLabelMsg = new JLabel();
//设置jLabelMsg大小
jLabelMsg.setPreferredSize(new Dimension(120,40));
//设置字体与文字
Font font = new Font("Dialog",1,15);
jLabelMsg.setFont(font);
jLabelMsg.setText("服务器状态:");
//jPanelNorth采用的是流式布局,所以直接添加会从左往右排列
jPanelNorth.add(jLabelMsg);
//设置显示具体状态的组件
JLabel jLabelStatus = new JLabel();
jLabelStatus.setPreferredSize(new Dimension(260,40));
//这里是从后台动态拉取需要显示的服务器状态值
String data = monitorService.getTomcatStatus();
if(data.equals("运行")){
    jLabelStatus.setText("运行");
    jLabelStatus.setForeground(Color.green);
}
    else{
        jLabelStatus.setText("停止");
        jLabelStatus.setForeground(Color.red);
    }
jLabelStatus.setFont(font);
jPanelNorth.add(jLabelStatus);

//设置启停按钮
JLabel jLabel3 = new JLabel();
jLabel3.setPreferredSize(new Dimension(180,40));
//这里再次嵌套一个布局是为了能够控制两个按钮的间距,如果不嵌套布局将无法控制按钮间距
jLabel3.setLayout(new FlowLayout(FlowLayout.CENTER,20,5));
JButton jButton1_jPanelNorth = new JButton("启动");
JButton jButton2_jPanelNorth = new JButton("停止");
jButton1_jPanelNorth.setFont(font);
jButton2_jPanelNorth.setFont(font);
jLabel3.add(jButton1_jPanelNorth);
jLabel3.add(jButton2_jPanelNorth);
jPanelNorth.add(jLabel3);

//设置主框体的中心布局以及组件
JTableOperation jTableInit = new JTableOperation();
//初始化一个带初始数据的JTable
JTable jTable = jTableInit.JtableDataInit();
//这里JTable必须要嵌套在JScrollPane中才行,不嵌套的话将无法显示列名
JScrollPane scrollPane=new JScrollPane(jTable);
scrollPane.setPreferredSize(new Dimension(535, 540));
GlobalObject.setjScrollPane(scrollPane);

f.add(scrollPane,"Center");

//设置按钮
JPanel jPanel = new JPanel();
JButton jButton1 = new JButton("启动传输");
jButton1.setPreferredSize(new Dimension(100, 30));
JButton jButton2 = new JButton("停止传输");
jButton2.setPreferredSize(new Dimension(100, 30));
JButton jButton3 = new JButton("查看详情");
jButton3.setPreferredSize(new Dimension(100, 30));
JButton jButton4 = new JButton("新增商户");
jButton4.setPreferredSize(new Dimension(100, 30));
JButton jButton5 = new JButton("删除商户");
jButton5.setPreferredSize(new Dimension(100, 30));

//设置主框体的东部布局以及组件
jPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 15));
jPanel.setPreferredSize(new Dimension(120, 540));
jPanel.add(jButton3);
jPanel.add(jButton1);
jPanel.add(jButton2);
jPanel.add(jButton4);
jPanel.add(jButton5);
f.add(jPanel,"East");


//增加监听
MyActionListener myActionListener = new MyActionListener();
jTable.addMouseListener(myActionListener);
jButton1.addActionListener(myActionListener);
jButton2.addActionListener(myActionListener);
jButton3.addActionListener(myActionListener);
jButton4.addActionListener(myActionListener);
jButton5.addActionListener(myActionListener);
jButton1_jPanelNorth.addActionListener(myActionListener);
jButton2_jPanelNorth.addActionListener(myActionListener);

//最后显示主框体,必须要有
f.setVisible(true);

swing的布局设计思路与web页面设计类似,定完主框体布局后,再在每个布局下添加面板组件,然后再定义该面板布局,然后再在特别布局区域添加下一个面板或其他组件,这样层层的布局嵌套,当然swing写桌面程序还具有很大的局限性,所以一般是不用java来写桌面程序的。