我的申请处理患者记录。在主框架中,用户可以打开几个内部框架。每个内部框架包含一个选项卡式窗格,用户创建的每个选项卡都包含一个表单,用于输入患者的数据和一个jtable,其中显示所有添加的患者。
当用户点击jtable中的行(患者)时,表单的字段由患者的数据填充,当他按下“Escape”时,表单的字段被清除,用户可以继续搜索/检查/输入另一个患者。
我的问题是这个转义键事件,在我使用的Substance look and feel中引发了classCastException。我为执行的操作编写的代码工作正常。这个问题出现了,因为我开始使用选项卡式窗格(在一个窗格中完成所有内容之前)。如果我改变了对于例如Windows的外观和感觉,则不会抛出任何异常。你有什么主意吗?
这是一个代码示例:
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;
}
您提到您已切换到使用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();
}
可能是你需要做的改变。
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();
}
}
});