如何将 JTable 添加到 JTabbedPane 中?
How to add JTable into JTabbedPane?
我正在编写一个显示 800 名学生的 GUI table,其中学生的信息是从 txt 文件中读取的。我写了一个 Screen() class 和 StudentTable() class,其中 Screen class 有带有两个 JPanel 选项卡 "PList" 和 "PChart" 的 TabbedPane。我想将我的 StudentTable class 添加到我的 PList JPanel 中。所以当我 运行 屏幕 class 时,它会显示 StudentTable class 里面有学生 table。通常,我设法在没有标签的情况下显示我的 table。
首先,我打开 "Students.txt" 文件,然后读取该文件。然后我关闭了文件。之后,我创建了一个创建 table 的无效方法 createTable()。 table 从 ArrayList 获取信息。现在,我想将此 class 添加到我的 PList JPanel。代码有点长。所以我上传了它们。不过,如果你想让我分享代码,我也可以。
Screen.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class Screen extends JFrame implements ActionListener {
private JTabbedPane tabbedPane;
private JPanel PList, PChart;
private JMenuBar menuBar;
private JMenu menuStudent, menuSort, menuFilter;
private JMenuItem addStudent, removeStudent, removeAllStudents;
private JMenuItem sortLastName, sortId, sortAverage;
private JMenuItem filterLetterGrade, removeFilter;
private StudentTable studentTable;
public Screen() {
// TODO Auto-generated constructor stub
tabbedPane = new JTabbedPane();
menuBar = new JMenuBar();
menuStudent = new JMenu();
menuSort = new JMenu();
menuFilter = new JMenu();
addStudent = new JMenuItem();
removeStudent = new JMenuItem();
removeAllStudents = new JMenuItem();
sortLastName = new JMenuItem();
sortId = new JMenuItem();
sortAverage = new JMenuItem();
filterLetterGrade = new JMenuItem();
removeFilter = new JMenuItem();
PList = new JPanel();
PChart = new JPanel();
studentTable = new StudentTable();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setExtendedState(MAXIMIZED_BOTH);
setTitle("Screen");
setLayout(new BorderLayout());
setJMenuBar(menuBar);
// Menu
menuStudent.setText("Student");
addStudent.setText("Add Student");
addStudent.addActionListener(this);
removeStudent.setText("Remove Student");
removeStudent.addActionListener(this);
removeAllStudents.setText("Remove All Students");
removeAllStudents.addActionListener(this);
menuStudent.add(addStudent);
menuStudent.add(removeStudent);
menuStudent.add(removeAllStudents);
menuSort.setText("Sort");
sortLastName.setText("Sort by last name");
sortLastName.addActionListener(this);
sortId.setText("Sort by ID");
sortId.addActionListener(this);
sortAverage.setText("Sort by average");
sortAverage.addActionListener(this);
menuSort.add(sortLastName);
menuSort.add(sortId);
menuSort.add(sortAverage);
menuFilter.setText("Filter");
filterLetterGrade.setText("Filter by letter grade");
filterLetterGrade.addActionListener(this);
removeFilter.setText("Remove filter");
removeFilter.addActionListener(this);
menuFilter.add(filterLetterGrade);
menuFilter.add(removeFilter);
menuBar.add(menuStudent);
menuBar.add(menuSort);
menuBar.add(menuFilter);
// Tabs
tabbedPane.add("List", PList);
tabbedPane.add("Chart", PChart);
add(tabbedPane);
PList.add(studentTable);
}
public static void main(String[] args) {
new Screen();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
StudentTable.java
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class StudentTable extends JPanel {
private static JTable table;
private String name, lastName, letterGrade;
private long studentId;
private double quiz1,quiz2,project,midterm,finalGrade,average;
private static ArrayList<Students> studentList;
private Scanner scanner;
public StudentTable() {
}
/**
*
* @param name
* @param lastName
* @param studentId
* @param quiz1
* @param quiz2
* @param project
* @param midterm
* @param finalGrade
* @param average
* @param letterGrade
*/
public StudentTable(String name, String lastName, long studentId, double quiz1, double quiz2, double project, double midterm, double finalGrade, double average, String letterGrade) {
this.name = name;
this.lastName = lastName;
this.letterGrade = letterGrade;
this.studentId = studentId;
this.quiz1 = quiz1;
this.quiz2 = quiz2;
this.project = project;
this.midterm = midterm;
this.finalGrade = finalGrade;
this.average = average;
}
// GET & SET METHODS
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLetterGrade() {
return letterGrade;
}
public void setLetterGrade(String letterGrade) {
this.letterGrade = letterGrade;
}
public long getStudentId() {
return studentId;
}
public void setStudentId(long studentId) {
this.studentId = studentId;
}
public double getQuiz1() {
return quiz1;
}
public void setQuiz1(double quiz1) {
this.quiz1 = quiz1;
}
public double getQuiz2() {
return quiz2;
}
public void setQuiz2(double quiz2) {
this.quiz2 = quiz2;
}
public double getProject() {
return project;
}
public void setProject(double project) {
this.project = project;
}
public double getMidterm() {
return midterm;
}
public void setMidterm(double midterm) {
this.midterm = midterm;
}
public double getFinalGrade() {
return finalGrade;
}
public void setFinalGrade(double finalGrade) {
this.finalGrade = finalGrade;
}
public double getAverage() {
return average;
}
public void setAverage(double average) {
this.average = average;
}
public ArrayList<Students> getStudentList() {
return studentList;
}
public void setStudentList(ArrayList<Students> studentList) {
this.studentList = studentList;
}
// Opening "Students.txt" file from a url.
public void openFile() {
try {
URL url = new URL("http://rawsly.com/Students.txt");
scanner = new Scanner(url.openStream());
} catch (Exception e) {
// TODO: handle exception
System.out.println("Error: " + e.getMessage());
}
}
// Reading "Students.txt" file and adding students to the "studentList" ArrayList.
public void readFile() {
studentList = new ArrayList<>();
while(scanner.hasNext()) {
name = scanner.next();
lastName = scanner.next();
studentId = scanner.nextLong();
quiz1 = scanner.nextDouble();
quiz2 = scanner.nextDouble();
project = scanner.nextDouble();
midterm = scanner.nextDouble();
finalGrade = scanner.nextDouble();
average = scanner.nextDouble();
letterGrade = scanner.next();
studentList.add(new Students(name, lastName, studentId, quiz1, quiz2, project, midterm, finalGrade, average, letterGrade));
}
}
// Closing "Students.txt" file.
public void closeFile() {
scanner.close();
}
public void createTable() {
openFile();
readFile();
closeFile();
String [] columnNames = {"NAME", "SURNAME", "ID", "QUIZ1", "QUIZ2", "PROJECT", "MIDTERM", "FINAL", "AVERAGE", "LETTER GRADE"};
Object [][] data = new Object[studentList.size()][10];
for(int i=0; i<studentList.size(); i++) {
for(int j=0; j<10; j++) {
switch (j) {
case 0:
data[i][0] = studentList.get(i).getName();
break;
case 1:
data[i][1] = studentList.get(i).getLastName();
break;
case 2:
data[i][2] = studentList.get(i).getStudentId();
break;
case 3:
data[i][3] = studentList.get(i).getQuiz1();
break;
case 4:
data[i][4] = studentList.get(i).getQuiz2();
break;
case 5:
data[i][5] = studentList.get(i).getProject();
break;
case 6:
data[i][6] = studentList.get(i).getMidterm();
break;
case 7:
data[i][7] = studentList.get(i).getFinalGrade();
break;
case 8:
data[i][8] = studentList.get(i).getAverage();
break;
case 9:
data[i][9] = studentList.get(i).getLetterGrade();
break;
} // end of the switch
} // end of the first for loop
} // end of the second for loop
table = new JTable(data,columnNames) {
public boolean isCellEditable(int row, int column) { // To make row and columns not editable
return false;
}
};
table.setFillsViewportHeight(true);
table.setAutoCreateRowSorter(true); // to activate sorting property of each column
}
public static void main(String[] args) {
StudentTable s = new StudentTable();
s.createTable();
}
}
这就是您在屏幕 class.
中创建带有 Table 的面板的方式
studentTable = new StudentTable();
这就是 StudentTable 的构造函数。
public StudentTable() {}
你看到这里的问题了吗?您正在调用一个空的构造函数。
您可以尝试通过在 table 构造函数中实际创建 table 或通过在对象上显式调用 createTable()
来修复它。
所以要么 Screen
做。
// Tabs
tabbedPane.add("List", PList);
tabbedPane.add("Chart", PChart);
add(tabbedPane);
studentTable.createTable(); // Create the table
PList.add(studentTable);
validate(); // Revalidate the frame
或在 StudentTable
class 中创建 table。
public StudentTable() {
createTable();
}
看,就这么简单。现在有用吗?并不真地。 StudentTable 是一个 Panel
,你实际上从来没有向它添加任何东西。所以您只是将一个空面板添加到您的 TabbedPane。
您需要在某个时候实际将 table 添加到面板。所以回到StudentTable
Class,在createTable()
方法的末尾添加add(table)
。或者,如果您在构造函数中调用 createTable(),您还可以在构造函数中的 createTable().
下面添加这一行
public StudentTable() {
createTable();
add(table);
}
你现在能看到 table 吗?是的你可以。你能看到 table 中的所有值吗?你不能。那是因为 table 不可滚动。您需要将其添加到 ScrollPane
.
最简单的方法是在 StudentTable
Class 中创建一个 ScrollPane
。将 Table
添加到 ScrollPane
,然后将 ScrollPane
添加到 Panel
。
回到你在 StudentTable
中写 add(table)
的地方 Class 并 将 替换为
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
add(scrollPane);
这应该可以暂时解决您的问题。
另外,我想指出您使用了太多 class 字段。例如在 StudentTable
Class, name
, lastName
, letterGrade
, studentId
, quiz1
, quiz2
, project
, midterm
, finalGrade
, average
和 scanner
作为字段是没有用的。除了一个以外,其他都不是 table 的属性,它们不代表 table 的实例。它们代表 Student
我建议你看一下 this answer 的相关问题。
You should never use a field to simplify passing data from one method to another method. That's simply not its purpose. Doing so also makes your methods intrinsically thread unsafe or require synchronization.
您也不需要在每个 class 中都使用 main()
方法。它只是作为代码启动时的入口点,仅此而已。
关于getter和setter,只添加你需要的。您的 screen
Class 访问了 StudentTable
class 中的 none 个字段(可以理解,因为其中大多数都不合理,并且对其他 classes),但你对它们中的每一个都有一个 getter 和 setter。
如果您确实有很多 getter 和 setter,将它们放在末尾,这样就不必滚动 20 分钟来访问 class.[=52 中的任何实际实现=]
我可能会这样写 StudentTable
Class.
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class StudentTablePanel extends JPanel {
private static JTable table;
public StudentTablePanel() {
table = createTable(getStudentList()); // This line—in my opinion—makes it more immediately understandable what is happening, you're creating a table from a list of students.
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
add(scrollPane);
}
// Reading "Students.txt" file and adding students to the "studentList" ArrayList.
public ArrayList<Students> getStudentList() {
ArrayList<Students> list = new ArrayList<>();
try (Scanner scanner = new Scanner(new URL("http://rawsly.com/Students.txt").openStream())) { // Try with resources loop automatically closes the scanner when done
while (scanner.hasNext()) {
String name = scanner.next();
String lastName = scanner.next();
long studentId = scanner.nextLong();
double quiz1 = scanner.nextDouble();
double quiz2 = scanner.nextDouble();
double project = scanner.nextDouble();
double midterm = scanner.nextDouble();
double finalGrade = scanner.nextDouble();
double average = scanner.nextDouble();
String letterGrade = scanner.next();
list.add(new Students(name, lastName, studentId, quiz1, quiz2, project, midterm, finalGrade, average, letterGrade));
}
} catch (IOException ex) {
// Handle your exception
}
return list;
}
public JTable createTable(ArrayList<Students> studentList) {
String[] columnNames = {"NAME", "SURNAME", "ID", "QUIZ1", "QUIZ2", "PROJECT", "MIDTERM", "FINAL", "AVERAGE", "LETTER GRADE"};
int row = studentList.size();
int column = columnNames.length;
Object[][] data = new Object[row][column];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
switch (j) {
case 0:
data[i][0] = studentList.get(i).getName();
break;
case 1:
data[i][1] = studentList.get(i).getLastName();
break;
case 2:
data[i][2] = studentList.get(i).getStudentId();
break;
case 3:
data[i][3] = studentList.get(i).getQuiz1();
break;
case 4:
data[i][4] = studentList.get(i).getQuiz2();
break;
case 5:
data[i][5] = studentList.get(i).getProject();
break;
case 6:
data[i][6] = studentList.get(i).getMidterm();
break;
case 7:
data[i][7] = studentList.get(i).getFinalGrade();
break;
case 8:
data[i][8] = studentList.get(i).getAverage();
break;
case 9:
data[i][9] = studentList.get(i).getLetterGrade();
} // end of the switch
} // end of the first for loop
} // end of the second for loop
JTable newTable= new JTable(data, columnNames) {
@Override
public boolean isCellEditable(int row, int column) { // To make row and columns not editable
return false;
}
};
newTable.setFillsViewportHeight(true);
newTable.setAutoCreateRowSorter(true); // to activate sorting property of each column
return newTable;
}
}
我正在编写一个显示 800 名学生的 GUI table,其中学生的信息是从 txt 文件中读取的。我写了一个 Screen() class 和 StudentTable() class,其中 Screen class 有带有两个 JPanel 选项卡 "PList" 和 "PChart" 的 TabbedPane。我想将我的 StudentTable class 添加到我的 PList JPanel 中。所以当我 运行 屏幕 class 时,它会显示 StudentTable class 里面有学生 table。通常,我设法在没有标签的情况下显示我的 table。
首先,我打开 "Students.txt" 文件,然后读取该文件。然后我关闭了文件。之后,我创建了一个创建 table 的无效方法 createTable()。 table 从 ArrayList 获取信息。现在,我想将此 class 添加到我的 PList JPanel。代码有点长。所以我上传了它们。不过,如果你想让我分享代码,我也可以。
Screen.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class Screen extends JFrame implements ActionListener {
private JTabbedPane tabbedPane;
private JPanel PList, PChart;
private JMenuBar menuBar;
private JMenu menuStudent, menuSort, menuFilter;
private JMenuItem addStudent, removeStudent, removeAllStudents;
private JMenuItem sortLastName, sortId, sortAverage;
private JMenuItem filterLetterGrade, removeFilter;
private StudentTable studentTable;
public Screen() {
// TODO Auto-generated constructor stub
tabbedPane = new JTabbedPane();
menuBar = new JMenuBar();
menuStudent = new JMenu();
menuSort = new JMenu();
menuFilter = new JMenu();
addStudent = new JMenuItem();
removeStudent = new JMenuItem();
removeAllStudents = new JMenuItem();
sortLastName = new JMenuItem();
sortId = new JMenuItem();
sortAverage = new JMenuItem();
filterLetterGrade = new JMenuItem();
removeFilter = new JMenuItem();
PList = new JPanel();
PChart = new JPanel();
studentTable = new StudentTable();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setExtendedState(MAXIMIZED_BOTH);
setTitle("Screen");
setLayout(new BorderLayout());
setJMenuBar(menuBar);
// Menu
menuStudent.setText("Student");
addStudent.setText("Add Student");
addStudent.addActionListener(this);
removeStudent.setText("Remove Student");
removeStudent.addActionListener(this);
removeAllStudents.setText("Remove All Students");
removeAllStudents.addActionListener(this);
menuStudent.add(addStudent);
menuStudent.add(removeStudent);
menuStudent.add(removeAllStudents);
menuSort.setText("Sort");
sortLastName.setText("Sort by last name");
sortLastName.addActionListener(this);
sortId.setText("Sort by ID");
sortId.addActionListener(this);
sortAverage.setText("Sort by average");
sortAverage.addActionListener(this);
menuSort.add(sortLastName);
menuSort.add(sortId);
menuSort.add(sortAverage);
menuFilter.setText("Filter");
filterLetterGrade.setText("Filter by letter grade");
filterLetterGrade.addActionListener(this);
removeFilter.setText("Remove filter");
removeFilter.addActionListener(this);
menuFilter.add(filterLetterGrade);
menuFilter.add(removeFilter);
menuBar.add(menuStudent);
menuBar.add(menuSort);
menuBar.add(menuFilter);
// Tabs
tabbedPane.add("List", PList);
tabbedPane.add("Chart", PChart);
add(tabbedPane);
PList.add(studentTable);
}
public static void main(String[] args) {
new Screen();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
StudentTable.java
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class StudentTable extends JPanel {
private static JTable table;
private String name, lastName, letterGrade;
private long studentId;
private double quiz1,quiz2,project,midterm,finalGrade,average;
private static ArrayList<Students> studentList;
private Scanner scanner;
public StudentTable() {
}
/**
*
* @param name
* @param lastName
* @param studentId
* @param quiz1
* @param quiz2
* @param project
* @param midterm
* @param finalGrade
* @param average
* @param letterGrade
*/
public StudentTable(String name, String lastName, long studentId, double quiz1, double quiz2, double project, double midterm, double finalGrade, double average, String letterGrade) {
this.name = name;
this.lastName = lastName;
this.letterGrade = letterGrade;
this.studentId = studentId;
this.quiz1 = quiz1;
this.quiz2 = quiz2;
this.project = project;
this.midterm = midterm;
this.finalGrade = finalGrade;
this.average = average;
}
// GET & SET METHODS
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLetterGrade() {
return letterGrade;
}
public void setLetterGrade(String letterGrade) {
this.letterGrade = letterGrade;
}
public long getStudentId() {
return studentId;
}
public void setStudentId(long studentId) {
this.studentId = studentId;
}
public double getQuiz1() {
return quiz1;
}
public void setQuiz1(double quiz1) {
this.quiz1 = quiz1;
}
public double getQuiz2() {
return quiz2;
}
public void setQuiz2(double quiz2) {
this.quiz2 = quiz2;
}
public double getProject() {
return project;
}
public void setProject(double project) {
this.project = project;
}
public double getMidterm() {
return midterm;
}
public void setMidterm(double midterm) {
this.midterm = midterm;
}
public double getFinalGrade() {
return finalGrade;
}
public void setFinalGrade(double finalGrade) {
this.finalGrade = finalGrade;
}
public double getAverage() {
return average;
}
public void setAverage(double average) {
this.average = average;
}
public ArrayList<Students> getStudentList() {
return studentList;
}
public void setStudentList(ArrayList<Students> studentList) {
this.studentList = studentList;
}
// Opening "Students.txt" file from a url.
public void openFile() {
try {
URL url = new URL("http://rawsly.com/Students.txt");
scanner = new Scanner(url.openStream());
} catch (Exception e) {
// TODO: handle exception
System.out.println("Error: " + e.getMessage());
}
}
// Reading "Students.txt" file and adding students to the "studentList" ArrayList.
public void readFile() {
studentList = new ArrayList<>();
while(scanner.hasNext()) {
name = scanner.next();
lastName = scanner.next();
studentId = scanner.nextLong();
quiz1 = scanner.nextDouble();
quiz2 = scanner.nextDouble();
project = scanner.nextDouble();
midterm = scanner.nextDouble();
finalGrade = scanner.nextDouble();
average = scanner.nextDouble();
letterGrade = scanner.next();
studentList.add(new Students(name, lastName, studentId, quiz1, quiz2, project, midterm, finalGrade, average, letterGrade));
}
}
// Closing "Students.txt" file.
public void closeFile() {
scanner.close();
}
public void createTable() {
openFile();
readFile();
closeFile();
String [] columnNames = {"NAME", "SURNAME", "ID", "QUIZ1", "QUIZ2", "PROJECT", "MIDTERM", "FINAL", "AVERAGE", "LETTER GRADE"};
Object [][] data = new Object[studentList.size()][10];
for(int i=0; i<studentList.size(); i++) {
for(int j=0; j<10; j++) {
switch (j) {
case 0:
data[i][0] = studentList.get(i).getName();
break;
case 1:
data[i][1] = studentList.get(i).getLastName();
break;
case 2:
data[i][2] = studentList.get(i).getStudentId();
break;
case 3:
data[i][3] = studentList.get(i).getQuiz1();
break;
case 4:
data[i][4] = studentList.get(i).getQuiz2();
break;
case 5:
data[i][5] = studentList.get(i).getProject();
break;
case 6:
data[i][6] = studentList.get(i).getMidterm();
break;
case 7:
data[i][7] = studentList.get(i).getFinalGrade();
break;
case 8:
data[i][8] = studentList.get(i).getAverage();
break;
case 9:
data[i][9] = studentList.get(i).getLetterGrade();
break;
} // end of the switch
} // end of the first for loop
} // end of the second for loop
table = new JTable(data,columnNames) {
public boolean isCellEditable(int row, int column) { // To make row and columns not editable
return false;
}
};
table.setFillsViewportHeight(true);
table.setAutoCreateRowSorter(true); // to activate sorting property of each column
}
public static void main(String[] args) {
StudentTable s = new StudentTable();
s.createTable();
}
}
这就是您在屏幕 class.
中创建带有 Table 的面板的方式studentTable = new StudentTable();
这就是 StudentTable 的构造函数。
public StudentTable() {}
你看到这里的问题了吗?您正在调用一个空的构造函数。
您可以尝试通过在 table 构造函数中实际创建 table 或通过在对象上显式调用 createTable()
来修复它。
所以要么 Screen
做。
// Tabs
tabbedPane.add("List", PList);
tabbedPane.add("Chart", PChart);
add(tabbedPane);
studentTable.createTable(); // Create the table
PList.add(studentTable);
validate(); // Revalidate the frame
或在 StudentTable
class 中创建 table。
public StudentTable() {
createTable();
}
看,就这么简单。现在有用吗?并不真地。 StudentTable 是一个 Panel
,你实际上从来没有向它添加任何东西。所以您只是将一个空面板添加到您的 TabbedPane。
您需要在某个时候实际将 table 添加到面板。所以回到StudentTable
Class,在createTable()
方法的末尾添加add(table)
。或者,如果您在构造函数中调用 createTable(),您还可以在构造函数中的 createTable().
public StudentTable() {
createTable();
add(table);
}
你现在能看到 table 吗?是的你可以。你能看到 table 中的所有值吗?你不能。那是因为 table 不可滚动。您需要将其添加到 ScrollPane
.
最简单的方法是在 StudentTable
Class 中创建一个 ScrollPane
。将 Table
添加到 ScrollPane
,然后将 ScrollPane
添加到 Panel
。
回到你在 StudentTable
中写 add(table)
的地方 Class 并 将 替换为
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
add(scrollPane);
这应该可以暂时解决您的问题。
另外,我想指出您使用了太多 class 字段。例如在 StudentTable
Class, name
, lastName
, letterGrade
, studentId
, quiz1
, quiz2
, project
, midterm
, finalGrade
, average
和 scanner
作为字段是没有用的。除了一个以外,其他都不是 table 的属性,它们不代表 table 的实例。它们代表 Student
我建议你看一下 this answer 的相关问题。
You should never use a field to simplify passing data from one method to another method. That's simply not its purpose. Doing so also makes your methods intrinsically thread unsafe or require synchronization.
您也不需要在每个 class 中都使用 main()
方法。它只是作为代码启动时的入口点,仅此而已。
关于getter和setter,只添加你需要的。您的 screen
Class 访问了 StudentTable
class 中的 none 个字段(可以理解,因为其中大多数都不合理,并且对其他 classes),但你对它们中的每一个都有一个 getter 和 setter。
如果您确实有很多 getter 和 setter,将它们放在末尾,这样就不必滚动 20 分钟来访问 class.[=52 中的任何实际实现=]
我可能会这样写 StudentTable
Class.
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class StudentTablePanel extends JPanel {
private static JTable table;
public StudentTablePanel() {
table = createTable(getStudentList()); // This line—in my opinion—makes it more immediately understandable what is happening, you're creating a table from a list of students.
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
add(scrollPane);
}
// Reading "Students.txt" file and adding students to the "studentList" ArrayList.
public ArrayList<Students> getStudentList() {
ArrayList<Students> list = new ArrayList<>();
try (Scanner scanner = new Scanner(new URL("http://rawsly.com/Students.txt").openStream())) { // Try with resources loop automatically closes the scanner when done
while (scanner.hasNext()) {
String name = scanner.next();
String lastName = scanner.next();
long studentId = scanner.nextLong();
double quiz1 = scanner.nextDouble();
double quiz2 = scanner.nextDouble();
double project = scanner.nextDouble();
double midterm = scanner.nextDouble();
double finalGrade = scanner.nextDouble();
double average = scanner.nextDouble();
String letterGrade = scanner.next();
list.add(new Students(name, lastName, studentId, quiz1, quiz2, project, midterm, finalGrade, average, letterGrade));
}
} catch (IOException ex) {
// Handle your exception
}
return list;
}
public JTable createTable(ArrayList<Students> studentList) {
String[] columnNames = {"NAME", "SURNAME", "ID", "QUIZ1", "QUIZ2", "PROJECT", "MIDTERM", "FINAL", "AVERAGE", "LETTER GRADE"};
int row = studentList.size();
int column = columnNames.length;
Object[][] data = new Object[row][column];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
switch (j) {
case 0:
data[i][0] = studentList.get(i).getName();
break;
case 1:
data[i][1] = studentList.get(i).getLastName();
break;
case 2:
data[i][2] = studentList.get(i).getStudentId();
break;
case 3:
data[i][3] = studentList.get(i).getQuiz1();
break;
case 4:
data[i][4] = studentList.get(i).getQuiz2();
break;
case 5:
data[i][5] = studentList.get(i).getProject();
break;
case 6:
data[i][6] = studentList.get(i).getMidterm();
break;
case 7:
data[i][7] = studentList.get(i).getFinalGrade();
break;
case 8:
data[i][8] = studentList.get(i).getAverage();
break;
case 9:
data[i][9] = studentList.get(i).getLetterGrade();
} // end of the switch
} // end of the first for loop
} // end of the second for loop
JTable newTable= new JTable(data, columnNames) {
@Override
public boolean isCellEditable(int row, int column) { // To make row and columns not editable
return false;
}
};
newTable.setFillsViewportHeight(true);
newTable.setAutoCreateRowSorter(true); // to activate sorting property of each column
return newTable;
}
}