Java 如何使用JFileChooser保存/打开对象的ArrayList?
好吧,我对Java编程有点陌生,我不太理解文件读/写的概念。我试着查看docs.oracle网站上关于文件的页面,但我发现它们并没有真正的帮助,因为我正在尝试做的是不同的 我有这3个文件:CVolunteer、CDialog和TestDialog。CVolunteer创建了一个可以自愿辅导学生的人的目标。CDialog管理志愿者的添加/编辑。TestDialog显示志愿者列表,允许用户编辑、添加、删除或清除列表。我有这3个类工作得很好,并将在下面显示它们(对不起,它们太长了!) 这是我需要帮助的。。。我在主窗口中添加了两个按钮,“保存”和“打开”。在任何时候,用户都应该能够保存当前的志愿者名单。单击“保存”时,会弹出一个JFileChooser窗口,要求用户提供一个文件名,所有志愿者对象都将保存在该文件名中。当用户单击“打开”时,会弹出另一个JFileChooser窗口,询问用户要打开的文件。当前在主窗口中的志愿者将被删除,所选文件中的志愿者将取代他们的位置。我不确定是否需要使用序列化 如果有人能帮我解释如何完成这项任务,或者帮我编写代码来处理“保存”/“打开”事件,我将不胜感激!提前感谢:) 我相信唯一需要更改的文件是TestDialog,我包括了其他文件,以防有人想要运行它 ***很抱歉,如果有任何缩进错误,我必须在这个对话框中手动完成 CVolunteer.javaJava 如何使用JFileChooser保存/打开对象的ArrayList?,java,file-io,arraylist,jfilechooser,Java,File Io,Arraylist,Jfilechooser,好吧,我对Java编程有点陌生,我不太理解文件读/写的概念。我试着查看docs.oracle网站上关于文件的页面,但我发现它们并没有真正的帮助,因为我正在尝试做的是不同的 我有这3个文件:CVolunteer、CDialog和TestDialog。CVolunteer创建了一个可以自愿辅导学生的人的目标。CDialog管理志愿者的添加/编辑。TestDialog显示志愿者列表,允许用户编辑、添加、删除或清除列表。我有这3个类工作得很好,并将在下面显示它们(对不起,它们太长了!) 这是我需要帮助的
public class CVolunteer {
int volNum;
String name;
int sub, volDays, trans;
public CVolunteer(int vNum, String vName, int subj, int days, int needTrans){
volNum = vNum;
name = vName;
sub = subj;
volDays = days;
trans = needTrans;
}
private String subjectToString()
{
switch (sub){
case 0:
return "Math";
case 1:
return "Science";
case 2:
return "English";
case 3:
return "History";
}
return " ";
}
private String volDaysToString()
{
String str = "";
str +=((volDays&1)!=0)?"M":"-";
str +=((volDays&2)!=0)?"T":"-";
str +=((volDays&4)!=0)?"W":"-";
str +=((volDays&8)!=0)?"R":"-";
return str;
}
private String transToString()
{
switch(trans)
{
case 0:
return "Yes";
case 1:
return "No";
}
return " ";
}
public String getVolunteerLine()
{
return String.format("%05d %-30s%-30s%-30s%s",
volNum, name, subjectToString(), volDaysToString(), transToString());
}
}
CDialog.java
import java.awt.Container;
import java.awt.event.*;
import javax.swing.*;
public class CDialog extends JDialog implements ActionListener
{
private JLabel label1;
private JLabel lNum;
private JLabel label2;
private JTextField tfName;
private JLabel label3;
private ButtonGroup subGroup;
private JRadioButton rbMath;
private JRadioButton rbScience;
private JRadioButton rbEnglish;
private JRadioButton rbHistory;
private JLabel label4;
private JCheckBox cbMonday;
private JCheckBox cbTuesday;
private JCheckBox cbWednesday;
private JCheckBox cbThursday;
private JLabel label5;
private ButtonGroup transGroup;
private JRadioButton rbYes;
private JRadioButton rbNo;
private JButton okButton = null;
private JButton cancelButton = null;
private boolean cancelled = true;
public boolean isCancelled() {return cancelled;}
private CVolunteer answer;
public CVolunteer getAnswer() {return answer;}
public CDialog(JFrame owner, String title, CVolunteer vol)
{
super(owner, title, true);
Container c = getContentPane();
c.setLayout(null);
label1 = new JLabel ("Volunteer Number:");
label1.setSize(140,20);
label1.setLocation(40,40);
c.add(label1);
lNum = new JLabel(String.format("%05d", vol.volNum));
lNum.setSize(40,20);
lNum.setLocation(150,40);
c.add(lNum);
label2 = new JLabel ("Volunteer Name: ");
label2.setSize(100,20);
label2.setLocation(40,90);
c.add(label2);
tfName = new JTextField(vol.name);
tfName.setSize(120,20);
tfName.setLocation(140,90);
c.add(tfName);
int x,y;
int w,h;
x=4;
y=150;
w=180;
h=20;
label3 = new JLabel("Subject: ");
label3.setSize(85,13);
label3.setLocation(x,y);
c.add(label3);
rbMath = new JRadioButton("Math", vol.sub==0);
rbMath.setSize(w,h);
rbMath.setLocation(x+16,y+30);
c.add(rbMath);
rbScience = new JRadioButton("Science", vol.sub==1);
rbScience.setSize(w,h);
rbScience.setLocation(x+16,y+66);
c.add(rbScience);
rbEnglish = new JRadioButton("English", vol.sub==2);
rbEnglish.setSize(w,h);
rbEnglish.setLocation(x+16,y+102);
c.add(rbEnglish);
rbHistory = new JRadioButton("History", vol.sub==3);
rbHistory.setSize(w,h);
rbHistory.setLocation(x+16,y+138);
c.add(rbHistory);
subGroup = new ButtonGroup();
subGroup.add(rbMath);
subGroup.add(rbScience);
subGroup.add(rbEnglish);
subGroup.add(rbHistory);
x=220;
y=150;
w=120;
h=20;
label4 = new JLabel("Days Available: ");
label4.setSize(w,h);
label4.setLocation(x,y);
c.add(label4);
cbMonday = new JCheckBox("Monday (M)", (vol.volDays&1)!=0);
cbMonday.setSize(w,h);
cbMonday.setLocation(x+6,y+30);
c.add(cbMonday);
cbTuesday = new JCheckBox("Tuesday (T)", (vol.volDays&2)!=0);
cbTuesday.setSize(w,h);
cbTuesday.setLocation(x+6,y+66);
c.add(cbTuesday);
cbWednesday = new JCheckBox("Wednesday (W)", (vol.volDays&4)!=0);
cbWednesday.setSize(w,h);
cbWednesday.setLocation(x+6,y+102);
c.add(cbWednesday);
cbThursday = new JCheckBox("Thursday (R)", (vol.volDays&8)!=0);
cbThursday.setSize(w,h);
cbThursday.setLocation(x+6,y+138);
c.add(cbThursday);
x=480;
y=150;
w=180;
h=20;
label5 = new JLabel("Need Transport? :");
label5.setSize(150,13);
label5.setLocation(x,y);
c.add(label5);
rbYes = new JRadioButton("Yes", vol.trans==0);
rbYes.setSize(w,h);
rbYes.setLocation(x+12,y+30);
c.add(rbYes);
rbNo = new JRadioButton("No", vol.trans==1);
rbNo.setSize(w,h);
rbNo.setLocation(x+12,y+66);
c.add(rbNo);
transGroup = new ButtonGroup();
transGroup.add(rbYes);
transGroup.add(rbNo);
cancelButton = new JButton("Cancel");
cancelButton.addActionListener(this);
cancelButton.setSize(100,50);
cancelButton.setLocation(116,380);
c.add(cancelButton);
okButton = new JButton("OK");
okButton.addActionListener(this);
okButton.setSize(100,50);
okButton.setLocation(400,380);
c.add(okButton);
setSize(700,480);
setLocationRelativeTo(owner);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==okButton) {
int num=Integer.parseInt(lNum.getText());
String name=tfName.getText();
int subj=-1;
if (rbMath.isSelected()) subj = 0;
if (rbScience.isSelected()) subj = 1;
if (rbEnglish.isSelected()) subj = 2;
if (rbHistory.isSelected()) subj = 3;
int days=0;
if (cbMonday.isSelected()) days |= 1;
if (cbTuesday.isSelected()) days |= 2;
if (cbWednesday.isSelected()) days |= 4;
if (cbThursday.isSelected()) days |= 8;
int tran=0;
if (rbYes.isSelected()) tran = 0;
if (rbNo.isSelected()) tran = 1;
answer=new CVolunteer(num, name, subj, days, tran);
cancelled = false;
setVisible(false);
}
else if(e.getSource()==cancelButton) {
cancelled = true;
setVisible(false);
}
}
}
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
public class TestDialog extends JFrame implements ActionListener
{
JLabel myLabel1 = null;
JLabel myLabel2 = null;
JLabel myLabel3 = null;
JLabel myLabel4 = null;
JLabel myLabel5 = null;
JLabel myLabel6 = null;
File fileName = new File("Volunteers.txt");
ArrayList<CVolunteer> volArray;
private DefaultListModel volunteers;
JList volList;
JScrollPane scrollPane = null;
JButton bAdd = null;
JButton bEdit = null;
JButton bRemove = null;
JButton bClear = null;
JButton bSave = null;
JButton bOpen = null;
int volNumb;
public TestDialog()
{
super("Volunteer Info");
Container c = getContentPane();
c.setLayout(null);
myLabel1 = new JLabel("Vol Number");
myLabel1.setSize(200,50);
myLabel1.setLocation(100,10);
c.add(myLabel1);
myLabel2 = new JLabel("Vol Name");
myLabel2.setSize( 200, 50 );
myLabel2.setLocation( 200, 10 );
c.add(myLabel2);
myLabel3 = new JLabel("Subject");
myLabel3.setSize( 200, 50 );
myLabel3.setLocation( 310, 10);
c.add(myLabel3);
myLabel4 = new JLabel("Vol Days");
myLabel4.setSize( 200, 50 );
myLabel4.setLocation( 400, 10 );
c.add(myLabel4);
myLabel5 = new JLabel("Transport");
myLabel5.setSize( 200, 50 );
myLabel5.setLocation( 500, 10 );
c.add(myLabel5);
volArray = new ArrayList<CVolunteer>();
volunteers = new DefaultListModel();
volList = new JList(volunteers);
volList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollPane = new JScrollPane(volList);
scrollPane.setSize(500,300);
scrollPane.setLocation(100,50);
c.add(scrollPane);
bAdd = new JButton("Add");
bAdd.setSize( 100, 50 );
bAdd.setLocation( 20, 400 );
bAdd.addActionListener(this);
c.add(bAdd);
bEdit = new JButton("Edit");
bEdit.setSize( 100, 50 );
bEdit.setLocation( 150, 400 );
bEdit.addActionListener(this);
c.add(bEdit);
bRemove = new JButton("Remove");
bRemove.setSize( 100, 50 );
bRemove.setLocation( 280, 400 );
bRemove.addActionListener(this);
c.add(bRemove);
bClear = new JButton("Clear");
bClear.setSize( 100, 50 );
bClear.setLocation( 410, 400 );
bClear.addActionListener(this);
c.add(bClear);
bSave = new JButton("Save");
bSave.setSize( 100, 50 );
bSave.setLocation( 540, 400 );
bSave.addActionListener(this);
c.add(bSave);
bOpen = new JButton("Open");
bOpen.setSize( 100, 50 );
bOpen.setLocation( 670, 400 );
bOpen.addActionListener(this);
c.add(bOpen);
setSize( 800, 600 );
setLocation( 100, 100 );
setVisible(true);
volNumb = 0;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bAdd) {
volNumb++;
CVolunteer defaultVol = new CVolunteer(volNumb, "", 1, 0, 0);
CDialog dialogWnd = new CDialog(this, "Add a Volunteer", defaultVol);
if (!dialogWnd.isCancelled()) {
volArray.add(dialogWnd.getAnswer());
volunteers.addElement(dialogWnd.getAnswer().getVolunteerLine());
volList.setSelectedIndex(volunteers.size()-1);
volList.ensureIndexIsVisible(volunteers.size()-1);
}
}
else if(e.getSource()==bEdit) {
int index=volList.getSelectedIndex();
if (index>=0) {
CDialog dialogWnd = new CDialog (this, "Edit a Volunteer", volArray.get(index));
if (!dialogWnd.isCancelled()) {
volArray.set(index, dialogWnd.getAnswer());
volunteers.set(index, dialogWnd.getAnswer().getVolunteerLine());
}
}
}
else if(e.getSource()==bRemove) {
int index=volList.getSelectedIndex();
if (index>=0) {
volArray.remove(index);
volunteers.remove(index);
if (volunteers.size()>0) {
if (index==volunteers.size()) {
index--;
}
volList.setSelectedIndex(index);
volList.ensureIndexIsVisible(index);
}
}
}
else if(e.getSource()==bClear) {
volArray.clear();
volunteers.clear();
}
else if (e.getSource()==bSave)
{
//my sorry attempt at writing a file.. ignore this!
try {
FileWriter fw = new FileWriter(fileName);
Writer output = new BufferedWriter(fw);
int size = volArray.size();
for (int i = 0; i<size; i++)
{
output.write(volArray.get(i).getVolunteerLine() + "\n");
}
output.close();
}
catch (Exception e1) {
// TODO Auto-generated catch block
}
final JFileChooser fc = new JFileChooser();
}
else if (e.getSource()==bOpen)
{
}
}
public static void main(String[] args) {
TestDialog mainWnd = new TestDialog();
}
}
TestDialog.java
import java.awt.Container;
import java.awt.event.*;
import javax.swing.*;
public class CDialog extends JDialog implements ActionListener
{
private JLabel label1;
private JLabel lNum;
private JLabel label2;
private JTextField tfName;
private JLabel label3;
private ButtonGroup subGroup;
private JRadioButton rbMath;
private JRadioButton rbScience;
private JRadioButton rbEnglish;
private JRadioButton rbHistory;
private JLabel label4;
private JCheckBox cbMonday;
private JCheckBox cbTuesday;
private JCheckBox cbWednesday;
private JCheckBox cbThursday;
private JLabel label5;
private ButtonGroup transGroup;
private JRadioButton rbYes;
private JRadioButton rbNo;
private JButton okButton = null;
private JButton cancelButton = null;
private boolean cancelled = true;
public boolean isCancelled() {return cancelled;}
private CVolunteer answer;
public CVolunteer getAnswer() {return answer;}
public CDialog(JFrame owner, String title, CVolunteer vol)
{
super(owner, title, true);
Container c = getContentPane();
c.setLayout(null);
label1 = new JLabel ("Volunteer Number:");
label1.setSize(140,20);
label1.setLocation(40,40);
c.add(label1);
lNum = new JLabel(String.format("%05d", vol.volNum));
lNum.setSize(40,20);
lNum.setLocation(150,40);
c.add(lNum);
label2 = new JLabel ("Volunteer Name: ");
label2.setSize(100,20);
label2.setLocation(40,90);
c.add(label2);
tfName = new JTextField(vol.name);
tfName.setSize(120,20);
tfName.setLocation(140,90);
c.add(tfName);
int x,y;
int w,h;
x=4;
y=150;
w=180;
h=20;
label3 = new JLabel("Subject: ");
label3.setSize(85,13);
label3.setLocation(x,y);
c.add(label3);
rbMath = new JRadioButton("Math", vol.sub==0);
rbMath.setSize(w,h);
rbMath.setLocation(x+16,y+30);
c.add(rbMath);
rbScience = new JRadioButton("Science", vol.sub==1);
rbScience.setSize(w,h);
rbScience.setLocation(x+16,y+66);
c.add(rbScience);
rbEnglish = new JRadioButton("English", vol.sub==2);
rbEnglish.setSize(w,h);
rbEnglish.setLocation(x+16,y+102);
c.add(rbEnglish);
rbHistory = new JRadioButton("History", vol.sub==3);
rbHistory.setSize(w,h);
rbHistory.setLocation(x+16,y+138);
c.add(rbHistory);
subGroup = new ButtonGroup();
subGroup.add(rbMath);
subGroup.add(rbScience);
subGroup.add(rbEnglish);
subGroup.add(rbHistory);
x=220;
y=150;
w=120;
h=20;
label4 = new JLabel("Days Available: ");
label4.setSize(w,h);
label4.setLocation(x,y);
c.add(label4);
cbMonday = new JCheckBox("Monday (M)", (vol.volDays&1)!=0);
cbMonday.setSize(w,h);
cbMonday.setLocation(x+6,y+30);
c.add(cbMonday);
cbTuesday = new JCheckBox("Tuesday (T)", (vol.volDays&2)!=0);
cbTuesday.setSize(w,h);
cbTuesday.setLocation(x+6,y+66);
c.add(cbTuesday);
cbWednesday = new JCheckBox("Wednesday (W)", (vol.volDays&4)!=0);
cbWednesday.setSize(w,h);
cbWednesday.setLocation(x+6,y+102);
c.add(cbWednesday);
cbThursday = new JCheckBox("Thursday (R)", (vol.volDays&8)!=0);
cbThursday.setSize(w,h);
cbThursday.setLocation(x+6,y+138);
c.add(cbThursday);
x=480;
y=150;
w=180;
h=20;
label5 = new JLabel("Need Transport? :");
label5.setSize(150,13);
label5.setLocation(x,y);
c.add(label5);
rbYes = new JRadioButton("Yes", vol.trans==0);
rbYes.setSize(w,h);
rbYes.setLocation(x+12,y+30);
c.add(rbYes);
rbNo = new JRadioButton("No", vol.trans==1);
rbNo.setSize(w,h);
rbNo.setLocation(x+12,y+66);
c.add(rbNo);
transGroup = new ButtonGroup();
transGroup.add(rbYes);
transGroup.add(rbNo);
cancelButton = new JButton("Cancel");
cancelButton.addActionListener(this);
cancelButton.setSize(100,50);
cancelButton.setLocation(116,380);
c.add(cancelButton);
okButton = new JButton("OK");
okButton.addActionListener(this);
okButton.setSize(100,50);
okButton.setLocation(400,380);
c.add(okButton);
setSize(700,480);
setLocationRelativeTo(owner);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==okButton) {
int num=Integer.parseInt(lNum.getText());
String name=tfName.getText();
int subj=-1;
if (rbMath.isSelected()) subj = 0;
if (rbScience.isSelected()) subj = 1;
if (rbEnglish.isSelected()) subj = 2;
if (rbHistory.isSelected()) subj = 3;
int days=0;
if (cbMonday.isSelected()) days |= 1;
if (cbTuesday.isSelected()) days |= 2;
if (cbWednesday.isSelected()) days |= 4;
if (cbThursday.isSelected()) days |= 8;
int tran=0;
if (rbYes.isSelected()) tran = 0;
if (rbNo.isSelected()) tran = 1;
answer=new CVolunteer(num, name, subj, days, tran);
cancelled = false;
setVisible(false);
}
else if(e.getSource()==cancelButton) {
cancelled = true;
setVisible(false);
}
}
}
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
public class TestDialog extends JFrame implements ActionListener
{
JLabel myLabel1 = null;
JLabel myLabel2 = null;
JLabel myLabel3 = null;
JLabel myLabel4 = null;
JLabel myLabel5 = null;
JLabel myLabel6 = null;
File fileName = new File("Volunteers.txt");
ArrayList<CVolunteer> volArray;
private DefaultListModel volunteers;
JList volList;
JScrollPane scrollPane = null;
JButton bAdd = null;
JButton bEdit = null;
JButton bRemove = null;
JButton bClear = null;
JButton bSave = null;
JButton bOpen = null;
int volNumb;
public TestDialog()
{
super("Volunteer Info");
Container c = getContentPane();
c.setLayout(null);
myLabel1 = new JLabel("Vol Number");
myLabel1.setSize(200,50);
myLabel1.setLocation(100,10);
c.add(myLabel1);
myLabel2 = new JLabel("Vol Name");
myLabel2.setSize( 200, 50 );
myLabel2.setLocation( 200, 10 );
c.add(myLabel2);
myLabel3 = new JLabel("Subject");
myLabel3.setSize( 200, 50 );
myLabel3.setLocation( 310, 10);
c.add(myLabel3);
myLabel4 = new JLabel("Vol Days");
myLabel4.setSize( 200, 50 );
myLabel4.setLocation( 400, 10 );
c.add(myLabel4);
myLabel5 = new JLabel("Transport");
myLabel5.setSize( 200, 50 );
myLabel5.setLocation( 500, 10 );
c.add(myLabel5);
volArray = new ArrayList<CVolunteer>();
volunteers = new DefaultListModel();
volList = new JList(volunteers);
volList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollPane = new JScrollPane(volList);
scrollPane.setSize(500,300);
scrollPane.setLocation(100,50);
c.add(scrollPane);
bAdd = new JButton("Add");
bAdd.setSize( 100, 50 );
bAdd.setLocation( 20, 400 );
bAdd.addActionListener(this);
c.add(bAdd);
bEdit = new JButton("Edit");
bEdit.setSize( 100, 50 );
bEdit.setLocation( 150, 400 );
bEdit.addActionListener(this);
c.add(bEdit);
bRemove = new JButton("Remove");
bRemove.setSize( 100, 50 );
bRemove.setLocation( 280, 400 );
bRemove.addActionListener(this);
c.add(bRemove);
bClear = new JButton("Clear");
bClear.setSize( 100, 50 );
bClear.setLocation( 410, 400 );
bClear.addActionListener(this);
c.add(bClear);
bSave = new JButton("Save");
bSave.setSize( 100, 50 );
bSave.setLocation( 540, 400 );
bSave.addActionListener(this);
c.add(bSave);
bOpen = new JButton("Open");
bOpen.setSize( 100, 50 );
bOpen.setLocation( 670, 400 );
bOpen.addActionListener(this);
c.add(bOpen);
setSize( 800, 600 );
setLocation( 100, 100 );
setVisible(true);
volNumb = 0;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bAdd) {
volNumb++;
CVolunteer defaultVol = new CVolunteer(volNumb, "", 1, 0, 0);
CDialog dialogWnd = new CDialog(this, "Add a Volunteer", defaultVol);
if (!dialogWnd.isCancelled()) {
volArray.add(dialogWnd.getAnswer());
volunteers.addElement(dialogWnd.getAnswer().getVolunteerLine());
volList.setSelectedIndex(volunteers.size()-1);
volList.ensureIndexIsVisible(volunteers.size()-1);
}
}
else if(e.getSource()==bEdit) {
int index=volList.getSelectedIndex();
if (index>=0) {
CDialog dialogWnd = new CDialog (this, "Edit a Volunteer", volArray.get(index));
if (!dialogWnd.isCancelled()) {
volArray.set(index, dialogWnd.getAnswer());
volunteers.set(index, dialogWnd.getAnswer().getVolunteerLine());
}
}
}
else if(e.getSource()==bRemove) {
int index=volList.getSelectedIndex();
if (index>=0) {
volArray.remove(index);
volunteers.remove(index);
if (volunteers.size()>0) {
if (index==volunteers.size()) {
index--;
}
volList.setSelectedIndex(index);
volList.ensureIndexIsVisible(index);
}
}
}
else if(e.getSource()==bClear) {
volArray.clear();
volunteers.clear();
}
else if (e.getSource()==bSave)
{
//my sorry attempt at writing a file.. ignore this!
try {
FileWriter fw = new FileWriter(fileName);
Writer output = new BufferedWriter(fw);
int size = volArray.size();
for (int i = 0; i<size; i++)
{
output.write(volArray.get(i).getVolunteerLine() + "\n");
}
output.close();
}
catch (Exception e1) {
// TODO Auto-generated catch block
}
final JFileChooser fc = new JFileChooser();
}
else if (e.getSource()==bOpen)
{
}
}
public static void main(String[] args) {
TestDialog mainWnd = new TestDialog();
}
}
导入java.awt.event.*;
导入javax.swing.*;
导入java.awt.*;
导入java.io.*;
导入java.util.ArrayList;
公共类TestDialog扩展JFrame实现ActionListener
{
JLabel myLabel1=null;
JLabel myLabel2=null;
JLabel myLabel3=null;
JLabel myLabel4=null;
JLabel myLabel5=null;
JLabel myLabel6=null;
文件名=新文件(“志愿者.txt”);
数组列表;
私营部门;模范志愿者;
JList-volList;
JScrollPane scrollPane=null;
JButton-bAdd=null;
JButton bEdit=null;
JButton-bRemove=null;
JButton bClear=null;
JButton bSave=null;
JButton bOpen=null;
int-volNumb;
公共测试对话框()
{
超级(“志愿者信息”);
容器c=getContentPane();
c、 setLayout(空);
myLabel1=新的JLabel(“卷号”);
myLabel1.setSize(200,50);
myLabel1.设置位置(100,10);
c、 添加(myLabel1);
myLabel2=新的JLabel(“卷名”);
myLabel2.setSize(200,50);
myLabel2.设置位置(200,10);
c、 添加(myLabel2);
myLabel3=新的JLabel(“主体”);
myLabel3.setSize(200,50);
myLabel3.设置位置(310,10);
c、 添加(myLabel3);
myLabel4=新的JLabel(“卷天数”);
myLabel4.设置大小(200,50);
myLabel4.设置位置(400,10);
c、 添加(myLabel4);
myLabel5=新的JLabel(“传输”);
myLabel5.setSize(200,50);
myLabel5.设置位置(500,10);
c、 添加(myLabel5);
volArray=newarraylist();
志愿者=新的DefaultListModel();
volList=新的JList(志愿者);
volList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollPane=新的JScrollPane(volList);
滚动窗格。设置大小(500300);
滚动窗格。设置位置(100,50);
c、 添加(滚动窗格);
bAdd=新按钮(“添加”);
添加设置尺寸(100,50);
添加设置位置(20400);
addd.addActionListener(这个);
c、 添加(添加);
bEdit=新的JButton(“编辑”);
bEdit.setSize(100,50);
设置位置(150400);
addActionListener(this);
c、 添加(bEdit);
bRemove=新按钮(“移除”);
bRemove.setSize(100,50);
设置位置(280400);
bRemove.addActionListener(this);
c、 添加(删除);
bClear=新按钮(“清除”);
b清晰设置尺寸(100,50);
b清除设置位置(410400);
bClear.addActionListener(此);
c、 添加(b清除);
bSave=新的JButton(“保存”);
b保存设置大小(100,50);
b保存设置位置(540400);
bSave.addActionListener(这个);
c、 添加(bSave);
bOpen=新的JButton(“打开”);
bOpen.setSize(100,50);
设置位置(670400);
bOpen.addActionListener(这个);
c、 添加(bOpen);
设置大小(800600);
设置位置(100100);
setVisible(真);
volNumb=0;
}
已执行的公共无效操作(操作事件e){
如果(如getSource()==bAdd){
volNumb++;
CVolunteer defaultVol=新的CVolunteer(volNumb,“,1,0,0);
CDialog dialogWnd=新建CDialog(此“添加志愿者”,默认音量);
如果(!dialogWnd.isCancelled()){
add(dialogWnd.getAnswer());
addElement(dialogWnd.getAnswer().GetOrganizationLine());
volList.setSelectedIndex(志愿者.size()-1);
volList.ensureIndexIsVisible(志愿者.size()-1);
}
}
else if(e.getSource()==bEdit){
int index=volList.getSelectedIndex();
如果(索引>=0){
CDialog dialogWnd=newcdialog(这是“编辑志愿者”,volArray.get(index));
如果(!dialogWnd.isCancelled()){
set(索引,dialogWnd.getAnswer());
set(索引,dialogWnd.getAnswer().getOrganizationLine());
}
}
}
else if(e.getSource()==bRemove){
int index=volList.getSelec
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
// Here you can open the file and write to it
//
} else {
log.append("Open command cancelled by user." + newline);
}