欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

【JDK】【Java工具】Java GUI实现窗口跳转,Java GUI基本使用

程序员文章站 2024-03-30 21:03:51
中午遇见了一个需求,100块钱,一个银行的系统,用Java图形界面这一套写。100块钱不值,但我没用Java图形界面写过东西,就想搞清楚一下其中的类之间的关系,顺便实现一些基本的功能。它需求很明确,需要切换窗口,需要持久层存储数据。持久层就不搞了,就基本实现我暂时不会的切换窗口吧。基本实现:每一个类相当于就是一个单独的页面每个类中需要定义属性,属性为窗口的"dom"元素在实例化的时候,把窗口中的属性给实例化,相当于初始化dom,初始化页面。每个页面需要关联在一起,则需要监听自己窗...

我的代码只实现了窗口跳转等基本功能,需要学习Java GUI布局,可以看看这位兄嘚的文章
JAVA 图形界面大作业(一) 布局篇

中午遇见了一个需求,100块钱,一个银行的系统,用Java图形界面这一套写。

100块钱不值,但我没用Java图形界面写过东西,拒绝这个单子之后,抱着学习的态度,就想搞清楚一下其中的类之间的关系,顺便实现一些基本的功能。

【JDK】【Java工具】Java GUI实现窗口跳转,Java GUI基本使用

它需求很明确,需要切换窗口,需要持久层存储数据。持久层就不搞了,就基本实现我暂时不会的切换窗口吧。

基本实现:

每一个类相当于就是一个单独的页面

  • 每个类中需要定义属性,属性为窗口的"dom"元素

  • 在实例化的时候,把窗口中的属性给实例化,相当于初始化dom,初始化页面。

  • 每个页面需要关联在一起,则需要监听自己窗口中的事件,事件中有切换窗口的代码。

  • 监听到点击事件后,可以操作持久层,很简单。

下面是我建项目的目录

【JDK】【Java工具】Java GUI实现窗口跳转,Java GUI基本使用

Main.java

package com.tangxz.bind;

import com.tangxz.test.Menu;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2020/11/09 15:30
 */
public class Main {
    public static void main(String[] args) {
        new Bind();
    }
}

Bind.java

package com.tangxz.bind;

import com.tangxz.bind.admin.AdminLogin;
import com.tangxz.bind.setting.Setting;
import com.tangxz.bind.user.UserLogin;
import com.tangxz.test.Newframe;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2020/11/09 14:53
 */
public class Bind extends JFrame implements ActionListener {
    private JButton adminLoginButton;
    private JButton userLoginButton;
    private JButton settingButton;

    public Bind() {
        this.setLayout(new FlowLayout());
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        this.setSize(300,200);
        this.setLocation(300,400);
        //监听右上角的退出按钮
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });
        adminLoginButton = new JButton("管理员登录");
        userLoginButton = new JButton("用户登录");
        settingButton = new JButton("系统设置");
        adminLoginButton.setBounds(100, 20, 165, 25);
        userLoginButton.setBounds(100, 50, 165, 25);
        settingButton.setBounds(100, 80, 165, 25);

        this.add(adminLoginButton);
        this.add(userLoginButton);
        this.add(settingButton);

        adminLoginButton.addActionListener(this);
        userLoginButton.addActionListener(this);
        settingButton.addActionListener(this);


        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == adminLoginButton){
            this.dispose();
            new AdminLogin();
        }else if(e.getSource() == userLoginButton){
            this.dispose();
            new UserLogin();
        }else if(e.getSource() == settingButton){
            this.dispose();
            new Setting();
        }
    }
}

UserLogin.java

package com.tangxz.bind.user;

import com.tangxz.bind.admin.AdminIndex;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2020/11/09 14:53
 */
public class UserLogin extends JFrame implements ActionListener {
    private JButton submit;
    private JTextField user_num;
    private JPasswordField user_pwd;
    private JLabel info;

    public UserLogin() {
        setLayout(new FlowLayout());
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        this.setSize(300, 200);
        this.setLocation(300, 400);
        //监听右上角的退出按钮
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });
        //设置输入框
        user_num = new JTextField(20);
        user_num.setBounds(100, 20, 165, 25);
        this.add(user_num);
        user_pwd = new JPasswordField(20);
        user_pwd.setBounds(100, 20, 165, 25);
        this.add(user_pwd);

        //设置按钮
        submit = new JButton("登录");
        this.add(submit);
        submit.addActionListener(this);
        //设置提示消息
        info = new JLabel("", JLabel.CENTER);
        info.setSize(350, 100);
        info.setBackground(new Color(102, 14, 122));
        this.add(info);

        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == submit) {
            //登录验证
            if (user_num.getText().isEmpty() || !user_num.getText().equals("user")) {
                System.out.println(user_num.getText());
                info.setText("用户名错误!");
            } else if (new String(user_pwd.getPassword()).isEmpty() || !new String(user_pwd.getPassword()).equals("123")) {
                System.out.println(new String(user_pwd.getPassword()));
                info.setText("密码错误");
            } else {
                //关闭当前窗口
                this.dispose();
                new UserIndex();
            }
        }
    }
}

