我有一个主要是调查的GUI,当调查完成后,信息将被提取到一个表中,我目前无法获取所有信息,我创建了一个名为finish处理程序的Actionlistener,它附加到调查表中GUI按钮“完成”所以当单击完成按钮时,它将提取所有信息并将其存储到我的DataStorage类中。下面是我的调查GUI,actionListener类和数据存储类。
/*********frame3 Survey*********/
frame3 = new JFrame();
frame3.setTitle("Student Survey");
frame3.setLayout(new MigLayout());
frame3.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
btnFinish = new JButton("Finish!");
btnFinish.addActionListener(new FinishHandler());
pSSFinish = new JPanel();
pSSFinish.add(btnFinish);
String[] levelStrings = { "1.1", "1.2", "2.1", "2.2", "3.1", "3.2" };
String[] DipStrings = { "Info-Communications", "Computer Engineering", "Mechatronics", "Electronics"};
jcbDip = new JComboBox(DipStrings);
jcbLvl = new JComboBox(levelStrings);
jcbDip.addActionListener(new FinishHandler());
jcbLvl.addActionListener(new FinishHandler());
//pSSInfo
pSSInfo = new JPanel();
pSSInfo.setBorder(loweredetched);
pSSInfo.setLayout(new MigLayout());
lblSSTitleName = new JLabel("Name:");
lblSSTitleID = new JLabel("Student ID:");
lblSSName = new JLabel("");
lblSSID = new JLabel("");
pSSInfo.add(lblSSTitleName);
pSSInfo.add(lblSSName,"wrap");
pSSInfo.add(lblSSTitleID);
pSSInfo.add(lblSSID);
//pSSCourse
pSSCourse = new JPanel();
pSSCourse.setBorder(titleCourse);
pSSCourse.setLayout(new MigLayout());
lblSSCourseDip = new JLabel("Diploma:");
lblSSCourseLvl = new JLabel("Level:");
lblSSCourseCre = new JLabel("No of credits:");
txfSSCourse = new JTextField(8);
pSSCourse.add(lblSSCourseDip);
pSSCourse.add(jcbLvl,"wrap");
pSSCourse.add(lblSSCourseLvl);
pSSCourse.add(jcbDip,"wrap");
pSSCourse.add(lblSSCourseCre);
pSSCourse.add(txfSSCourse);
//pSSAge
pSSAge = new JPanel();
pSSAge.setBorder(raisedbevel);
lblSSAge = new JLabel("Age");
txfSSAge = new JTextField(8);
pSSAge.add(lblSSAge);
pSSAge.add(txfSSAge);
//pSSGender
String male = "Male";
String female = "Female";
btnMale = new JRadioButton(male);
btnMale.addActionListener(new FinishHandler());
btnFemale = new JRadioButton(female);
btnFemale.addActionListener((new FinishHandler()));
ButtonGroup group = new ButtonGroup();
group.add(btnMale);
group.add(btnFemale);
pSSGender = new JPanel();
pSSGender.setBorder(titleGender);
pSSGender.setLayout(new MigLayout());
pSSGender.add(btnMale,"wrap");
pSSGender.add(btnFemale);
//pSSMisc
pSSMisc = new JPanel();
pSSMisc.setBorder(titleMisc);
pSSMisc.setLayout(new MigLayout());
cbMiscPt = new JCheckBox("Working Part Time");
cbMiscPt.addItemListener(new FinishHandler());
cbMiscPc = new JCheckBox("Have own PC/NoteBook");
cbMiscPc.addItemListener(new FinishHandler());
cbMiscChild = new JCheckBox("Have children");
cbMiscChild.addItemListener(new FinishHandler());
pSSMisc.add(cbMiscPt,"wrap");
pSSMisc.add(cbMiscPc,"wrap");
pSSMisc.add(cbMiscChild);
frame3.add(pSSFinish,"dock south");
frame3.add(pSSInfo,"dock north");
frame3.add(pSSCourse,"dock west");
frame3.add(pSSAge,"wrap, grow");
frame3.add(pSSGender);
frame3.add(pSSMisc,"dock east");
frame3.pack();
frame3.setVisible(false);
/ 完成处理程序 /
class FinishHandler implements ActionListener, ItemListener
{
public void actionPerformed(ActionEvent e)
{ //System.out.println("current login in student is: "+ds.getStudent(currentUser).getUser());
ds.getStudent(currentUser).setDiploma((String)jcbDip.getSelectedItem());
System.out.println(ds.getStudent(currentUser).getDiploma());//debug
ds.getStudent(currentUser).setLevel((String)jcbLvl.getSelectedItem());
System.out.println(ds.getStudent(currentUser).getLevel());//debug
ds.getStudent(currentUser).setCredits(txfSSCourse.getText());
System.out.println(ds.getStudent(currentUser).getCredits());//debug
ds.getStudent(currentUser).setAge(txfSSAge.getText());
System.out.println(ds.getStudent(currentUser).getAge());
if(btnMale.getText().equals("Male"))
{
ds.getStudent(currentUser).setGender("Male");
System.out.println(ds.getStudent(currentUser).getGender());//debug
}
else if(btnFemale.getText().equals("Female"))
{
ds.getStudent(currentUser).setGender("Female");
System.out.println(ds.getStudent(currentUser).getGender());//debug
}
if(btnMale.getText().equals(""))
{
ds.getStudent(currentUser).setGender("Did not set");
System.out.println(ds.getStudent(currentUser).getGender());//debug
}
}
public void itemStateChanged(ItemEvent e)
{
Object source = e.getItemSelectable();
if (source == cbMiscPt)
{
ds.getStudent(currentUser).setPartTime(true);
System.out.println(ds.getStudent(currentUser).getPartTime());//debug
}
else if (source == cbMiscPc)
{
ds.getStudent(currentUser).setHavePc(true);
System.out.println(ds.getStudent(currentUser).getHavePc());//debug
}
else if (source == cbMiscChild)
{
ds.getStudent(currentUser).setHaveChild(true);
System.out.println(ds.getStudent(currentUser).getHaveChild());//debug
}
if (e.getStateChange() == ItemEvent.DESELECTED)
{
if (source == cbMiscPt)
{
ds.getStudent(currentUser).setPartTime(false);
System.out.println(ds.getStudent(currentUser).getPartTime());//debug
}
else if (source == cbMiscPc)
{
ds.getStudent(currentUser).setHavePc(false);
System.out.println(ds.getStudent(currentUser).getHavePc());//debug
}
else if (source == cbMiscChild)
{
ds.getStudent(currentUser).setHaveChild(false);
System.out.println(ds.getStudent(currentUser).getHaveChild());//debug
}
}
}
}
/ **** DataSotrage类*** /
import java.util.*;
public class DataStorage
{
HashMap<String, Student> students = new HashMap<String, Student>();
HashMap<String, Staff> staffMembers = new HashMap<String, Staff>();
//Default constructor
public DataStorage(){
}
public void addStaffMember(Staff aAcc)
{
staffMembers.put(aAcc.getUser(),aAcc);
}
public void addStudentMember(Student aAcc)
{
students.put(aAcc.getUser(),aAcc);
}
public Staff getStaffMember(String user)
{
return staffMembers.get(user);
}
public Student getStudent(String user)
{
return students.get(user);
}
}
答案 0 :(得分:1)
你去过的设计非常健谈。当您只需将其设置为FinishHandler
时,您已为每个小部件附加了新的btnFinish
。您设置的方式,对于GUI中的每个更改,您将更新DataStorage
对象。相反,只需将其添加到btnFinish
,您就可以减少处理程序被调用的次数,并可以简化您的过程。当然,您需要整合itemStateChanged
和actionPerformed
方法。这样,您就可以选择在进入DataStorage
时初始化actionPerformed
对象,然后可以启动持久性过程。在研究将数据从内存移动到存储时,请查看SwingWorker和concurrency in Swing周围的概念。
编辑:
通过'合并您的itemStateChanged
和actionPerformed
方法,'我的意思是将您的所有数据收集复选到您执行的操作方法中。这样,当您单击完成按钮时,您可以收集DataStorage
对象所需的所有数据。