我正在Java Swing中创建一个程序,允许用户添加学生姓名及其成绩以便稍后提取。我在查看提交后将其保存后,无法确定如何从搜索功能中提取学生信息。我将发布所有代码,以便您可以查看到底发生了什么,并突出显示重要部分。有7个班级。
DataModel类
package gradebook;
import java.util.ArrayList;
import java.util.Iterator;
public class DataModel {
public ArrayList<Student> students = new ArrayList<Student>();
public void addStudent(Student s){
students.add(s);
}
public Iterator<Student> getStudents()
{
return students.iterator();
}
public void addGrade(Student s, Grade g){
for (Iterator<Student> iterator = students.iterator(); iterator.hasNext();) {
Student ss = (Student) iterator.next();
if(s.getStudentName().equals(ss.getStudentName()))
{
ss.getGrades().add(g);
}
}
}
}
年级
package gradebook;
public class Grade {
public String assignment;
public String getAssignment() {
return assignment;
}
public void setAssignment(String assignment) {
this.assignment = assignment;
}
}
学生班
package gradebook;
import java.util.ArrayList;
public class Student {
public String studentName;
public ArrayList<Grade> grades = new ArrayList<Grade>();
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public ArrayList<Grade> getGrades() {
return grades;
}
public void setGrades(ArrayList<Grade> grades) {
this.grades = grades;
}
}
UIHomeScreen类
package gradebook;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.Color;
public class UIHomeScreen extends JFrame {
private JPanel contentPane;
private DataModel dm = new DataModel();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new UIHomeScreen().setVisible(true);
}
});
}
/**
* Create the frame.
*/
public UIHomeScreen() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel gradebookLbl = new JLabel("GradeBook");
gradebookLbl.setBounds(195, 6, 67, 16);
contentPane.add(gradebookLbl);
JLabel questionLbl = new JLabel("What would you like to do?");
questionLbl.setBounds(146, 72, 169, 16);
contentPane.add(questionLbl);
JComboBox selectComboBox = new JComboBox();
selectComboBox.setModel(new DefaultComboBoxModel(new String[] {"Select...", "Add Student and Grades", "Select Student to View", "Test"}));
selectComboBox.setBounds(156, 100, 142, 27);
contentPane.add(selectComboBox);
JLabel warningLbl = new JLabel("*");
warningLbl.setVisible(false);
warningLbl.setForeground(Color.RED);
warningLbl.setBounds(294, 104, 12, 16);
contentPane.add(warningLbl);
JButton continueBtn = new JButton("Continue");
continueBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (selectComboBox.getSelectedItem().equals("Select..."))
warningLbl.setVisible(true);
if(selectComboBox.getSelectedItem().equals("Add Student and Grades"))
{
UIStudent studentScreen = new UIStudent(dm);
studentScreen.setVisible(true);
warningLbl.setVisible(false);
}
if(selectComboBox.getSelectedItem().equals("Select Student to View")){
UISelectStudent selectStudentScreen = new UISelectStudent(dm);
selectStudentScreen.setVisible(true);
warningLbl.setVisible(false);
}
if(selectComboBox.getSelectedItem().equals("Test")){
UIStudentInfo studentInfoScreen= new UIStudentInfo(dm);
studentInfoScreen.setVisible(true);
warningLbl.setVisible(false);
}
}
});
continueBtn.setBounds(327, 243, 117, 29);
contentPane.add(continueBtn);
}
}
UISelectStudent class
package gradebook;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
public class UISelectStudent extends JFrame {
private JPanel contentPane;
private JTextField enterStudentTxt;
DataModel dm;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UISelectStudent frame = new UISelectStudent();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public UISelectStudent(DataModel dm) {
this();
this.dm = dm;
}
public UISelectStudent() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel gradebookLbl = new JLabel("Gradebook");
gradebookLbl.setBounds(193, 6, 68, 16);
contentPane.add(gradebookLbl);
JLabel enterStudentLbl = new JLabel("Please enter the student you would like to view");
enterStudentLbl.setBounds(85, 93, 296, 16);
contentPane.add(enterStudentLbl);
enterStudentTxt = new JTextField();
enterStudentTxt.setBounds(160, 122, 130, 26);
contentPane.add(enterStudentTxt);
enterStudentTxt.setColumns(10);
JButton submitBtn = new JButton("Submit");
submitBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (enterStudentTxt.equals(dm.getStudents())){
UIStudentInfo studentInfoScreen = new UIStudentInfo();
studentInfoScreen.setVisible(true);
}
}
});
submitBtn.setBounds(327, 243, 117, 29);
contentPane.add(submitBtn);
JButton cancelBtn = new JButton("Cancel");
cancelBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
cancelBtn.setBounds(200, 243, 117, 29);
contentPane.add(cancelBtn);
}
}
UIStudent class
package gradebook;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Window;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
public class UIStudent extends JFrame {
private JPanel contentPane;
private JTextField studentNameTxt;
private DataModel dm;
private JTextField ass1Txt;
private JTextField ass3Txt;
private JTextField ass5Txt;
private JTextField ass2Txt;
private JTextField ass4Txt;
private JLabel ass3Lbl;
private JLabel ass2Lbl;
private JLabel ass4Lbl;
private JLabel ass5Lbl;
private JTextField ass6Txt;
/**
* Create the frame.
*/
public UIStudent(DataModel dm) {
this();
this.dm = dm;
}
public UIStudent() {
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel gradebookLbl = new JLabel("Gradebook");
gradebookLbl.setBounds(197, 6, 68, 16);
contentPane.add(gradebookLbl);
JLabel addStudentLbl = new JLabel("Add Student");
addStudentLbl.setBounds(197, 34, 77, 16);
contentPane.add(addStudentLbl);
JLabel nameLbl = new JLabel("Student Name:");
nameLbl.setBounds(69, 67, 92, 16);
contentPane.add(nameLbl);
studentNameTxt = new JTextField();
studentNameTxt.setBounds(173, 62, 215, 26);
contentPane.add(studentNameTxt);
studentNameTxt.setColumns(10);
JButton submitBtn = new JButton("Submit");
submitBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Student s = new Student();
s.setStudentName(studentNameTxt.getText());
dm.addStudent(s);
Grade g = new Grade();
g.setAssignment(ass1Txt.getText());
dm.addGrade(s, g);
g = new Grade();
g.setAssignment(ass2Txt.getText());
dm.addGrade(s, g);
g = new Grade();
g.setAssignment(ass3Txt.getText());
dm.addGrade(s, g);
g = new Grade();
g.setAssignment(ass4Txt.getText());
dm.addGrade(s, g);
g = new Grade();
g.setAssignment(ass5Txt.getText());
dm.addGrade(s, g);
g = new Grade();
g.setAssignment(ass6Txt.getText());
dm.addGrade(s, g);
setVisible(false);
}
});
submitBtn.setBounds(327, 245, 117, 29);
contentPane.add(submitBtn);
ass1Txt = new JTextField();
ass1Txt.setBounds(173, 117, 41, 26);
contentPane.add(ass1Txt);
ass1Txt.setColumns(10);
JLabel ass1Lbl = new JLabel("Assignment 1:");
ass1Lbl.setBounds(69, 122, 92, 16);
contentPane.add(ass1Lbl);
ass3Txt = new JTextField();
ass3Txt.setBounds(173, 153, 41, 26);
contentPane.add(ass3Txt);
ass3Txt.setColumns(10);
ass5Txt = new JTextField();
ass5Txt.setBounds(173, 187, 41, 26);
contentPane.add(ass5Txt);
ass5Txt.setColumns(10);
ass2Txt = new JTextField();
ass2Txt.setBounds(347, 117, 41, 26);
contentPane.add(ass2Txt);
ass2Txt.setColumns(10);
ass4Txt = new JTextField();
ass4Txt.setBounds(347, 153, 41, 26);
contentPane.add(ass4Txt);
ass4Txt.setColumns(10);
ass3Lbl = new JLabel("Assignment 3:");
ass3Lbl.setBounds(69, 158, 92, 16);
contentPane.add(ass3Lbl);
ass2Lbl = new JLabel("Assignment 2:");
ass2Lbl.setBounds(243, 122, 92, 16);
contentPane.add(ass2Lbl);
ass4Lbl = new JLabel("Assignment 4:");
ass4Lbl.setBounds(243, 158, 92, 16);
contentPane.add(ass4Lbl);
ass5Lbl = new JLabel("Assignment 5:");
ass5Lbl.setBounds(69, 192, 92, 16);
contentPane.add(ass5Lbl);
JButton cancelBtn = new JButton("Cancel");
cancelBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
cancelBtn.setBounds(214, 245, 117, 29);
contentPane.add(cancelBtn);
JLabel ass6Lbl = new JLabel("Assignment 6:");
ass6Lbl.setBounds(243, 191, 92, 16);
contentPane.add(ass6Lbl);
ass6Txt = new JTextField();
ass6Txt.setColumns(10);
ass6Txt.setBounds(347, 187, 41, 26);
contentPane.add(ass6Txt);
}
}
UIStudentInfo类
package gradebook;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.awt.event.ActionEvent;
public class UIStudentInfo extends JFrame {
private JPanel contentPane;
private JTextField studentNameTxt;
private JTextField ass1Txt;
private JTextField ass3Txt;
private JTextField ass5Txt;
private JTextField ass2Txt;
private JTextField ass4Txt;
DataModel dm;
private JLabel ass6Lbl;
private JTextField ass6Txt;
private JLabel gradebookLbl;
private JLabel viewingStudentGradesLbl;
private JButton exitBtn;
/**
* Create the frame.
*/
public UIStudentInfo(DataModel dm){
this();
this.dm = dm;
Student s = new Student();
for (Iterator<Student> iterator = dm.getStudents(); iterator.hasNext();) {
s = (Student) iterator.next();
}
studentNameTxt.setText(s.getStudentName());
ass1Txt.setText("" + s.getGrades().get(0).getAssignment());
ass2Txt.setText("" + s.getGrades().get(1).getAssignment());
ass3Txt.setText("" + s.getGrades().get(2).getAssignment());
ass4Txt.setText("" + s.getGrades().get(3).getAssignment());
ass5Txt.setText("" + s.getGrades().get(4).getAssignment());
ass6Txt.setText("" + s.getGrades().get(5).getAssignment());
}
public UIStudentInfo() {
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel studentNameLbl = new JLabel("Student Name:");
studentNameLbl.setBounds(69, 67, 92, 16);
contentPane.add(studentNameLbl);
studentNameTxt = new JTextField();
studentNameTxt.setEditable(false);
studentNameTxt.setBounds(173, 62, 215, 26);
contentPane.add(studentNameTxt);
studentNameTxt.setColumns(10);
JLabel ass1Lbl = new JLabel("Assignment 1:");
ass1Lbl.setBounds(69, 122, 92, 16);
contentPane.add(ass1Lbl);
ass1Txt = new JTextField();
ass1Txt.setBounds(173, 117, 41, 26);
contentPane.add(ass1Txt);
ass1Txt.setColumns(10);
JLabel ass3Lbl = new JLabel("Assignment 3:");
ass3Lbl.setBounds(69, 158, 92, 16);
contentPane.add(ass3Lbl);
ass3Txt = new JTextField();
ass3Txt.setColumns(10);
ass3Txt.setBounds(173, 153, 41, 26);
contentPane.add(ass3Txt);
JLabel ass5Lbl = new JLabel("Assignment 5:");
ass5Lbl.setBounds(69, 192, 92, 16);
contentPane.add(ass5Lbl);
ass5Txt = new JTextField();
ass5Txt.setColumns(10);
ass5Txt.setBounds(173, 187, 41, 26);
contentPane.add(ass5Txt);
JLabel ass2Lbl = new JLabel("Assignment 2:");
ass2Lbl.setBounds(243, 122, 92, 16);
contentPane.add(ass2Lbl);
ass2Txt = new JTextField();
ass2Txt.setColumns(10);
ass2Txt.setBounds(347, 117, 41, 26);
contentPane.add(ass2Txt);
JLabel ass4Lbl = new JLabel("Assignment 4:");
ass4Lbl.setBounds(243, 158, 92, 16);
contentPane.add(ass4Lbl);
ass4Txt = new JTextField();
ass4Txt.setColumns(10);
ass4Txt.setBounds(347, 153, 41, 26);
contentPane.add(ass4Txt);
ass6Lbl = new JLabel("Assignment 6:");
ass6Lbl.setBounds(243, 191, 92, 16);
contentPane.add(ass6Lbl);
ass6Txt = new JTextField();
ass6Txt.setBounds(347, 187, 41, 26);
contentPane.add(ass6Txt);
ass6Txt.setColumns(10);
gradebookLbl = new JLabel("Gradebook");
gradebookLbl.setBounds(204, 6, 68, 16);
contentPane.add(gradebookLbl);
viewingStudentGradesLbl = new JLabel("Viewing Student's Grades");
viewingStudentGradesLbl.setBounds(162, 34, 162, 16);
contentPane.add(viewingStudentGradesLbl);
exitBtn = new JButton("Exit");
exitBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
exitBtn.setBounds(327, 243, 117, 29);
contentPane.add(exitBtn);
}
}
我尝试做的是,当我在enterStudentTxt文本字段中输入名称后点击此提交按钮时,它将检查它是否等于我之前从UIStudent类提交的学生姓名。我在这里做了一个if声明,但我相信我这样做是错误的,因为它没有做任何事情。
JButton submitBtn = new JButton("Submit");
submitBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (enterStudentTxt.equals(dm.getStudents())){
UIStudentInfo studentInfoScreen = new UIStudentInfo();
studentInfoScreen.setVisible(true);
}
}
});
以下是从UIStudent类保存学生姓名的代码。我需要获取此处保存的studentNameTxt,并使其能够通过用户输入再次上拉。
JButton submitBtn = new JButton("Submit");
submitBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Student s = new Student();
s.setStudentName(studentNameTxt.getText());
dm.addStudent(s);
Grade g = new Grade();
g.setAssignment(ass1Txt.getText());
dm.addGrade(s, g);
g = new Grade();
g.setAssignment(ass2Txt.getText());
dm.addGrade(s, g);
g = new Grade();
g.setAssignment(ass3Txt.getText());
dm.addGrade(s, g);
g = new Grade();
g.setAssignment(ass4Txt.getText());
dm.addGrade(s, g);
g = new Grade();
g.setAssignment(ass5Txt.getText());
dm.addGrade(s, g);
g = new Grade();
g.setAssignment(ass6Txt.getText());
dm.addGrade(s, g);
setVisible(false);
}
});
很抱歉,如果其中一些不清楚,我对编程很陌生。随意提出问题以清除这些信息。但基本上我试图创建一个数据库来存储我已经输入的所有这些学生,并能够通过搜索用户输入的文本再次将他们拉回来。我能够提取我最近输入的信息,但我希望能够将每个学生分开并搜索他们并单独提升他们的成绩。我觉得我已接近完成这项工作,但我错过了一些完成的关键要素。谢谢你的时间。
答案 0 :(得分:1)
您已将输入字符串与学生姓名的迭代器进行比较;您需要做的是搜索现有学生,使用新名称。
> enterStudentTxt.equals(dm.getStudents())
尝试编写一个检查重复名称函数,该函数返回学生是否已经存在于数据模型实例中。有什么影响:
public boolean checkDuplicateName(String name)
{
// check the whole ArrayList for a student with the same name
for(Student s : students)
{
// compare the actual name strings
if (s.studentName.equals(name)) return true;
}
return false;
}
在DataModel类中包含该函数,并确保在管理按钮的位置调用它。
JButton submitBtn = new JButton("Submit");
submitBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (checkDuplicate(enterStudentTxt)){
UIStudentInfo studentInfoScreen = new UIStudentInfo();
studentInfoScreen.setVisible(true);
}
}
});
我希望这有助于在按下按钮时发生某些事情。我没有检查你的用户界面代码,但我发现你在搜索学生时出错了。