📄 employeetest.java
字号:
/*=========================================================================== PLEASE NOTE!!! Do not edit any of this support code. Please scroll down to see the code you need to edit for this lab.===========================================================================*/import java.awt.*;import java.awt.event.*;public class EmployeeTest extends Panel implements ActionListener, ItemListener{ private java.util.Vector employees = new java.util.Vector(); // GUI Components private List employeeList = new List(); private Label nameLabel = new Label("Name:"); private Label ageLabel = new Label("Age:"); private TextField nameField = new TextField(); private TextField ageField = new TextField(); private Button addButton = new Button("Add Employee"); private Button modifyButton = new Button("Modify Employee"); private Button incButton = new Button("Inc Age"); private Button decButton = new Button("Dec Age"); public EmployeeTest() { setLayout(null); setupGUI(); } private void setupGUI() { setupEdit(); setupList(); } private void setupEdit() { add(nameLabel); add(nameField); add(ageLabel); add(ageField); add(addButton); add(modifyButton); nameLabel.setSize(50,24); nameLabel.setLocation(20,20); nameField.setSize(150,24); nameField.setLocation(75,20); ageLabel.setSize(50,24); ageLabel.setLocation(20,50); ageField.setSize(50,24); ageField.setLocation(75,50); addButton.setSize(120,24); addButton.setLocation(20,80); addButton.addActionListener(this); modifyButton.setSize(150,24); modifyButton.setLocation(150,80); modifyButton.setEnabled(false); modifyButton.addActionListener(this); } private void setupList() { add(employeeList); add(incButton); add(decButton); employeeList.setSize(200,100); employeeList.setLocation(20,140); employeeList.addItemListener(this); incButton.setSize(100,24); incButton.setLocation(20,250); incButton.setEnabled(false); incButton.addActionListener(this); decButton.setSize(100,24); decButton.setLocation(130,250); decButton.setEnabled(false); decButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource()==addButton) { Employee anEmployee = setupEmployee(nameField.getText(), Integer.parseInt(ageField.getText())); String displayString = setupDisplayString(anEmployee); employeeList.add(displayString); employees.addElement(anEmployee); clearFields(); } else if (e.getSource()==modifyButton) { int index = employeeList.getSelectedIndex(); Employee anEmployee = (Employee) employees.elementAt(index); updateEmployee(anEmployee, nameField.getText(), Integer.parseInt(ageField.getText())); String displayString = setupDisplayString(anEmployee); employeeList.replaceItem(displayString, index); employeeList.select(index); } else if (e.getSource()==incButton) { int index = employeeList.getSelectedIndex(); Employee anEmployee = (Employee) employees.elementAt(index); incEmployee(anEmployee); String displayString = setupDisplayString(anEmployee); employeeList.replaceItem(displayString, index); employeeList.select(index); } else if (e.getSource()==decButton) { int index = employeeList.getSelectedIndex(); Employee anEmployee = (Employee) employees.elementAt(index); decEmployee(anEmployee); String displayString = setupDisplayString(anEmployee); employeeList.replaceItem(displayString, index); employeeList.select(index); } } public void itemStateChanged(ItemEvent e) { int index = employeeList.getSelectedIndex(); if (index >= 0) { Employee anEmployee = (Employee) employees.elementAt(index); nameField.setText(anEmployee.getName()); ageField.setText(" " + anEmployee.getAge()); incButton.setEnabled(true); decButton.setEnabled(true); modifyButton.setEnabled(true); } else { clearFields(); incButton.setEnabled(false); decButton.setEnabled(false); modifyButton.setEnabled(false); } } private void clearFields() { nameField.setText(""); ageField.setText(""); } public static void main(String[] args) { Frame aFrame = new Frame("EmployeeTest"); aFrame.setSize(320,300); aFrame.setLocation(100,100); EmployeeTest aTest = new EmployeeTest(); aFrame.add(aTest); aFrame.setVisible(true); }/*=========================================================================== DO NOT EDIT ABOVE THIS LINE. However, you can look to see if you can understand what the support code does. Fill in the methods below.===========================================================================*/// When the "Add" button is pressed, this method is invoked. It is passed // the name and the age as entered in GUI. This method must create a new// instance of employee using the supplied values. The newly created employee// object is then to be returned. public Employee setupEmployee(String aName, int anAge) { // Create a new Instance of Employee. Initialize it with the name and // age supplied. Employee temp = new Employee(aName,anAge); // Return the newly created employee object; return temp; }// This method is invoked every time an employee object must be displayed in// the list. This method should create a display string using the data// obtained from the employee object parameter. public String setupDisplayString(Employee anEmployee) { // Create a string and assign the display text to it. The display text // should be created using data supplied by the employee object passed into // this method. String info = "Name = " + anEmployee.getName() + "," + "age = " + anEmployee.getAge() + ";"; return info; }// This method is invoked every time the modify employee button is pressed.// This method must replace the values of the employee parameter with the // values passed to the method. public void updateEmployee(Employee anEmployee, String aNewName, int aNewAge) { // Update the name of the employee anEmployee.setName(aNewName); // Update the age of the employee anEmployee.setAge(aNewAge); }// This method is invoked when the "Increment age" Button is pressed. You must// increment the age of the employee object passed as a parameter public void incEmployee(Employee anEmployee) { // Increment the age of the employee anEmployee.incAge(); }// This method is invoked when the "Decrement age" Button is pressed. You must// decrement the age of the employee object passed as a parameter public void decEmployee(Employee anEmployee) { // Decrement the age of the employee anEmployee.decAge(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -