我写了这段代码 EmployeeTable.java
package com.xyz.view.employeelist;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import com.xyz.view.iframe.employeedetail.EmployeeofficeDetail;
public class EmployeeTable extends JFrame
{
private JTextField Find;
private JLabel jFind,jDepartment,jDesignation,byName,bySalary;
private JButton SearchButton,add_Employee,getList,removeEmployee,save;
private JComboBox<String> Department, Designation;
private JTable EmpTable;
private JScrollPane EmpTableScrollpane;
private JRadioButton sortByName,sortBySalary;
private ButtonGroup radioButton;
EmployeeofficeDetail e;
EmployeeTable()
{
setLayout(null);
//------------ Text Feild ---------------------------
jFind = new JLabel("Find :");
jFind.setFont(new Font("Copperplate Gothic", Font.PLAIN, 16));
jFind.setBounds(10, 20, 40, 20);
Find = new JTextField(5);
Find.setBounds(70, 20, 100, 20);
//--------------------- Search Button -------------------------
SearchButton = new JButton("Search");
SearchButton.setBounds(190,20, 80, 20);
//--------------------------- Get all list -------------------------
getList = new JButton("Get All Employee List");
getList.setBounds(160, 360, 200, 20);
//---------------------- Add new Employee button -----------------
add_Employee = new JButton("ADD Employee");
add_Employee.setBounds(10, 400, 120, 20);
//--------------------- Remove Employee Button --------------------
removeEmployee = new JButton("Remove Employee");
removeEmployee.setBounds(150, 400, 140, 20);
//---------------------- Save Button -------------------------
save = new JButton("Save");
save.setBounds(310, 400, 100, 20);
//------------------- Department Combobox ----------------------
jDepartment = new JLabel("Department");
jDepartment.setFont(new Font("Copperplate Gothic", Font.PLAIN, 16));
jDepartment.setBounds(10, 50, 90, 30);
String department[]={"Select Department","Software Development","Testing","Database","Financial","Planning","Human Resources"};
Department = new JComboBox<String>(department);
Department.setBounds(100, 50, 200, 30);
//----------------- Designation Combobox -------------------------
jDesignation = new JLabel("Designation");
jDesignation.setBounds(10, 100,90, 20);
jDesignation.setFont(new Font("Copperplate Gothic", Font.PLAIN, 16));
String post[]={"Select Designation","Junior Developer","Senior Developer","Project Manager","Manager","Tester","Trainee"};
Designation = new JComboBox<String>(post);
Designation.setBounds(110, 100, 200, 30);
//--------------------- Table -------------------------------
Object[] column = {"ID","Name","salary","Designation","Department","Date of Joining"};
Object[][] data = {{1,"amit"},{2,"raj"},{3,"ram"},{4,"rina"},{5,"sara"}};
TableModel emp = new DefaultTableModel(data, column);
EmpTable = new JTable(emp);
EmpTableScrollpane = new JScrollPane(EmpTable);
EmpTableScrollpane.setBounds(10, 140, 480, 200);
//--------------------------- Radio Buttons -------------------------
radioButton = new ButtonGroup();
sortByName = new JRadioButton("Sort Table By Name");
sortByName.setBounds(10, 430, 160, 20);
sortBySalary = new JRadioButton("Sort Table By Salary");
sortBySalary.setBounds(200, 430, 160, 20);
add(jFind);
add(Find);
add(SearchButton);
add(jDepartment);
add(Department);
add(jDesignation);
add(Designation);
add(EmpTableScrollpane);
add(getList);
add(add_Employee);
add(removeEmployee);
add(save);
radioButton.add(sortByName);
radioButton.add(sortBySalary);
add(sortByName);
add(sortBySalary);
add_Employee.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent a) {
if(a.getSource()==add_Employee)
{
e = new EmployeeofficeDetail();
e.setTitle("Registration Form");
e.setVisible(true);
}
}
});
}
public static void main(String args[])
{
JFrame f=new EmployeeTable();
f.setVisible(true);;
f.setSize(500, 500);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
和EmployeeOfficedetail.java
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class EmployeeofficeDetail extends JInternalFrame
{
private JTextField name;
private JLabel jName;
private JButton jSave,jNext;
private JComboBox<String> jDepartment;
public EmployeeofficeDetail()
{
jName = new JLabel("Employee Name");
name = new JTextField();
jSave = new JButton();
jDepartment = new JComboBox<String>();
add(jName);
add(name);
add(jSave);
add(jDepartment);
}
}
我想在点击添加员工按钮时打开EmployeeOfficedetail框架,以便可用的框架消失,但我的代码无效。以及如何在我的代码中为combobox和radiobutton编写actionlistener,因此可以相应地对表值进行排序。例如,如果我按名称选择单选按钮排序,它将根据名称对表进行排序。