我正在尝试将我的代码组织成有意义的类,所以我创建了一个FileIOManagement
类,我想处理所有文本文件的读取等。目前,我用来读取文件的方法存在于显示数据的GUI类,但我希望能够分离GUI和其他逻辑。
这是我当前的FileIOManagement类的代码:
package playingwithswing;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
public class FileIOManagement {
private ArrayList<String> nameList = new ArrayList<String>();
private ArrayList<String> courseList = new ArrayList<String>();
private ArrayList<String> semesterList = new ArrayList<String>();
private ArrayList<String> moderatorList = new ArrayList<String>();
private ArrayList<String> programList = new ArrayList<String>();
private ArrayList<String> majorList = new ArrayList<String>();
public FileIOManagement(){
readTextFile();
}
private void readTextFile(){
try{
Scanner scan = new Scanner(new File("Course.txt"));
while(scan.hasNextLine()){
String line = scan.nextLine();
String[] tokens = line.split("~");
String course = tokens[0].trim();
String examiner = tokens[1].trim();
String moderator = tokens[2].trim();
String semester = tokens[3].trim();
String program = tokens[4].trim();
String major = tokens[5].trim();
courseList.add(course);
semesterList.add(semester);
nameList.add(examiner);
moderatorList.add(moderator);
programList.add(program);
majorList.add(major);
HashSet hs = new HashSet();
hs.addAll(nameList);
nameList.clear();
nameList.addAll(hs);
Collections.sort(nameList);
}
scan.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}
}
这是我希望能够显示FileIOManagement
类完成的文件读取数据的类。我已经注释掉了代码行,我已经进入了FileIOManagement
类:
package playingwithswing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.border.EmptyBorder;
public class ReportGUI extends JFrame{
//Fields
private JButton viewAllReports = new JButton("View All Program Details");
private JButton viewPrograms = new JButton("View Programs and Majors Associated with this course");
private JButton viewTaughtCourses = new JButton("View Courses this Examiner Teaches");
private JLabel courseLabel = new JLabel("Select a Course: ");
private JLabel examinerLabel = new JLabel("Select an Examiner: ");
private JPanel panel = new JPanel(new GridLayout(6,2,4,4));
private FileIOManagement fileWork = new FileIOManagement();
// private ArrayList<String> nameList = new ArrayList<String>();
// private ArrayList<String> courseList = new ArrayList<String>();
// private ArrayList<String> semesterList = new ArrayList<String>();
// private ArrayList<String> moderatorList = new ArrayList<String>();
// private ArrayList<String> programList = new ArrayList<String>();
// private ArrayList<String> majorList = new ArrayList<String>();
public ReportGUI(){
reportInterface();
allReportsBtn();
// examinnerFileRead();
// courseFileRead();
comboBoxes();
}
// private void examinnerFileRead(){
// try{
// Scanner scan = new Scanner(new File("Course.txt"));
//
// while(scan.hasNextLine()){
// String line = scan.nextLine();
// String[] tokens = line.split("~");
// String course = tokens[0].trim();
// String examiner = tokens[1].trim();
// String moderator = tokens[2].trim();
// String semester = tokens[3].trim();
// String program = tokens[4].trim();
// String major = tokens[5].trim();
//
//
// courseList.add(course);
// semesterList.add(semester);
// nameList.add(examiner);
// moderatorList.add(moderator);
// programList.add(program);
// majorList.add(major);
// HashSet hs = new HashSet();
// hs.addAll(nameList);
// nameList.clear();
// nameList.addAll(hs);
// Collections.sort(nameList);
// // nameList.add(moderator);
// }
// scan.close();
// }
// catch (FileNotFoundException e){
// e.printStackTrace();
// }
// }
// private void courseFileRead(){
// try{
// Scanner scan = new Scanner(new File("Course.txt"));
//
// while(scan.hasNextLine()){
// courseList.add(scan.nextLine());
// }
// scan.close();
// }
// catch (FileNotFoundException e){
// e.printStackTrace();
// }
// }
private void reportInterface(){
setTitle("Choose Report Specifications");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(new FlowLayout());
add(panel, BorderLayout.CENTER);
setSize(650,200);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
}
private void allReportsBtn(){
JPanel panel = new JPanel(new GridLayout(1,1));
panel.setBorder(new EmptyBorder(70, 50, 70, 25));
panel.add(viewAllReports);
viewAllReports.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
new AllDataGUI();
}
});
add(panel, BorderLayout.LINE_END);
}
private void comboBoxes(){
panel.setBorder(new EmptyBorder(0, 5, 5, 10));
String[] comboBox1Array = nameList.toArray(new String[nameList.size()]);
JComboBox comboBox1 = new JComboBox(comboBox1Array);
panel.add(examinerLabel);
panel.add(comboBox1);
panel.add(viewTaughtCourses);
viewTaughtCourses.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ViewCourseGUI();
}
});
String[] comboBox2Array = courseList.toArray(new String[courseList.size()]);
JComboBox comboBox2 = new JComboBox(comboBox2Array);
panel.add(courseLabel);
panel.add(comboBox2);
panel.add(viewPrograms);
viewPrograms.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ViewProgramGUI();
}
});
add(panel, BorderLayout.LINE_START);
}
}
最好的面向对象的聚焦方法是做什么的?
编辑:我已经尝试过没有运气的安装者和吸气剂......这让我疯狂。package playingwithswing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.border.EmptyBorder;
public class ReportGUI extends JFrame {
//Fields
private JButton viewAllReports = new JButton("View All Program Details");
private JButton viewPrograms = new JButton("View Programs and Majors Associated with this course");
private JButton viewTaughtCourses = new JButton("View Courses this Examiner Teaches");
private JLabel courseLabel = new JLabel("Select a Course: ");
private JLabel examinerLabel = new JLabel("Select an Examiner: ");
private JPanel panel = new JPanel(new GridLayout(6,2,4,4));
private FileIOManagement fileInformation;
private ArrayList<FileIOManagement> nameList;
private ArrayList<FileIOManagement> courseList;
private void setNameList(ArrayList<FileIOManagement> names){
nameList = names;
}
private ArrayList<FileIOManagement> getNameList(){
return nameList;
}
private void setCourseList(ArrayList<FileIOManagement> courses){
courseList = courses;
}
private ArrayList<FileIOManagement> getCourseList(){
return courseList;
}
private void setFileIOManagement(FileIOManagement fileObj){
fileInformation = fileObj;
}
private FileIOManagement getFileInformation(){
return fileInformation;
}
private String obtainFileList(){
ArrayList<String> nameList = this.getFileInformation().obtainNamesForGUI();
ArrayList<String> courseList = this.getFileInformation().obtainCoursesForGUI();
return nameList.get(0);
}
public ReportGUI(){
reportInterface();
allReportsBtn();
//fileRead();
comboBoxes();
}
public ReportGUI(ArrayList<FileIOManagement> nameList, ArrayList<FileIOManagement> courseList){
setNameList(nameList);
setCourseList(courseList);
reportInterface();
allReportsBtn();
//fileRead();
comboBoxes();
}
// private void fileRead(){
// try{
// Scanner scan = new Scanner(new File("Course.txt"));
//
// while(scan.hasNextLine()){
// String line = scan.nextLine();
// String[] tokens = line.split("~");
// String course = tokens[0].trim();
// String examiner = tokens[1].trim();
// String moderator = tokens[2].trim();
// String semester = tokens[3].trim();
// String program = tokens[4].trim();
// String major = tokens[5].trim();
//
//
// courseList.add(course);
// semesterList.add(semester);
// nameList.add(examiner);
// moderatorList.add(moderator);
// programList.add(program);
// majorList.add(major);
// HashSet hs = new HashSet();
// hs.addAll(nameList);
// nameList.clear();
// nameList.addAll(hs);
// Collections.sort(nameList);
// // nameList.add(moderator);
// }
// scan.close();
// }
// catch (FileNotFoundException e){
// e.printStackTrace();
// }
// }
private void reportInterface(){
setTitle("Choose Report Specifications");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(new FlowLayout());
add(panel, BorderLayout.CENTER);
setSize(650,200);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
}
private void allReportsBtn(){
JPanel panel = new JPanel(new GridLayout(1,1));
panel.setBorder(new EmptyBorder(70, 50, 70, 25));
panel.add(viewAllReports);
viewAllReports.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
new AllDataGUI();
}
});
add(panel, BorderLayout.LINE_END);
}
private void comboBoxes(){
panel.setBorder(new EmptyBorder(0, 5, 5, 10));
String[] comboBox1Array = nameList.toArray(new String[nameList.size()]);
JComboBox comboBox1 = new JComboBox(comboBox1Array);
panel.add(examinerLabel);
panel.add(comboBox1);
panel.add(viewTaughtCourses);
viewTaughtCourses.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ViewCourseGUI();
}
});
String[] comboBox2Array = courseList.toArray(new String[courseList.size()]);
JComboBox comboBox2 = new JComboBox(comboBox2Array);
panel.add(courseLabel);
panel.add(comboBox2);
panel.add(viewPrograms);
viewPrograms.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ViewProgramGUI();
}
});
add(panel, BorderLayout.LINE_START);
}
}
FIO课程:
package playingwithswing;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
public class FileIOManagement {
private ArrayList<String> nameList;// = new ArrayList<String>();
private ArrayList<String> courseList;// = new ArrayList<String>();
// private ArrayList<String> semesterList = new ArrayList<String>();
// private ArrayList<String> moderatorList = new ArrayList<String>();
// private ArrayList<String> programList = new ArrayList<String>();
// private ArrayList<String> majorList = new ArrayList<String>();
public ArrayList<String> obtainNamesForGUI(){
ArrayList<String> nameList = new ArrayList<String>();
return nameList;
}
public ArrayList<String> obtainCoursesForGUI(){
ArrayList<String> courseList = new ArrayList<String>();
return courseList;
}
// private ArrayList<String> getNameList(){
// return nameList;
// }
//
// private ArrayList<String> getCourseList(){
// return courseList;
// }
public FileIOManagement(){
readTextFile();
}
private void readTextFile(){
try{
Scanner scan = new Scanner(new File("Course.txt"));
while(scan.hasNextLine()){
String line = scan.nextLine();
String[] tokens = line.split("~");
String course = tokens[0].trim();
String examiner = tokens[1].trim();
String moderator = tokens[2].trim();
String semester = tokens[3].trim();
String program = tokens[4].trim();
String major = tokens[5].trim();
courseList.add(course);
//semesterList.add(semester);
nameList.add(examiner);
// moderatorList.add(moderator);
// programList.add(program);
// majorList.add(major);
HashSet hs = new HashSet();
hs.addAll(nameList);
nameList.clear();
nameList.addAll(hs);
Collections.sort(nameList);
}
scan.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
根据我的理解,您只需要在FileIOManagement类中添加get方法。然后,您将能够通过调用与fileWork变量关联的方法(FileIOManagement对象)来访问FileIOManagement中的数组列表。
FileIOManagement类:
package playingwithswing;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
public class FileIOManagement {
private ArrayList<String> nameList;
private ArrayList<String> courseList;
private ArrayList<String> semesterList;
private ArrayList<String> moderatorList;
private ArrayList<String> programList;
private ArrayList<String> majorList;
public FileIOManagement(){
nameList = new ArrayList<String>();
courseList = new ArrayList<String>();
semesterList = new ArrayList<String>();
moderatorList = new ArrayList<String>();
programList = new ArrayList<String>();
majorList = new ArrayList<String>();
readTextFile();
}
public ArrayList<String> getNameList() {return nameList;}
public ArrayList<String> getCourseList() {return nameList;}
public ArrayList<String> getSemesterList() {return nameList;}
public ArrayList<String> getModeratorList() {return nameList;}
public ArrayList<String> getProgramList() {return nameList;}
public ArrayList<String> getMajorList() {return nameList;}
private void readTextFile(){
try{
Scanner scan = new Scanner(new File("Course.txt"));
while(scan.hasNextLine()){
String line = scan.nextLine();
String[] tokens = line.split("~");
String course = tokens[0].trim();
String examiner = tokens[1].trim();
String moderator = tokens[2].trim();
String semester = tokens[3].trim();
String program = tokens[4].trim();
String major = tokens[5].trim();
courseList.add(course);
semesterList.add(semester);
nameList.add(examiner);
moderatorList.add(moderator);
programList.add(program);
majorList.add(major);
HashSet hs = new HashSet();
hs.addAll(nameList);
nameList.clear();
nameList.addAll(hs);
Collections.sort(nameList);
}
scan.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}
}
ReportGUI课程:
package playingwithswing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.border.EmptyBorder;
public class ReportGUI extends JFrame{
//Fields
private JButton viewAllReports = new JButton("View All Program Details");
private JButton viewPrograms = new JButton("View Programs and Majors Associated with this course");
private JButton viewTaughtCourses = new JButton("View Courses this Examiner Teaches");
private JLabel courseLabel = new JLabel("Select a Course: ");
private JLabel examinerLabel = new JLabel("Select an Examiner: ");
private JPanel panel = new JPanel(new GridLayout(6,2,4,4));
private FileIOManagement fileWork;
public ReportGUI(){
fileWork = new FileIOManagement();
reportInterface();
allReportsBtn();
examinnerFileRead();
courseFileRead();
comboBoxes();
}
private void reportInterface(){
setTitle("Choose Report Specifications");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(new FlowLayout());
add(panel, BorderLayout.CENTER);
setSize(650,200);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
}
private void allReportsBtn(){
JPanel panel = new JPanel(new GridLayout(1,1));
panel.setBorder(new EmptyBorder(70, 50, 70, 25));
panel.add(viewAllReports);
viewAllReports.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
//new AllDataGUI();
}
});
add(panel, BorderLayout.LINE_END);
}
private void comboBoxes(){
panel.setBorder(new EmptyBorder(0, 5, 5, 10));
String[] comboBox1Array = fileWork.getNameList().toArray(
new String[fileWork.getNameList().size()]);
JComboBox comboBox1 = new JComboBox(comboBox1Array);
panel.add(examinerLabel);
panel.add(comboBox1);
panel.add(viewTaughtCourses);
viewTaughtCourses.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//new ViewCourseGUI();
}
});
String[] comboBox2Array = fileWork.getCourseList().toArray(
new String[fileWork.getCourseList().size()]);
JComboBox comboBox2 = new JComboBox(comboBox2Array);
panel.add(courseLabel);
panel.add(comboBox2);
panel.add(viewPrograms);
viewPrograms.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//new ViewProgramGUI();
}
});
add(panel, BorderLayout.LINE_START);
}
}