我的代码完全符合要求
package com.grantbroadwater.signInAssistant.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import org.junit.Test;
import com.grantbroadwater.school.Student;
public class SignInSheetTableTest implements ActionListener{
static SignInSheetTableModel model;
static JTextField tfFirst, tfLast;
static JTable table;
public SignInSheetTableTest() {
// TODO Auto-generated constructor stub
}
private static JPanel createContentPanel(){
JPanel panel = new JPanel(new GridLayout(1, 0));
table = new JTable(new SignInSheetTableModel());
model = (SignInSheetTableModel)table.getModel();
table.setPreferredScrollableViewportSize(new Dimension(500, 80));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane);
return panel;
}
private static void createAndShowGUI(){
JFrame frame = new JFrame("Table Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = SignInSheetTableTest.createContentPanel();
panel.setOpaque(true);
JPanel entryPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
tfFirst = new JTextField(8);
tfLast = new JTextField(8);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new SignInSheetTableTest());
JButton btnDelete = new JButton("Delete");
btnDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int rowIndex = table.getSelectedRow();
model.deleteRow(rowIndex);
}
});
entryPanel.add(tfFirst);
entryPanel.add(tfLast);
entryPanel.add(btnAdd);
entryPanel.add(btnDelete);
JPanel housingPanel = new JPanel(new BorderLayout());
housingPanel.add(entryPanel, BorderLayout.NORTH);
housingPanel.add(panel, BorderLayout.CENTER);
frame.setContentPane(housingPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
String first = tfFirst.getText();
String last = tfLast.getText();
Student s = new Student(first, last, "11111");
model.addStudent(s);
table.scrollRectToVisible(table.getCellRect(table.getRowCount() - 1, 0, true));
}
@Test
public void test(){
main(null);
try {
Thread.sleep(15000);
} catch (InterruptedException e) {}
}
}
但是,当我将JTable实例更改为JTable的SignInSheetTable
子类,并将构造函数更改为new SignInSheetTable()
时,程序仍将执行,但不会出现JTable。我找不到任何解决方案,并且我完全不知道为什么子类不能工作时超类会在下面显示整个SignInSheetTable
类。
package com.grantbroadwater.signInAssistant.view;
import javax.swing.JTable;
import com.grantbroadwater.school.Student;
import com.grantbroadwater.util.Log;
import com.grantbroadwater.util.Log.LogType;
public class SignInSheetTable extends JTable{
private static final long serialVersionUID = 1L;
private SignInSheetTableModel model;
public SignInSheetTable() {
super(new SignInSheetTableModel());
model = (SignInSheetTableModel) super.getModel();
this.setFillsViewportHeight(true);
new Log(LogType.DEBUG, "new Sign In sheet");
}
public SignInSheetTableModel getModel() {
return model;
}
public void addStudent(Student student) {
model.addStudent(student);
}
public void deleteRow(int rowIndex) {
model.deleteRow(rowIndex);
}
}
答案 0 :(得分:3)
覆盖getModel
似乎会导致程序其余部分出现问题。无论如何,这并不是你想要实现的目标所必需的。
就个人而言,我不打扰自定义表,你没有在类中添加任何新功能,这些功能无法直接通过模型进行管理,无论如何都应该进行管理
我也担心过度使用static
,因为随着程序变大,这可能会给你带来一些问题