返回自定义JDialog的值(Return value from custom JDialog)

编程入门 行业动态 更新时间:2024-10-23 11:24:54
返回自定义JDialog的值(Return value from custom JDialog)

我发现这已被多次询问,并提前道歉,如果我只是想念一些简单的事情......

我用这里的Java文档中提供的示例创建了一个自定义的JDialog,并从这里提出了一个类似的问题。

我的主要应用程序是一个JFrame,其中包含一个带有一系列JButton的JPanel,它们显示各种员工姓名。 我为每个调用所提到的JDialog的JButton添加了一个自定义ActionListener:

//inner class for handling user button pushes private class UserButtonHandler implements ActionListener { //handle button event @Override public void actionPerformed(ActionEvent event) { statusDialog = new ChangeDialog(statusWindow); statusDialog.setVisible(true); statusDialog.setLocationRelativeTo(null); //set title for dialog box String dialogTitle = "Status change for " + event.getActionCommand(); statusDialog.setTitle(dialogTitle); statNum = ((ChangeDialog) statusDialog).getInputStatus(); System.out.println("Current num is: " + statNum); //statNum = statusDialog.getInputStatus(); } }

这里是自定义JDialog ( ChangeDialog )的类:

class ChangeDialog extends JDialog implements ActionListener, PropertyChangeListener { //create panel where users can modify their status private final ChangePanel empStatusChangePanel; //text of buttons in dialog private String btnString1 = "OK"; private String btnString2 = "Cancel"; private String btnString3 = "Clear Time-Off"; private JOptionPane statusPane; //determines message to return for user input private int inputStatus; public ChangeDialog(JFrame statusFrame) { empStatusChangePanel = new ChangePanel(); //create an array specifying the number //of dialog buttons and their text Object[] options = {btnString1, btnString2, btnString3}; //create the JOptionPane statusPane = new JOptionPane(empStatusChangePanel, JOptionPane.PLAIN_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION, null, options, options[0]); //set contents of dialog setContentPane(statusPane); //handle window closing setDefaultCloseOperation(DISPOSE_ON_CLOSE); //register event handler for changes in status pane state statusPane.addPropertyChangeListener(this); pack(); } @Override public void actionPerformed(ActionEvent e) { statusPane.setValue(btnString1); } @Override public void propertyChange(PropertyChangeEvent e) { String prop = e.getPropertyName(); if (isVisible() && (e.getSource() == statusPane) && (JOptionPane.VALUE_PROPERTY.equals(prop))) { Object value = statusPane.getValue(); if (value == JOptionPane.UNINITIALIZED_VALUE) { //ignore reset return; } //Reset the JOptionPane's value. If this is not done, //then if the user presses the same button next time, //no property change event will be fired statusPane.setValue(JOptionPane.UNINITIALIZED_VALUE); if(value.equals(btnString1)) //user clicked "OK" { //validation of user input inputStatus = empStatusChangePanel.validateUserInput(); //handle validation results switch (inputStatus) { case 0: //user input is good JOptionPane.showMessageDialog(this, "Good input given"); dispose(); break; case 1: //one (or both) of the date pickers are empty JOptionPane.showMessageDialog(this, "PTO pickers can't be empty.", "ERROR", JOptionPane.ERROR_MESSAGE); break; case 2: case 3: //bad date range (start before end or visa-versa) JOptionPane.showMessageDialog(this, "Bad date range.", "ERROR", JOptionPane.ERROR_MESSAGE); break; case 99: //dates are equal JOptionPane.showMessageDialog(this, "Single-day PTO"); dispose(); break; } } else if(value.equals(btnString3)) //user clicked "Clear Input" { JOptionPane.showMessageDialog(this, "User clicked 'clear input"); //more processing should be done here empStatusChangePanel.recycle(); //dispose(); } else //user clicked "Cancel" or closed dialog { JOptionPane.showMessageDialog(this, "User closed status window"); dispose(); } } } //returns value from user validation public int getInputStatus() { return inputStatus; } }

我需要从自定义对话框中访问方法getInputStatus ,但是我尝试过的每一次尝试都回来说明:

getInputStatus is undefined for the type JDialog

我看过其他几个类似的帖子,但觉得我在尝试解决这个问题时失去了一些基本的东西(或者我一直在看代码太长)。

另一件我难住的事(为什么我把它留在第一个片段中)是,如果我将方法转换为ChangeDialog类型

statNum = ((ChangeDialog) statusDialog).getInputStatus();

它突然有访问权限(这是来自Eclipse的建议,对我来说没有意义)。 再次感谢您的帮助。

I see this has been asked multiple times and apologize in advance if I'm just missing something simple...

I've created a custom JDialog with the examples provided in the Java docs here and from a similar question asked here.

My main application is a JFrame that contains a JPanel with an array of JButtons that display various employee names. I've added a custom ActionListener to each JButton that calls the mentioned JDialog:

//inner class for handling user button pushes private class UserButtonHandler implements ActionListener { //handle button event @Override public void actionPerformed(ActionEvent event) { statusDialog = new ChangeDialog(statusWindow); statusDialog.setVisible(true); statusDialog.setLocationRelativeTo(null); //set title for dialog box String dialogTitle = "Status change for " + event.getActionCommand(); statusDialog.setTitle(dialogTitle); statNum = ((ChangeDialog) statusDialog).getInputStatus(); System.out.println("Current num is: " + statNum); //statNum = statusDialog.getInputStatus(); } }

Here is the class for the custom JDialog (ChangeDialog):

class ChangeDialog extends JDialog implements ActionListener, PropertyChangeListener { //create panel where users can modify their status private final ChangePanel empStatusChangePanel; //text of buttons in dialog private String btnString1 = "OK"; private String btnString2 = "Cancel"; private String btnString3 = "Clear Time-Off"; private JOptionPane statusPane; //determines message to return for user input private int inputStatus; public ChangeDialog(JFrame statusFrame) { empStatusChangePanel = new ChangePanel(); //create an array specifying the number //of dialog buttons and their text Object[] options = {btnString1, btnString2, btnString3}; //create the JOptionPane statusPane = new JOptionPane(empStatusChangePanel, JOptionPane.PLAIN_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION, null, options, options[0]); //set contents of dialog setContentPane(statusPane); //handle window closing setDefaultCloseOperation(DISPOSE_ON_CLOSE); //register event handler for changes in status pane state statusPane.addPropertyChangeListener(this); pack(); } @Override public void actionPerformed(ActionEvent e) { statusPane.setValue(btnString1); } @Override public void propertyChange(PropertyChangeEvent e) { String prop = e.getPropertyName(); if (isVisible() && (e.getSource() == statusPane) && (JOptionPane.VALUE_PROPERTY.equals(prop))) { Object value = statusPane.getValue(); if (value == JOptionPane.UNINITIALIZED_VALUE) { //ignore reset return; } //Reset the JOptionPane's value. If this is not done, //then if the user presses the same button next time, //no property change event will be fired statusPane.setValue(JOptionPane.UNINITIALIZED_VALUE); if(value.equals(btnString1)) //user clicked "OK" { //validation of user input inputStatus = empStatusChangePanel.validateUserInput(); //handle validation results switch (inputStatus) { case 0: //user input is good JOptionPane.showMessageDialog(this, "Good input given"); dispose(); break; case 1: //one (or both) of the date pickers are empty JOptionPane.showMessageDialog(this, "PTO pickers can't be empty.", "ERROR", JOptionPane.ERROR_MESSAGE); break; case 2: case 3: //bad date range (start before end or visa-versa) JOptionPane.showMessageDialog(this, "Bad date range.", "ERROR", JOptionPane.ERROR_MESSAGE); break; case 99: //dates are equal JOptionPane.showMessageDialog(this, "Single-day PTO"); dispose(); break; } } else if(value.equals(btnString3)) //user clicked "Clear Input" { JOptionPane.showMessageDialog(this, "User clicked 'clear input"); //more processing should be done here empStatusChangePanel.recycle(); //dispose(); } else //user clicked "Cancel" or closed dialog { JOptionPane.showMessageDialog(this, "User closed status window"); dispose(); } } } //returns value from user validation public int getInputStatus() { return inputStatus; } }

I need to access the method getInputStatus from the custom dialog but each attempt I've tried comes back stating that:

getInputStatus is undefined for the type JDialog

I have looked at several other similar posts but feel that I'm missing something fundamental in trying to solve this problem (or I've been looking at the code too long).

Another thing that has me stumped (and why I left it in the first snippet) is that if I cast the method to the type ChangeDialog

statNum = ((ChangeDialog) statusDialog).getInputStatus();

It suddenly has access (this was a suggestion from Eclipse and doesn't make sense to me). Thanks again for any and all help.

最满意答案

这就是继承的工作原理,你已经将statusDialog定义为JDialog引用,并且JDialog没有getInputStatus方法。 要访问ChangeDialog的成员,必须将statusDialog定义为ChangeDialog的变量。

That is how inheritance works, you have defined statusDialog as a JDialog reference and JDialog doesn't have a getInputStatus method. To access the members of ChangeDialog, you have to define statusDialog as variable of ChangeDialog.

更多推荐

本文发布于:2023-07-30 21:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340355.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   JDialog   custom   Return

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!