JAVA 设计Manager类、GUI类及事件处理 (1)设计一个Manager类并编写代码, Manager类的属性有姓名,工号,基本工资,小时工资(元/小时)。自定义方法:至少包括计算月工资的方法:calSalary()。 (2)编写一个GUI类,输入manager的姓名、工号,基本工资,月工作时间(小时),创建对象,调用calSalary()方法计算出该manager的月工资,并显示在用户界面上。
时间: 2024-02-22 10:57:34 浏览: 69
好的,我会根据您的要求进行解答。下面是代码实现:
Manager.java
```
public class Manager {
private String name;
private String id;
private double basicSalary;
private double hourSalary;
public Manager(String name, String id, double basicSalary, double hourSalary) {
this.name = name;
this.id = id;
this.basicSalary = basicSalary;
this.hourSalary = hourSalary;
}
public double calSalary(double workHours) {
return basicSalary + workHours * hourSalary;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public double getBasicSalary() {
return basicSalary;
}
public double getHourSalary() {
return hourSalary;
}
}
```
GUI.java
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JFrame implements ActionListener {
private JLabel nameLabel;
private JLabel idLabel;
private JLabel basicSalaryLabel;
private JLabel workHoursLabel;
private JLabel salaryLabel;
private JTextField nameField;
private JTextField idField;
private JTextField basicSalaryField;
private JTextField workHoursField;
private JButton calButton;
private Manager manager;
public GUI() {
setTitle("Manager");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(5, 2));
nameLabel = new JLabel("Name:");
idLabel = new JLabel("ID:");
basicSalaryLabel = new JLabel("Basic Salary:");
workHoursLabel = new JLabel("Work Hours:");
salaryLabel = new JLabel("Salary:");
nameField = new JTextField(10);
idField = new JTextField(10);
basicSalaryField = new JTextField(10);
workHoursField = new JTextField(10);
calButton = new JButton("Calculate Salary");
calButton.addActionListener(this);
add(nameLabel);
add(nameField);
add(idLabel);
add(idField);
add(basicSalaryLabel);
add(basicSalaryField);
add(workHoursLabel);
add(workHoursField);
add(calButton);
add(salaryLabel);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String id = idField.getText();
double basicSalary = Double.parseDouble(basicSalaryField.getText());
double workHours = Double.parseDouble(workHoursField.getText());
manager = new Manager(name, id, basicSalary, 50);
double salary = manager.calSalary(workHours);
salaryLabel.setText("Salary: " + salary);
}
public static void main(String[] args) {
new GUI();
}
}
```
上述代码实现了一个简单的 GUI 界面,用户可以输入经理的姓名、工号、基本工资和月工作时间,然后调用 Manager 类的 calSalary() 方法计算月工资并将结果显示在界面上。
阅读全文