一、实验目的
1.掌握Java中设计GUI界面的方法
2.掌握Java GUI界面上的事件监听机制
3.掌握GUI类与问题域交互的方法
二、实验内容
1. 设计Manager类、GUI类及事件处理
(1)设计一个Manager类并编写代码, Manager类的属性有姓名,工号,基本工资,小时工资(元/小时)。自定义方法:至少包括计算月工资的方法:calSalary()。
(2)编写一个GUI类,输入manager的姓名、工号,基本工资,月工作时间(小时),创建对象,调用calSalary()方法计算出该manager的月工资,并显示在用户界面上。
运行结果:
源码:
package managerGUI;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;import javax.swing.*;public class ManagerGUI {public static void main(String[] args) {// TODO Auto-generated method stubnew GUI();}}class GUI extends JFrame implements ActionListener {// 继承JFrame及接口private String name;private String ID;private String basic_salary;private String hourly_salary;private String monthly_salary;// 定义组件private JLabel jl1, jl2, jl3, jl4, jl5;// 定义5个标签private JTextField jt1, jt2, jt3, jt4, jt5;// 定义6个文本框private JTextArea jt6;private JButton jb1, jb2;// 两个按钮private JPanel jp1, jp2, jp3, jp4, jp5;// 中间容器// 构造方法public GUI() {// 创建组件jl1 = new JLabel("name");jl2 = new JLabel("empNo");jl3 = new JLabel("baseSalsry");jl4 = new JLabel("wageOfHour");jl5 = new JLabel("wageOfMonthly");jb1 = new JButton("calculate");jb2 = new JButton("cancel");jt1 = new JTextField(10);jt2 = new JTextField(10);jt3 = new JTextField(10);jt4 = new JTextField(10);jt5 = new JTextField(10);jt6 = new JTextArea();jp1 = new JPanel();jp2 = new JPanel();jp3 = new JPanel();jp4 = new JPanel();jp5 = new JPanel();// 布局管理// jt6.setHorizontalAlignment(JTextField.LEFT);jt6.setText("result:");jp5.setLayout(new GridLayout(3, 1, 10, 10));this.setLayout(new GridLayout(3, 1, 10, 10));// GridLayout(几行,几列,左右间距,上下间距)jp1.add(jl1);jp1.add(jt1);jp1.add(jl2);jp1.add(jt2);jp2.add(jl3);jp2.add(jt3);jp2.add(jl4);jp2.add(jt4);jp3.add(jl5);jp3.add(jt5);jp4.add(jb1);jp4.add(jb2);jp5.add(jp2, BorderLayout.NORTH);jp5.add(jp3, BorderLayout.CENTER);jp5.add(jp4, BorderLayout.SOUTH);// 监听注册jb1.addActionListener(this);jb2.addActionListener(this);// 设置窗口this.add(jp1, BorderLayout.NORTH);this.add(jp5, BorderLayout.CENTER);this.add(jt6, BorderLayout.SOUTH);this.setTitle("calculate of Salary");this.setSize(400, 500);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif (e.getSource() == jb1)jt6.setText(calSalary());else if (e.getSource() == jb2)cancel();}public String calSalary() {this.name = jt1.getText();this.ID = jt2.getText();this.basic_salary = jt3.getText();this.hourly_salary = jt4.getText();this.monthly_salary = jt5.getText();return "result:" + "\n" + "name:" + name + "\n" + "empNo:" + ID + "\n" + "baseSalsry:" + basic_salary + "\n"+ "wageOfHour:" + hourly_salary + "\n" + "wageOfMonthly:" + monthly_salary + "\n" + "calSalary:"+ String.valueOf(Double.parseDouble(basic_salary)+ Double.parseDouble(monthly_salary) * Double.parseDouble(hourly_salary));}public void cancel() {this.name = "无";this.ID = "无";this.basic_salary = "0";this.hourly_salary = "0";this.monthly_salary = "0";jt1.setText("");jt2.setText("");jt3.setText("");jt4.setText("");jt5.setText("");}
}
2 用户登录-GUI及事件响应
编写一个JFrame框架应用程序,如图,并根据登录情况显示相应的提示信息。假设用户名为admin,密码为admin。
事件响应:输入正确的用户名和密码,系统提示“Seccussful!Username and password is correct!”,否则提示“Username or password isn’t correct!”
说明:两个消息提示框不需要自己编写界面类,使用JOptionPane类调用showMessageDialog()
运行结果:
源码:
package userGUICome;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.*;public class UserGUIDome {public static void main(String[] args) {// TODO Auto-generated method stubnew UserGUI();}}class UserGUI extends JFrame implements ActionListener {// 定义加创建private JLabel jl1, jl2;private JTextField jt1;private JPasswordField jt2;private JButton jb1, jb2;private JPanel jp1, jp2, jp3;public UserGUI() {jl1 = new JLabel("UserName");jl2 = new JLabel("Password");jt1 = new JTextField(10);jt2 = new JPasswordField(10);jb1 = new JButton("Login");jb2 = new JButton("Cancel");jp1 = new JPanel();jp2 = new JPanel();jp3 = new JPanel();// 设置字体Font font = new Font("楷体", Font.BOLD, 18);jl1.setFont(font);jl2.setFont(font);jb1.setFont(font);jb2.setFont(font);// 美化字的颜色Color color = new Color(0, 0, 255);jl1.setForeground(color.blue);jl2.setForeground(color);jb1.setForeground(color);jb2.setForeground(color);jp1.add(jl1);jp1.add(jt1);jp2.add(jl2);jp2.add(jt2);jp3.add(jb1);jp3.add(jb2);// 设置图标ImageIcon icon = new ImageIcon("src\\image\\QQ头像.png");//在JAVA的src文件夹里包含image文件其中有QQ头像.png照片this.setIconImage(icon.getImage());// 监听注册jb1.addActionListener(this);jb2.addActionListener(this);// 设置布局,添加组件this.setLayout(new GridLayout(3, 1));this.add(jp1);this.add(jp2);this.add(jp3);// 设置窗口this.setTitle("Login");this.setSize(300, 180);this.setLocation(400, 400);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif (e.getSource() == jb1)Login();else if (e.getSource() == jb2)clear1();}public void Login() {String userName = "admin";String password = "admin";String un = jt1.getText().trim();// 去除前后空格String pw = String.valueOf(jt2.getPassword()).trim();if (un.equals(userName) && pw.equals(password))JOptionPane.showMessageDialog(null, "Seccussful!\nUsername and password is correct!", "notice", 1);else {JOptionPane.showMessageDialog(null, "Username or password isn't correct!", "notice", 2);clear1();}}public void clear1() {jt1.setText("");jt2.setText("");}
}
END:学习不论方法