实验要求:
2.我们原来的系统里菜单选项(Add Delete Find List),增加(Add)与删除(Delete)就有严格的排他性,你在增加的时候,是不允许删除的,或者你在删除的时候是不允许增加或者查找、遍历的,不然查找的结果或显示的结果与实际结果不会相同,发生我们说过的读写相关的问题。
3.在前期试验的基础上,考虑如何对2中我们提出的问题进行改进?(当然,这里肯定是通过线程同步的技术加以实现)我原来写的基础代码:
 package _5518;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.*;
public class Gui {
public static void main(String[] args) {
Login login = new Login("用户登录界面");
}
}class Login extends JFrame implements ActionListener {
final String name = "2011150058";
final String password = "2011150058";
JLabel user1, password1;
JTextField user2;
JPasswordField password2;
JButton button, button2; public Login(String s) {
setTitle(s);
setLayout(null);
setBounds(600, 300, 380, 240);
user1 = new JLabel("请输入用户名");
password1 = new JLabel("请输入密码");
user2 = new JTextField(20);
password2 = new JPasswordField(120);
button = new JButton("Login");
button2 = new JButton("Reset");
user1.setBounds(90, 50, 200, 40);
user2.setBounds(190, 60, 130, 20);
password1.setBounds(90, 80, 80, 40);
password2.setBounds(190, 90, 130, 20);
password2.setEchoChar('*');
button.setBounds(90, 120, 50, 25);
button2.setBounds(190, 120, 50, 25);
button.setMargin(new Insets(0, 0, 0, 0));
button2.setMargin(new Insets(0, 0, 0, 0));
button.addActionListener(this);
button2.addActionListener(this);
add(user1);
add(user2);
add(password1);
add(password2);
add(button);
add(button2);
        setVisible(true);
validate();
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
String tempString=new String(password2.getPassword());
if (name.equals(user2.getText())
&& password.equals(tempString)) {
Manage manage = new Manage("学生管理界面");
} else {
JOptionPane.showMessageDialog(null, "您输入的密码不正确", "密码验证错误",
JOptionPane.WARNING_MESSAGE);
}
} else {
user2.setText(null);
password2.setText(null);
} }
}class Manage extends JFrame implements ActionListener {
JMenuBar jMenuBar;
JMenu menu1, menu2, menu3;
JMenuItem jMenuItem1, jMenuItem2, jMenuItem3, jMenuItem4; public Manage(String s) {
setTitle(s);
setLayout(null);
setBounds(600, 300, 600, 500);
jMenuBar = new JMenuBar();
setJMenuBar(jMenuBar);
menu1 = new JMenu("File");
menu2 = new JMenu("Edit");
menu3 = new JMenu("Help");
jMenuItem1 = new JMenuItem("Add");
jMenuItem2 = new JMenuItem("Delete");
jMenuItem3 = new JMenuItem("Find");
jMenuItem4 = new JMenuItem("List");
jMenuBar.add(menu1);
jMenuBar.add(menu2);
jMenuBar.add(menu3);
menu2.add(jMenuItem1);
menu2.add(jMenuItem2);
menu2.add(jMenuItem3);
menu2.add(jMenuItem4);
jMenuItem1.addActionListener(this);
jMenuItem2.addActionListener(this);
jMenuItem3.addActionListener(this);
jMenuItem4.addActionListener(this);
setVisible(true);
validate();
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == jMenuItem1) {
Add add = new Add("Add窗口");
} else if (e.getSource() == jMenuItem2) {
Delete delete = new Delete("Delete窗口");
} else if (e.getSource() == jMenuItem3) { Find find = new Find("Find窗口");
} else {
List list = new List("List窗口");
}
}
}class Student {
String name;
String gender;
String major;
String graduate;
double politicsScore;
double englishScore;
double mathScore;
double majorScore;
public Student(String name) {
this.name=name;
}
}class Add extends JFrame implements ActionListener {
JLabel label1, label2, label3, label4, label5,
       label6, label7, label8,label9;
JButton button1, button2;
JTextField textfield1, textfield2, textfield3, textfield4, 
           textfield5,textfield6, textfield7, textfield8;
Font font = new Font("宋体", Font.BOLD, 18);
Font f = new Font("宋体", Font.BOLD, 25);
static ArrayList<Student> mylist = new ArrayList<Student>(); Add(String s) {
setTitle(s);
setLayout(null);
setBounds(650, 370, 500, 400);
label1 = new JLabel("增加考生记录操作界面");
label2 = new JLabel("请输入姓名:");
label3 = new JLabel("请输入性别:");
label4 = new JLabel("请输入专业:");
label5 = new JLabel("请输入毕业学校:");
label6 = new JLabel("政治");
label7 = new JLabel("英语");
label8 = new JLabel("数学");
label9 = new JLabel("专业课程");
button1 = new JButton("确认");
button2 = new JButton("取消");
textfield1 = new JTextField(150);
textfield2 = new JTextField(150);
textfield3 = new JTextField(150);
textfield4 = new JTextField(150);
textfield5 = new JTextField(50);
textfield6 = new JTextField(50);
textfield7 = new JTextField(50);
textfield8 = new JTextField(50); button1.setMargin(new Insets(0, 0, 0, 0));
button2.setMargin(new Insets(0, 0, 0, 0));
label1.setFont(f);
label2.setFont(font);
label2.setFont(font);
label3.setFont(font);
label4.setFont(font);
label5.setFont(font);
label6.setFont(font);
label7.setFont(font);
label8.setFont(font);
label9.setFont(font); label1.setBounds(140, 10, 400, 25);
label2.setBounds(15, 60, 150, 20);
label3.setBounds(15, 100, 150, 20);
label4.setBounds(15, 140, 150, 20);
label5.setBounds(15, 180, 150, 20);
label6.setBounds(15, 220, 150, 20);
label7.setBounds(95, 220, 150, 20);
label8.setBounds(175, 220, 150, 20);
label9.setBounds(255, 220, 150, 20);
textfield1.setBounds(190, 50, 200, 32);
textfield2.setBounds(190, 90, 200, 32);
textfield3.setBounds(190, 130, 200, 32);
textfield4.setBounds(190, 170, 200, 32);
textfield5.setBounds(15, 250, 50, 30);
textfield6.setBounds(95, 250, 50, 30);
textfield7.setBounds(175, 250, 50, 30);
textfield8.setBounds(255, 250, 50, 30);
button1.setBounds(60, 300, 150, 50);
button2.setBounds(250, 300, 150, 50);
add(label1);
add(label2);
add(label3);
add(label4);
add(label5);
add(label6);
add(label7);
add(label8);
add(label9);
add(textfield1);
add(textfield2);
add(textfield3);
add(textfield4);
add(textfield5);
add(textfield6);
add(textfield7);
add(textfield8);
add(button1);
add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
setVisible(true);
validate();
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
Student student = new Student(null);
student.name = textfield1.getText();
student.gender = textfield2.getText();
student.major = textfield3.getText();
student.graduate = textfield4.getText();
student.politicsScore = Double.parseDouble(textfield5.getText());
student.englishScore = Double.parseDouble(textfield6.getText());
student.mathScore = Double.parseDouble(textfield7.getText());
student.majorScore = Double.parseDouble(textfield8.getText());
mylist.add(student);
textfield1.setText(null);
textfield2.setText(null);
textfield3.setText(null);
textfield4.setText(null);
textfield5.setText(null);
textfield6.setText(null);
textfield7.setText(null);
textfield8.setText(null);
} else if (e.getSource() == button2) {
dispose();
} }
}class Delete extends JFrame implements ActionListener {
JLabel label1, label2;
JTextField field;
JButton button1, button2; public Delete(String s) {
setTitle(s);
setLayout(null);
setBounds(650, 370, 500, 400);
Font f = new Font("宋体", Font.BOLD, 25);
label1 = new JLabel("学生删除界面");
label2 = new JLabel("请输入待删除学生姓名:");
field = new JTextField(100);
button1 = new JButton("确认");
button2 = new JButton("取消");
label1.setFont(f);
label2.setFont(f);
label1.setBounds(190, 10, 400, 100);
label2.setBounds(5, 60, 400, 100);
field.setBounds(300, 100, 150, 35);
        button1.setBounds(80, 250, 150, 50);
button2.setBounds(300, 250, 150, 50);
button1.setMargin(new Insets(0, 0, 0, 0));
button2.setMargin(new Insets(0, 0, 0, 0));
button1.addActionListener(this);
button2.addActionListener(this);
add(label1);
add(label2);
add(field);
add(button1);
add(button2);
setVisible(true);
validate();
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
     } public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
String keyString = field.getText();
Iterator<Student> iter = (Add.mylist).iterator();
while (iter.hasNext()) {
Student te = iter.next();
if ((te.name).equals(keyString)) {
(Add.mylist).remove(te);
break;
                 }
}
}
}
}

