私は、ユーザーが後でプルアップするために学生の名前とその成績を追加できるようにする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;
}
}
Studentクラス
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クラス
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クラス
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);
}
});
ご迷惑をおかけして申し訳ございませんが、私はプログラミングの面白いです。この情報のいくつかをクリアするために質問をすることをどうぞお気軽に。しかし、基本的には、私が入力したこれらの生徒のすべてを保存するデータベースを作成しようとしており、ユーザーが入力したテキストから再度検索して元に戻すことができます。私は最近入力した情報を元に戻すことができますが、各学生を分けて検索し、それぞれの成績を個別に引き上げたいと考えています。私はこれを完了するのに近いように感じるが、私はいくつかの重要な要素を欠いている。御時間ありがとうございます。
すぐに役立つようにするには、[MCVE]または[ショート、自己完結型、正しい例](http://www.sscce.org/)を投稿してください。 –
* "ユーザー検索機能の作成方法" * 'RowSorter'で' JTable'を使用してください。詳細は、[テーブルの使用方法:ソートとフィルタリング](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting)を参照してください。 –