- (0分)
设计Manager类、GUI类及事件处理
题目描述
(1)设计一个Manager类并编写代
码, Manager类的属性有姓名,
工号,基本工资,小时工资(元/小时)。自定义方法:至少包括计算月工资的方法:calSalary()。
(2)编写一个GUI类,输入manager的姓名、工号,基本工资,月工作时间(小时),创建对象,调用calSalary()方法计算出该manager的月工资,并显示在用户界面上。
*/
package test01_manager;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ManageDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
new ManagerGUI();
}
}
class Manager {
private String name;
private String ID;
private double hourNum;
private double basicPrice;
private double hourPrice;
public Manager(String name, String ID, double hourNum, double basicPrice, double hourPrice) {
this.name = name;
this.ID = ID;
this.hourNum = hourNum;
this.basicPrice = basicPrice;
this.hourPrice = hourPrice;
}
public double calSalary() {
return basicPrice + hourPrice * hourNum;
}
}
class ManagerGUI extends JFrame implements ActionListener {
// 定义组件
JLabel nameLabel, IDLabel, hourNumLabel, basicPriceLabel, hourPriceLabel, priceLabel;
JTextField nameTextField, IDTextField, hourNumTextField, basicPriceTextField, hourPriceTextField, priceTextField;
JPanel namePanel, IDPanel, hourNumPanel, basicPricePanel, hourPricePanel, pricePanel;
JButton calSalaryButton;
public ManagerGUI() {
nameLabel = new JLabel("姓名");
IDLabel = new JLabel("工号");
hourNumLabel = new JLabel("小时数");
basicPriceLabel = new JLabel("基本工资");
hourPriceLabel = new JLabel("小时工资");
priceLabel = new JLabel("总工资");
nameTextField = new JTextField(10);
IDTextField = new JTextField(10);
hourNumTextField = new JTextField(10);
basicPriceTextField = new JTextField(10);
hourPriceTextField = new JTextField(10);
priceTextField = new JTextField(10);
namePanel = new JPanel();
IDPanel = new JPanel();
hourNumPanel = new JPanel();
basicPricePanel = new JPanel();
hourPricePanel = new JPanel();
pricePanel = new JPanel();
calSalaryButton = new JButton("计算");
// 添加绑定
calSalaryButton.addActionListener(this);
namePanel.add(nameLabel);
namePanel.add(nameTextField);
IDPanel.add(IDLabel);
IDPanel.add(IDTextField);
hourNumPanel.add(hourNumLabel);
hourNumPanel.add(hourNumTextField);
basicPricePanel.add(basicPriceLabel);
basicPricePanel.add(basicPriceTextField);
hourPricePanel.add(hourPriceLabel);
hourPricePanel.add(hourPriceTextField);
pricePanel.add(priceLabel);
pricePanel.add(priceTextField);
this.setLayout(new GridLayout(7, 1, 5, 5));
this.add(namePanel);
this.add(IDPanel);
this.add(hourNumPanel);
this.add(basicPricePanel);
this.add(hourPricePanel);
this.add(calSalaryButton);
this.add(pricePanel);
this.setTitle("Manager");
this.setSize(300, 250);
// 设置窗体关闭时候,保证JVM要退出就是控制台
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 获取你的屏幕的宽和高
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
// 然后设置你编写的窗口的初始位置,也就是在中间,
this.setLocation(width / 2 - 200, height / 2 - 150);
// 显示
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == calSalaryButton) {
String name = nameTextField.getText();
String ID = IDTextField.getText();
double hourNum = Double.parseDouble(hourNumTextField.getText());
double basicPrice = Double.parseDouble(basicPriceTextField.getText());
double hourPrice = Double.parseDouble(hourPriceTextField.getText());
Manager manager = new Manager(name, ID, hourNum, basicPrice, hourPrice);
double price = manager.calSalary();
priceTextField.setText(String.valueOf(price));
}
}
}