解决方案 »

  1.   

    接着补充我的基础代码:
    class Find extends JFrame implements ActionListener {
    JLabel label1, label2;
    JTextArea textarea;
    JTextField textField;
    JButton button1, button2; Find(String s) {
    Font f = new Font("宋体", Font.BOLD, 25);
    setTitle(s);
    setLayout(null);
    setBounds(650, 370, 500, 400);
    button1 = new JButton("确认");
    button2 = new JButton("取消");
    label1 = new JLabel("学生信息查询");
    label2 = new JLabel("请输入查询姓名:");
    textField = new JTextField(150);
    textarea = new JTextArea(450, 120);
    label1.setFont(f);
    label2.setFont(f);
    label1.setBounds(150, 30, 200, 50);
    label2.setBounds(10, 85, 200, 50);
    textField.setBounds(230, 85, 200, 40);
    textarea.setBounds(20, 200, 450, 150);
    button1.setBounds(130, 140, 70, 40);
    button2.setBounds(230, 140, 70, 40);
    button1.setMargin(new Insets(0, 0, 0, 0));
    button2.setMargin(new Insets(0, 0, 0, 0));
    button1.addActionListener(this);
    button2.addActionListener(this);
    add(label1);
    add(label2);
    add(textField);
    add(textarea);
    add(button1);
    add(button2);
    setVisible(true);
    validate();
    setResizable(false);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         } public void actionPerformed(ActionEvent e) {
    if (e.getSource() == button1) {
    Student temp=null;
    String keyString = textField.getText();
    Iterator<Student> iter = (Add.mylist).iterator();
    while (iter.hasNext()) {
    Student te = iter.next();
    if (te.name.equals(keyString)) {
    textarea.append("姓名   性别   专业   毕业学校   政治   英语   数学   专业"
    + "\r\n");
    textarea.append(te.name + "    " + te.gender + "    "
    + te.major + "    " + te.graduate + "    "
    + te.politicsScore + "    " + te.englishScore
    + "    " + te.mathScore + "    " + te.majorScore
    + "\r\n");
    temp=new Student(keyString);
    temp=te;
    }
     
    }
      if(temp==null)
       textarea.append("Error,查找失败\r\n");

        else
       textField.setText(null);
    }
    }class List extends JFrame{
    JLabel label1, label2;
    JTextArea textArea1, textArea2;
    Font f = new Font("宋体", Font.BOLD, 25);
    List(String s) {
    setTitle(s);
    setLayout(null);
    setBounds(650, 370, 500, 400);
    label1 = new JLabel("参加考试学生名单");
    label2 = new JLabel("拟录取学生名单");
    textArea1 = new JTextArea(480, 120);
    textArea2 = new JTextArea(480, 120);
    label1.setFont(f);
    label2.setFont(f);
    label1.setBounds(150, 2, 300, 60);
    label2.setBounds(150, 150, 300, 100);
    textArea1.setBounds(15, 50, 450, 120);
    textArea2.setBounds(15, 230, 450, 120);
    add(label1);
    add(label2);
    add(textArea1);
    add(textArea2);
    setVisible(true);
    validate();
    setResizable(false);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    Iterator<Student> iter = (Add.mylist).iterator();
    while (iter.hasNext()) {
    Student te = iter.next();
    textArea1.append(te.name + "\t");
    boolean allReach = false;
    boolean eachReach = false;
    double allScore = te.politicsScore + te.englishScore
    + te.majorScore + te.mathScore;
    if (allScore >= 320)
    allReach = true;
    eachReach = (te.politicsScore >= 60 && te.englishScore >= 60
    && te.majorScore >= 60 && te.mathScore >= 60);
    if (allReach && eachReach) {
    textArea2.append(te.name + "\t");
    }
              }
         }
    }