我什至不知道要问什么,只能发布代码并将错误消息放在这里。该程序应该创建一个带有两个选项卡的窗口,每个选项卡中都有一个分数表。
package components;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JTextPane;
public class TopLevelDemo extends JPanel{
public TopLevelDemo() {
JTabbedPane tabbedPane = new JTabbedPane();
String[] players = new String[4];
Object[][] singlesScores = new Object[2][4];
Object[][] doublesScores = new Object[2][4];
createScores(singlesScores);
createScores(doublesScores);
JComponent panel1 = makeScoreTable(singlesScores, players, 0, 0);
tabbedPane.addTab("Singles",panel1);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
JComponent panel2 = makeScoreTable(doublesScores, players, 0, 0);
tabbedPane.addTab("Doubles", panel2);
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
add(tabbedPane);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
protected JComponent makeScoreTable(Object[][] scores, String[] players, int x, int y) {
JPanel panel = new JPanel(true);
JTable table = new JTable(scores, players);
GridBagConstraints c = new GridBagConstraints();
c.gridx=x;
c.gridy=y;
panel.add(table, c);
return panel;
}
protected JComponent makeTextPanel(String text) {
JPanel panel = new JPanel(true);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(10, 25));
panel.add(filler);
return panel;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("TopLevelDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TopLevelDemo(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
private Object[][] createScores(Object[][] scores) {
scores[0][0]= new String("Home");
scores[0][1]= new Integer(0);
scores[0][2]= new Integer(0);
scores[0][3]= new Integer(0);
scores[1][0]= new String("Away");
scores[0][1]= new Integer(0);
scores[0][2]= new Integer(0);
scores[0][3]= new Integer(0);
return scores;
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
然后我得到错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.JTable$1.getColumnName(JTable.java:665)
at javax.swing.JTable.addColumn(JTable.java:2770)
at javax.swing.JTable.createDefaultColumnsFromModel(JTable.java:1264)
at javax.swing.JTable.tableChanged(JTable.java:4374)
at javax.swing.JTable.setModel(JTable.java:3676)
at javax.swing.JTable.<init>(JTable.java:612)
at javax.swing.JTable.<init>(JTable.java:553)
at javax.swing.JTable.<init>(JTable.java:664)
at components.TopLevelDemo.makeScoreTable(TopLevelDemo.java:30)
at components.TopLevelDemo.<init>(TopLevelDemo.java:17)
at components.TopLevelDemo.createAndShowGUI(TopLevelDemo.java:49)
at components.TopLevelDemo.access$000(TopLevelDemo.java:8)
at components.TopLevelDemo$1.run(TopLevelDemo.java:68)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:666)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:627)
at java.awt.EventQueue$2.run(EventQueue.java:625)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:636)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
有人知道我做错了什么或做得不好吗?