如何解决有没有一种方法可以在不禁用Java组件的组件的情况下打开其无模式JDialog的多个实例?
我有一个GUI,其中包含一个名为“ View”的JLabel:
,其中附加了mouseClickEvent。单击“查看” JLabel后,我可以打开一个无模式JDialog的多个实例。
JDialog本身由几个禁用的JTextField和一个JLabel组成,该JLabel附加了mouseClickEvent,它们充当“关闭”按钮:
但是,在打开多个JDialog时,将禁用“关闭” JLabel,并且mouseClickEvent不再起作用。
这是我的代码:
private void viewDoctorClickedEvent(java.awt.event.MouseEvent evt)
{
javax.swing.JFrame topFrame = (javax.swing.JFrame)
javax.swing.SwingUtilities.getWindowAncestor(this);
viewDoctorDialog = new javax.swing.JDialog(topFrame,"View Doctor Details",false);
viewDoctorDialog.setMinimumSize(new java.awt.Dimension(580,350));
viewDoctorDialog.setResizable(false);
viewDoctorDialog.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
viewDoctorDialog.setLocationRelativeTo(topFrame);
// This part is used to collect the values from the selected row of a Jtable and set the
// values to the disabled JTextFields in the JDialog
try
{
int selectedTableRow = doctorListTB.getSelectedRow();
String doctorName = doctorListTB.getValueAt(selectedTableRow,0).toString();
String doctorEmail = doctorListTB.getValueAt(selectedTableRow,1).toString();
String doctorPassword = doctorListTB.getValueAt(selectedTableRow,2).toString();
String doctorAddress = doctorListTB.getValueAt(selectedTableRow,3).toString();
String doctorPhone = doctorListTB.getValueAt(selectedTableRow,4).toString();
String doctorDepartment = doctorListTB.getValueAt(selectedTableRow,5).toString();
viewDoctorNameTF.setText(doctorName);
viewDoctorEmailTF.setText(doctorEmail);
viewDoctorPasswordTF.setText(doctorPassword);
viewDoctorAddressTF.setText(doctorAddress);
viewDoctorPhoneTF.setText(doctorPhone);
viewDoctorDepartmentTF.setText(doctorDepartment);
viewDoctorDialog.add(viewDoctorDialogPanel);
viewDoctorDialog.setVisible(true);
}
catch(Exception e)
{
javax.swing.JOptionPane.showMessageDialog(viewDoctorDialogPanel,"Please select a row to view.","No data to view.",javax.swing.JOptionPane.ERROR_MESSAGE);
}
}
// Mouse Click Event to close the JDialog upon clicking on the "close" JLabel
private void closeViewButtonClickedEvent(java.awt.event.MouseEvent evt)
{
viewDoctorDialog.dispose();
}
是否有一种方法可以打开多个实例,同时仍在每个JDialog的打开实例中保留“关闭” JLabel的mouseClickEvent?
解决方法
JDialog本身由几个禁用的JTextField和一个JLabel组成,该JLabel附加了mouseClickEvent,它们充当“关闭”按钮。
为什么要结合使用JLabel和MouseListener来关闭对话框? JLabel并非旨在进行这种处理。
使用JButton
和ActionListener
来实现“关闭”功能。
如果您希望按钮看起来像标签,则可以使用:
button.setBorderPainted( false );
mouseClickEvent不再起作用。
viewDoctorDialog = new javax.swing.JDialog(topFrame,"View Doctor Details",false);
在我看来,您有一个实例变量来跟踪打开的对话框。创建第二个对话框时,可以将变量的引用更改为指向第二个对话框,因此不再有对原始对话框的引用。
因此,按照上面的建议使用JButton
时,ActionListener
中用于“关闭”按钮的代码如下:
Jbutton button = (JButton)event.getSource();
Window window = SwingUtilities.windowForComponent( button );
window.dispose();
因此,您甚至不需要一个特殊变量即可跟踪对话框。
您的“查看医生详细信息”对话框应在单独的类中定义。该类所需的所有变量都应在该类中定义,而不是在显示对话框的主类中定义。因此,当您创建类的实例时,您将在JTable中传递。
这将使结构代码更容易,以便每个类仅包含与该类相关的ActionListener。因此,主类将包含用于显示子对话框的侦听器。子对话框将具有侦听器,以“关闭”对话框。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。