4

在java中运行一个swing应用程序,我在我的程序中遇到了这个异常。值得注意的是,这并不是每次我运行程序时都会出现的。

完整的堆栈跟踪:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.backend.utils.importing.Loading$1.run(Loading.java:54)
    at org.backend.utils.importing.Loading.loadEverything(Loading.java:61)
    at org.main.BishopCarrollSchoolTracker.main(BishopCarrollSchoolTracker.java:70)
Caused by: java.lang.ClassCastException: javax.swing.plaf.FontUIResource cannot be cast to javax.swing.InputMap
    at javax.swing.plaf.basic.BasicButtonListener.getInputMap(BasicButtonListener.java:102)
    at javax.swing.plaf.basic.BasicButtonListener.installKeyboardActions(BasicButtonListener.java:78)
    at javax.swing.plaf.basic.BasicButtonUI.installKeyboardActions(BasicButtonUI.java:121)
    at javax.swing.plaf.basic.BasicButtonUI.installUI(BasicButtonUI.java:73)
    at javax.swing.JComponent.setUI(JComponent.java:662)
    at javax.swing.AbstractButton.setUI(AbstractButton.java:1782)
    at javax.swing.plaf.synth.SynthArrowButton.updateUI(SynthArrowButton.java:34)
    at javax.swing.AbstractButton.init(AbstractButton.java:2149)
    at javax.swing.JButton.<init>(JButton.java:118)
    at javax.swing.JButton.<init>(JButton.java:73)
    at javax.swing.plaf.synth.SynthArrowButton.<init>(SynthArrowButton.java:23)
    at javax.swing.plaf.synth.SynthScrollBarUI$2.<init>(SynthScrollBarUI.java:325)
    at javax.swing.plaf.synth.SynthScrollBarUI.createIncreaseButton(SynthScrollBarUI.java:325)
    at javax.swing.plaf.basic.BasicScrollBarUI.installComponents(BasicScrollBarUI.java:225)
    at javax.swing.plaf.basic.BasicScrollBarUI.installUI(BasicScrollBarUI.java:147)
    at javax.swing.JComponent.setUI(JComponent.java:662)
    at javax.swing.JScrollBar.setUI(JScrollBar.java:190)
    at javax.swing.JScrollBar.updateUI(JScrollBar.java:210)
    at javax.swing.JScrollBar.<init>(JScrollBar.java:144)
    at javax.swing.JScrollBar.<init>(JScrollBar.java:159)
    at javax.swing.JScrollPane$ScrollBar.<init>(JScrollPane.java:698)
    at javax.swing.JScrollPane.createHorizontalScrollBar(JScrollPane.java:794)
    at javax.swing.JScrollPane.<init>(JScrollPane.java:282)
    at javax.swing.JScrollPane.<init>(JScrollPane.java:305)
    at org.gui.base.generic.panels.ListTablePanel.<init>(ListTablePanel.java:44)
    at org.gui.base.main.internal.WorkPanel.<init>(WorkPanel.java:28)
    at org.gui.base.main.internal.InternalPanel.<clinit>(InternalPanel.java:38)
    ... 3 more

导致异常的行(在我的源代码中):

public static final WorkPanel WORK = new WorkPanel();

前往

super(new WorkTable(AllWork.getElements(), true, true, true, true, true, true, true),
            new WorkTable(AllWork.getElements(), true, true, true, true, true, true, true, true));

public ListTablePanel(RefreshableTable m, RefreshableTable t) {
    this.main = m;
    this.totals = t;

    setLayout(LayoutFactory.createLayout());

    JScrollPane pane = new JScrollPane(main);

    main.setAutoCreateRowSorter(false);

    totals.setFont(totals.getFont().deriveFont(Font.BOLD));
    totals.setEnabled(false);

    pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    add(pane, LayoutFactory.newFactory().setFill(GridBagConstraints.BOTH).
            setY(0).setWeightX(1).setWeightY(1));
    add(totals, LayoutFactory.newFactory().setFill(GridBagConstraints.BOTH).
            setY(1).setWeightX(1).setInsets(new Insets(0, 2, 0, 17)));
}

我源代码中的最后一段代码是JScrollPane.

我不明白为什么会抛出这个异常。

4

2 回答 2

7

每当我看到这个:

在java中运行一个swing应用程序,我在我的程序中遇到了这个异常。值得注意的是,这并不是每次我运行程序时都会出现的。

警钟在我脑海中响起,我想“我是否正确处理 Swing 线程”?因为 Swing 线程意外导致并不总是发生的奇怪错误并不少见。

那么,您是否通过首先在事件线程上启动和显示 Swing GUI 来正确处理 Swing 线程?IE,

private static void createAndShowGui() {

  // create and display my GUI here

}

public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
     public void run() {
        createAndShowGui();
     }
  });
}

顺便说一句,您没有使用 Nimbus 或其他外观吗?

于 2012-09-27T02:45:54.437 回答
1

修复了错误。发现我在 EDT 同时做 GUI 的东西。构造函数用了太久invokeLater(),开始用了invokeAndWait()

于 2012-09-27T03:26:25.763 回答