我有這樣的代碼,工程完全按照所需JTable的作品,但JTable的子類不會
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的實例爲SignInSheetTable
子類的JTable,並改變構造函數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);
}
}
有沒有真正的點覆蓋getModel – MadProgrammer 2015-04-05 03:03:14
真的,我想我沒想到關於這一點,你認爲這就是我的JTable沒有顯示的原因嗎? – Grant 2015-04-05 03:04:56
在十多年的Swing編程中,我需要將*** JTable精確地延長到*** 0 ***次。 – 2015-04-05 03:24:22