Java可视化界面简单写法

主窗口

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

public class Main extends JFrame implements MouseListener {
    /*
        定义控件
     */
    JPanel Tools;//功能选项模块
    /*
        功能选项子控件
     */
    JButton Button_Login;//登陆按钮
    JButton Button_Support;//支持能力按钮
    JButton Button_Listen;//监听按钮
    JButton Button_Remote;//远程配置按钮

    JPanel Event;//事件推送模块
    /*
        事件推送消息栏
     */
    JPanel Others;//其他模块
    /*
        其他模块
     */
    JButton Button_Exit;//退出按钮
    JButton Button_Clear;//清空按钮
    JButton Button_Save;//保存按钮


    public Main() {
        /*
            初始化主窗口
         */
        this.setTitle("远程控制Demo");
        this.setSize(500, 400);
        this.setLocation(500, 400);
        this.setLayout(new BorderLayout());//按照边界布局

        /*
            初始化功能选项模块
         */
        Tools = new JPanel(new GridLayout(4,1,20,50));
        Button_Login = new JButton("Login");//初始化登陆按钮
        Button_Support = new JButton("Support");//初始化支持能力集按钮
        Button_Listen = new JButton("Listen");//初始化监听配置按钮
        Button_Remote = new JButton("Remote");//初始化远程配置按钮
        //将按钮添加到面板里
        Tools.add(Button_Login);
        Tools.add(Button_Support);
        Tools.add(Button_Listen);
        Tools.add(Button_Remote);
        //将面板加入主页面
        this.add(Tools,BorderLayout.WEST);

        /*
            事件推送模块
         */
        Event = new JPanel();
        this.add(Event,BorderLayout.NORTH);
        /*
            其他模块
         */
        Others = new JPanel();
        Button_Exit = new JButton("Exit");
        Button_Clear = new JButton("Clear");
        Button_Save = new JButton("Save");
        Others.add(Button_Exit);
        Others.add(Button_Clear);
        Others.add(Button_Save);
        this.add(Others,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        /*
            添加按钮点击监听器
         */
        Button_Login.addMouseListener(this);
    }

    /*
        添加事件监听
     */

    @Override
    public void mouseClicked(MouseEvent e) {
        if(e.getSource()==Button_Login)
        {
            //登陆按钮
            new Login();
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    public static void main(String[] args) {
        Main testBorderLayout = new Main();

    }
}


登陆窗口

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

public class Login extends JFrame implements MouseListener{
    /*
        控件信息
     */
    JLabel Label_UserName;//用户名标签
    JLabel Label_PassWord;//密码标签
    JTextField Text_UserName;//用户名输入框
    JPasswordField Text_PassWord;//密码输入框
    JButton Button_Login;//登陆按钮
    JButton Button_Exit;//退出按钮

    /*
        初始化窗体
     */
    public Login()
    {
        /*
            初始化窗口
         */
        this.setTitle("Login");
        this.setSize(500, 400);
        this.setLocation(500, 400);
        this.setLayout((new GridLayout(3,3)));
        /*
            初始化控件
         */
        Label_UserName = new JLabel("UserName:");
        Label_PassWord = new JLabel("PassWord:");
        Text_UserName = new JTextField("admin");
        Text_PassWord = new JPasswordField("hik12345");
        Button_Login = new JButton("submit");
        Button_Exit = new JButton("Exit");
        /*
            添加控件
         */
        this.add(Label_UserName,0);
        this.add(Text_UserName,1);
        this.add(Label_PassWord,2);
        this.add(Text_PassWord,3);
        this.add(Button_Login,4);
        this.add(Button_Exit,5);

        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}

你可能感兴趣的:(JAVA,java)