实验目的
1.掌握Java中设计GUI界面的方法
2.掌握Java GUI界面上的事件监听机制
3.掌握GUI类与问题域交互的方法
目录
前言
环境:IDEA
按题目要求完成
图形用户界面及事件响应的相关题,在自己的电脑运行正确,提交后出现错误不用管,也不用担心没分。
实验内容
一、设计Manager类、GUI类及事件处理
题目要求:
(1)设计一个Manager类并编写代码, Manager类的属性有姓名,工号,基本工资,小时工资(元/小时)。自定义方法:至少包括计算月工资的方法:calSalary()。
(2)编写一个GUI类,输入manager的姓名、工号,基本工资,月工作时间(小时),创建对象,调用calSalary()方法计算出该manager的月工资,并显示在用户界面上。
代码实现:
//Manager.java
package experiment_5_1;
public class Manager {
private String name,ID;
private int baseSalary,wageOfHour,workHourOfMonthly;
public int calSalary(){
return baseSalary+workHourOfMonthly*wageOfHour;
}
public String getName() {
return name;
}
public Manager(String name, String ID, int baseSalary, int wageOfHour, int workHourOfMonthly) {
this.name = name;
this.ID = ID;
this.baseSalary = baseSalary;
this.wageOfHour = wageOfHour;
this.workHourOfMonthly = workHourOfMonthly;
}
public int getWorkHourOfMonthly() {
return workHourOfMonthly;
}
public String getID() {
return ID;
}
public int getBaseSalary() {
return baseSalary;
}
public int getWageOfHour() {
return wageOfHour;
}
}
//GUI.java
package experiment_5_1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JFrame implements ActionListener {
/*声明全局变量*/
//命令按钮
JButton button1 = new JButton("calculate");
JButton button2 = new JButton("cancel");//cancel:取消
//文本框
JTextField textField1 = new JTextField(10);
JTextField textField2 = new JTextField(10);
JTextField textField3 = new JTextField(10);
JTextField textField4 = new JTextField(10);
JTextField textField5 = new JTextField(10);
//文本区域
JTextArea textArea = new JTextArea(40,35);
//textArea.setText("result:");?为什么会报错
/*constructor*/
public GUI(){
super("calculate of salary");
//获取内容面板
Container c = this.getContentPane();
//设置容器排列组件形式
c.setLayout(new GridLayout(5,1));
//文本区域初始化
textArea.setText("result:");
/*创建中间容器,upperPanel、lowerPanel (多层布局)*/
//grid 网格;flow 流;
JPanel upperPanel1 = new JPanel(new FlowLayout());
JPanel upperPanel2 = new JPanel(new FlowLayout());
JPanel upperPanel3 = new JPanel(new FlowLayout());
JPanel middlePanel = new JPanel(new FlowLayout());
JPanel lowerPanel = new JPanel(new FlowLayout());
/*创建组件*/
//标签类
JLabel label1 = new JLabel("name:");
JLabel label2 = new JLabel("empNo:");
JLabel label3 = new JLabel("baseSalary:");
JLabel label4 = new JLabel("wageOfHour:");
JLabel label5 = new JLabel("workHourOfMonthly:");
/*注册按钮事件到监听器Frame,监听按钮事件*/
button1.addActionListener(this);
button2.addActionListener(this);
/*添加组件到Panel*/
upperPanel1.add(label1);
upperPanel1.add(textField1);
upperPanel1.add(label2);
upperPanel1.add(textField2);
upperPanel2.add(label3);
upperPanel2.add(textField3);
upperPanel2.add(label4);
upperPanel2.add(textField4);
upperPanel3.add(label5);
upperPanel3.add(textField5);
middlePanel.add(button1);
middlePanel.add(button2);
lowerPanel.add(textArea);
/*在主容器添加upperPanel lowerPanel*/
c.add(upperPanel1);
c.add(upperPanel2);
c.add(upperPanel3);
c.add(middlePanel);
c.add(lowerPanel);
//关闭窗口并结束程序运行
//default:默认 ; exit on close:关闭出口
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗口大小;this 代表本frame
this.setSize(500,650);
//设置窗口位置居中
this.setLocationRelativeTo(null);
//设置窗口可见
this.setVisible(true);
}
/*main*/
public static void main(String[] args) {
//创建窗口(顶层容器)的一个实例
new GUI();
}
/*当实现接口ActionListener的接受事件和分派时间的actionPerformed方法
* 按钮被单击后actionPerformed方法被调用*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button2){
clearText();
}
if (e.getSource() == button1){
calSalary();
}
}
/*实现按钮button1:calculate 事件的方法*/
private void calSalary() {
//获取文本框的输入内容
String name = textField1.getText();
String ID = textField2.getText();
int baseSalary = Integer.parseInt(textField3.getText());//parse:解析
int wageOfHour = Integer.parseInt(textField4.getText());
int workHourOfMonthly = Integer.parseInt(textField5.getText());
Manager manager = new Manager(name,ID,baseSalary,wageOfHour,workHourOfMonthly);
String info;
textArea.setText("result:\n");
info = "name:"+manager.getName()+"\n"+
"empNo:"+manager.getID()+"\n"+
"baseSalary:"+manager.getBaseSalary()+"\n"+
"wageOfHour:"+manager.getWageOfHour()+"\n"+
"workHourOfMonthly:"+manager.getWorkHourOfMonthly()+"\n"+
"salary:"+manager.calSalary();
textArea.append(info);//append:附加
}
/*实现button2 cancel 事件的方法*/
private void clearText() {
textField1.setText("");
textField2.setText("");
textField3.setText("");
textField4.setText("");
textField5.setText("");
}
}
结果显示:
二、用户登录-GUI及事件响应
题目要求:
编写一个JFrame框架应用程序,如图,并根据登录情况显示相应的提示信息。假设用户名为admin,密码为admin。
事件响应:输入正确的用户名和密码,系统提示“Seccussful!Username and password is correct!”,否则提示“Username or password isn't correct!”
说明:两个消息提示框不需要自己编写界面类,使用JOptionPane类调用showMessageDialog()
代码实现:
//UserLogin.java
package experiment_5_2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UserLogin extends JFrame implements ActionListener {
/*全局变量*/
//按钮
JButton loginBtn,cancelBtn;
//口令框 https://blog.csdn.net/SkeletonKing233/article/details/104985532
JPasswordField nameField = new JPasswordField(10);
JPasswordField passwordField = new JPasswordField(10);
public UserLogin(){
//设置标题“Login”
super("Login");
//初始化容器
Container c = this.getContentPane();
c.setLayout(new GridLayout(3,2,5,5));
//设置三个中间容器
JPanel upperPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel middlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel lowerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
/*创建组件*/
//标签
JLabel nameLabel = new JLabel("UserName");
JLabel passwordLabel = new JLabel("Password");
//按钮
loginBtn = new JButton("Login");
cancelBtn = new JButton("Cancel");
/*添加组件到中间容器*/
upperPanel.add(nameLabel);
upperPanel.add(nameField);
middlePanel.add(passwordLabel);
middlePanel.add(passwordField);
lowerPanel.add(loginBtn);
lowerPanel.add(cancelBtn);
//中间容器到容器
c.add(upperPanel);
c.add(middlePanel);
c.add(lowerPanel);
/*注册事件到监听器*/
loginBtn.addActionListener(this);//this 代表这个Frame
cancelBtn.addActionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//default:默认
this.setSize(360,170);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
//create instance of Frame
new UserLogin();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginBtn){
login();
}
if (e.getSource() == cancelBtn){
cancel();
}
}
private void cancel() {
nameField.setText("");
passwordField.setText("");
}
private void login() {
//getPassword返回的是字符数组 判断方法见下
//https://bbs.csdn.net/topics/70132524#:~:text=%E6%88%91%E6%83%B3%E5%88%A4%E6%96%ADpassword%E5%92%8Ccheckpassword%E8%BE%93%E5%85%A5%E7%9A%84%E6%98%AF%E5%90%A6%E4%B8%80%E8%87%B4%EF%BC%8C%E7%94%A8%E4%BA%86%E8%BF%99%E6%A0%B7%E4%B8%80%E5%8F%A5%EF%BC%9A%20if%20%28checkpassword.getPassword,%28%29%21%3Dpassword.getPassword%20%28%29%29%20%7BJOptionPane.showMessageDialog%20%28this%2C%22%E5%AF%86%E7%A0%81%E4%B8%8E%E7%A1%AE%E8%AE%A4%E5%AF%86%E7%A0%81%E5%BF%85%E9%A1%BB%E4%B8%80%E8%87%B4%22%2C%22%E9%94%99%E8%AF%AF%E4%BF%A1%E6%81%AF%E6%8F%90%E7%A4%BA%22%2CJOptionPane.ERROR_MESSAGE%29%3B%7D
if (new String(nameField.getPassword()).equals("admin")&&new String(passwordField.getPassword()).equals("admin")){
//“equals()”与“==”比较一个对象的不同之处在于:“equals()”只比较对象的“内容”,而“==”比较的是对象的地址(当然,如果地址相同了,内容当然也相同)。
//https://www.cnblogs.com/Libinkai/p/9306790.html
JOptionPane.showMessageDialog(null,"Successful!\nUsername and password is correct!","notice", JOptionPane.INFORMATION_MESSAGE);
}
else {
JOptionPane.showMessageDialog(null,"Username or password isn't correct!","notice", JOptionPane.INFORMATION_MESSAGE);
}
}
}
引用的材料:
Java:JPasswordField类:
(37条消息) Java:JPasswordField类_SkeletonKing233的博客-CSDN博客
确认密码是否相同:
关于确认密码是否一致的问题! JPasswordField-CSDN社区
JAVA中JPasswordField实现密码的确认 - 神的彬彬 - 博客园 (cnblogs.com)
结果显示:
末尾:
学习时间很短,代码水平有限,本文仅供学习交流;
欢迎指出其中的错误并提供你宝贵的建议!
谢谢!!