3

我的应用程序处理患者记录。在主框架中,用户可以打开几个内部框架。每个内部框架都包含一个选项卡式窗格,用户创建的每个选项卡都包含一个可以输入患者数据的表单和一个显示所有已添加患者的 jtable。

当用户单击 jtable 中的一行(患者)时,表单的字段由患者的数据填充,当他按下“Escape”时,表单的字段被清除,用户可以继续搜索/检查/输入另一个患者。

我的问题是这个转义键事件在我使用的Substance 外观中引发了一个 classCastException。我为执行的操作编写的代码运行良好。自从我开始使用选项卡式窗格(在所有内容都在单个窗格中制作之前)时就出现了这个问题。如果我将外观更改为 egWindows,则不会引发异常。你有什么主意吗?

这是一个代码示例:

private void db_existKeyReleased(java.awt.event.KeyEvent evt) {
    // TODO add your handling code here:

    if(evt.getKeyCode()==KeyEvent.VK_ESCAPE)
    {
        searchField.requestFocusInWindow();         // if user doesn't want to process any of the entries shown to the table
        if(searchField.getText().length()>=1)       // focus goes to search field and data pane fields are cleared form previous shows
        {
            dataPane.setPid(-1);
            dataPane.getPersonalDataPane().clearAll();
            treat_diagPane.getDiagnosis_pane().clearAll();
            treat_diagPane.getTreat_pane().clearAll();
        }

        DefaultTableModel model=new DefaultTableModel(
                new Object [][] {
        },
        new String [] {
            bundle.getString("lname"), bundle.getString("fname"), bundle.getString("date_birth"), bundle.getString("occupation")
        });
        db_exist.setModel(model);
    }

这是抛出的异常:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JTabbedPane cannot be cast to javax.swing.JDesktopPane
    at javax.swing.plaf.basic.BasicDesktopPaneUI$Actions.actionPerformed(BasicDesktopPaneUI.java:329)
    at org.jvnet.lafwidget.tabbed.TabPagerWidget$4.actionPerformed(TabPagerWidget.java:158)

这是导致异常的代码:

public void actionPerformed(ActionEvent e) {
        JDesktopPane dp = (JDesktopPane)e.getSource();
        String key = getName();

        if (CLOSE == key || MAXIMIZE == key || MINIMIZE == key ||
                RESTORE == key) {
            setState(dp, key);
        }
        else if (ESCAPE == key) {
            if (sourceFrame == dp.getSelectedFrame() &&
                    focusOwner != null) {
                focusOwner.requestFocus();
            }
            moving = false;
            resizing = false;
            sourceFrame = null;
            focusOwner = null;
        }
4

2 回答 2

2

您提到您切换到使用 JTabbedPane,但在您的“actionPerformed”方法代码中,您仍然转换为“JDesktopPane”并且堆栈跟踪显示:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:   
javax.swing.JTabbedPane cannot be cast to javax.swing.JDesktopPane  at   
javax.swing.plaf.basic.BasicDesktopPaneUI$Actions.actionPerformed

大概改成:

if(e.getSource() instanceof JTabbedPane) {
    JTabbedPane = (JTabbedPane)e.getSource();
}

可能是你需要做的改变。

于 2010-01-11T11:34:08.453 回答
0

Use keybindings

    this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "fecharAction");
    this.getRootPane().getActionMap().put("fecharAction", new AbstractAction() {
        private static final long serialVersionUID = 1L;
        @Override
        public void actionPerformed(ActionEvent e) {
            int resp = JOptionPane.showConfirmDialog(MainForm.this, "Encerrar sistema?", "Confirmação", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (resp == 0) {
                MainForm.this.setVisible(false);
                MainForm.this.dispose();
            }
        }
    });
于 2011-07-27T02:16:57.743 回答