二、common类建立好后就来建立一个窗体程序(com.window.ServerWindow.java)用来实例和拼装窗体
package com.window;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import com.common.FrameVariable;
/**
* 实现windows窗体类
*
@author q群 484994461
*/
public class ServerWindow {
//单例的标准写法start
private FrameVariable variable;
private static ServerWindow instance = null;
public static ServerWindow getInstance(){
if(instance == null)
instance = new ServerWindow();
return instance;
}
//单例的标准写法end
//私有化成员函数(单例用)
private ServerWindow(){
variable = FrameVariable.getInstance();
//初始化各个布局
initLayout();
//初始化窗体
initJFrame();
//初始化顶层容器
initJPanel();
//初始化North部分
initNorth();
//初始化Center部分
initCenter();
//组装各个零件
instComponent();
}
/**
* 初始化布局
*/
private void initLayout(){
//实例GridLayout会把容器平均分成纵向y份横向x份(y,x)
variable.frameLayout = new GridLayout(1,1);
//实例BorderLayout会把容器分成上(north),下(south),左(west),右(east),中间(center)5部分
variable.panelLayout = new BorderLayout();
//实例Center部分Layout
variable.centerLayout = new GridLayout();
//实例North部分Layout 1行2列
variable.northLayout = new GridLayout(1,2);
//North部分Layout各个部分水平距离20px
variable.northLayout.setHgap(20);
}
/**
* 初始化IFrame窗体
*/
private void initJFrame(){
//新建一个IFrame窗体
variable.serverFrame = new JFrame("server stopped");
//设置窗体左上角位置距离左侧100px,距离上方200px
variable.serverFrame.setLocation(100,200);
//设置窗体大小宽250px,高400px
variable.serverFrame.setSize(250, 400);
//设置IFrame顶层窗体布局为横向1份纵向1份
variable.serverFrame.setLayout(variable.frameLayout);
}
/**
* 初始化顶层容器
*/
private void initJPanel(){
//实例JPanel容器 并放置于IFrame顶层
variable.serverPanel = new JPanel();
variable.serverFrame.add(variable.serverPanel);
//JPanel容器设置borderLayout
variable.serverPanel.setLayout(variable.panelLayout);
}
/**
* 初始化center部分
*/
private void initCenter(){
//实例Center部分JPanel容器
variable.centerPanel = new JPanel();
//实例Center部分滚动条 把JPanel容器加入进来
variable.centerScroll = new JScrollPane(variable.centerPanel);
variable.centerPanel.setLayout(variable.centerLayout);
}
/**
* 初始化north部分
*/
private void initNorth(){
//实例North部分JPanel容器
variable.northPanel = new JPanel();
//设置布局 1行2列横向距离20px
variable.northPanel.setLayout(variable.northLayout);
//实例startButton
variable.startButton = new JButton("start server");
variable.startButton.setBackground(Color.PINK);
//实例knickButton
variable.knickButton = new JButton("knick");
variable.knickButton.setBackground(Color.RED);
}
/**
* 组装各个零件
* 有小到大组装
*/
private void instComponent(){
//north部分
//GridLayout 第1行第1格
variable.northPanel.add(variable.startButton);
//GridLayout 第1行第2格
variable.northPanel.add(variable.knickButton);
//顶层
//把JScrollPane加入到顶层JPanel中并设置为center
variable.serverPanel.add(variable.centerScroll,BorderLayout.CENTER);
//把northPane加入到顶层JPanel并设置north
variable.serverPanel.add(variable.northPanel,BorderLayout.NORTH);
//窗体
//把顶层JPanel加入到窗体中
variable.serverFrame.add(variable.serverPanel);
}
/**
* 显示窗体方法
*/
public void show(){
//设置默认关闭方法
variable.serverFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗体可见
variable.serverFrame.setVisible(true);
}
}