UserIndex.java

package com.tangxz.bind.user;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2020/11/09 14:59
 */
public class UserIndex extends JFrame implements ActionListener {
    private JButton showMoney;
    private JButton getMoney;
    private JLabel info;
    private String money;
    public UserIndex() {
        this.setLayout(new FlowLayout());
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        this.setSize(300,200);
        this.setLocation(300,400);
        //监听右上角的退出按钮
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });

        showMoney = new JButton("显示金额");
        getMoney = new JButton("取钱");
        this.add(showMoney);
        this.add(getMoney);

        showMoney.addActionListener(this);
        getMoney.addActionListener(this);


        //设置提示消息
        info = new JLabel("", JLabel.CENTER);
        info.setSize(350, 100);
        info.setBackground(new Color(102,14,122));
        this.add(info);

        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == showMoney){
            money = "123.00";
            info.setText(money);
        }else if(e.getSource() == getMoney){
            money = "现在有123.00";
            info.setText(money);
        }
    }
}

AdminLogin.java

package com.tangxz.bind.admin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2020/11/09 15:00
 */
public class AdminLogin extends JFrame implements ActionListener {
    private JButton submit;
    private JTextField user_num;
    private JPasswordField user_pwd;
    private JLabel info;
    public AdminLogin() {
        setLayout(new FlowLayout());
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        this.setSize(300,200);
        this.setLocation(300,400);
        //监听右上角的退出按钮
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });
        //设置输入框
        user_num = new JTextField(20);
        user_num.setBounds(100, 20, 165, 25);
        this.add(user_num);
        user_pwd = new JPasswordField(20);
        user_pwd.setBounds(100, 20, 165, 25);
        this.add(user_pwd);

        //设置按钮
        submit = new JButton("登录");
        this.add(submit);
        submit.addActionListener(this);
        //设置提示消息
        info = new JLabel("", JLabel.CENTER);
        info.setSize(350, 100);
        info.setBackground(new Color(102,14,122));
        this.add(info);

        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == submit){
            //登录验证
            if (user_num.getText().isEmpty()||!user_num.getText().equals("Tangxz")){
                System.out.println(user_num.getText());
                info.setText("用户名错误!");
            }else if (new String(user_pwd.getPassword()).isEmpty()||!new String(user_pwd.getPassword()).equals("123")){
                System.out.println(new String(user_pwd.getPassword()));
                info.setText("密码错误");
            }else {
                //关闭当前窗口
                this.dispose();
                new AdminIndex();
            }
        }
    }
}

AdminIndex.java

package com.tangxz.bind.admin;

import com.tangxz.bind.user.UserLogin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2020/11/09 15:00
 */
public class AdminIndex extends JFrame implements ActionListener {
    private JButton showAllUser;
    private JButton setting;
    private JLabel info;
    private String allUser;

    public AdminIndex() {
        this.setLayout(new FlowLayout());
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        this.setSize(300,200);
        this.setLocation(300,400);
        //监听右上角的退出按钮
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });

        showAllUser = new JButton("显示全部用户");
        setting = new JButton("设置");
        this.add(showAllUser);
        this.add(setting);

        showAllUser.addActionListener(this);
        setting.addActionListener(this);


        //设置提示消息
        info = new JLabel("", JLabel.CENTER);
        info.setSize(350, 100);
        info.setBackground(new Color(102,14,122));
        this.add(info);

        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == showAllUser){
            allUser = "我就是全部用户";
            info.setText(allUser);
        }else if(e.getSource() == setting){
            allUser = "我就是设置";
            info.setText(allUser);
        }
    }
}

Setting.java(懒得写了)

package com.tangxz.bind.setting;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2020/11/09 15:00
 */
public class Setting extends JFrame implements ActionListener {
    public Setting() {
    }
    @Override
    public void actionPerformed(ActionEvent e) {

    }
}

本文地址:https://blog.csdn.net/weixin_42295814/article/details/109581000

相关标签: JDK Java工具