1. 设计Manager类、GUI类及事件处理 (1)设计一个Manager类并编写代码, Manager类的属性有姓名,工号,基本工资,小时工资(元/小时)。自定义方法:至少包括计算月工资的方法:calSalary()。 (2)编写一个GUI类,输入manager的姓名、工号,基本工资,月工作时间(小时),创建对象,调用calSalary()方法计算出该manager的月工资,并显示在用户界面上。 2 用户登录-GUI及事件响应 编写一个JFrame框架应用程序,如图,并根据登录情况显示相应的提示信息。假设用户名为admin,密码为admin。 事件响应:输入正确的用户名和密码,系统提示“Seccussful!Username and password is correct!”,否则提示“Username or password isn't correct!” 说明:两个消息提示框不需要自己编写界面类,使用JOptionPane类调用showMessageDialog()
时间: 2023-12-03 18:44:52 浏览: 84
1. Manager类的代码如下:
```java
public class Manager {
private String name;
private String id;
private double baseSalary;
private double hourlyWage;
public Manager(String name, String id, double baseSalary, double hourlyWage) {
this.name = name;
this.id = id;
this.baseSalary = baseSalary;
this.hourlyWage = hourlyWage;
}
public double calSalary(double workHours) {
return baseSalary + hourlyWage * workHours;
}
// getter and setter methods for all attributes
}
```
GUI类的代码如下:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ManagerGUI extends JFrame {
private JLabel nameLabel;
private JTextField nameField;
private JLabel idLabel;
private JTextField idField;
private JLabel baseSalaryLabel;
private JTextField baseSalaryField;
private JLabel workHoursLabel;
private JTextField workHoursField;
private JButton calculateButton;
private JLabel resultLabel;
public ManagerGUI() {
init();
addComponents();
addListeners();
}
private void init() {
setTitle("Manager Salary Calculator");
setSize(400, 250);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addComponents() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 2));
nameLabel = new JLabel("Name:");
nameField = new JTextField();
idLabel = new JLabel("ID:");
idField = new JTextField();
baseSalaryLabel = new JLabel("Base Salary:");
baseSalaryField = new JTextField();
workHoursLabel = new JLabel("Work Hours:");
workHoursField = new JTextField();
calculateButton = new JButton("Calculate");
resultLabel = new JLabel();
panel.add(nameLabel);
panel.add(nameField);
panel.add(idLabel);
panel.add(idField);
panel.add(baseSalaryLabel);
panel.add(baseSalaryField);
panel.add(workHoursLabel);
panel.add(workHoursField);
panel.add(calculateButton);
panel.add(resultLabel);
add(panel);
}
private void addListeners() {
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String id = idField.getText();
double baseSalary = Double.parseDouble(baseSalaryField.getText());
double workHours = Double.parseDouble(workHoursField.getText());
Manager manager = new Manager(name, id, baseSalary, 50.0);
double salary = manager.calSalary(workHours);
resultLabel.setText("Salary: " + salary);
}
});
}
}
```
2. 用户登录GUI及事件响应的代码如下:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginGUI extends JFrame {
private JLabel usernameLabel;
private JTextField usernameField;
private JLabel passwordLabel;
private JPasswordField passwordField;
private JButton loginButton;
public LoginGUI() {
init();
addComponents();
addListeners();
}
private void init() {
setTitle("Login");
setSize(300, 150);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addComponents() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
usernameLabel = new JLabel("Username:");
usernameField = new JTextField();
passwordLabel = new JLabel("Password:");
passwordField = new JPasswordField();
loginButton = new JButton("Login");
panel.add(usernameLabel);
panel.add(usernameField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(loginButton);
add(panel);
}
private void addListeners() {
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (username.equals("admin") && password.equals("admin")) {
JOptionPane.showMessageDialog(null, "Seccussful! Username and password is correct!");
} else {
JOptionPane.showMessageDialog(null, "Username or password isn't correct!");
}
}
});
}
}
```
阅读全文