我仅从以下对象得到null:对象selectedValue = jPane.getValue(); (jPane =新的JOptionPane(...) [英] I get only null from: Object selectedValue = jPane.getValue(); (jPane = new JOptionPane(...)

查看:31
本文介绍了我仅从以下对象得到null:对象selectedValue = jPane.getValue(); (jPane =新的JOptionPane(...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    private void cancelButtonPressed() {  
    jPane = new JOptionPane("quit?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, buttons, buttons[1]);
    jPane.setName("WPane");
    dialog = jPane.createDialog(jPane.getParent(), "Confirm Dialog");

    dialog.setVisible(true);
    dialog.dispose();

    Object selectedValue = jPane.getValue();
    System.out.println("selectedValue:" + selectedValue);
    if(selectedValue == null)
        System.out.println("selectedValue:" + selectedValue);
        //return CLOSED_OPTION;
    if(buttons == null) {
        if(selectedValue instanceof Integer) {
            //return ((Integer)selectedValue).intValue();
            System.out.println("selectedValue instanceof Integer, selectedValue:" + selectedValue);
        //return CLOSED_OPTION;
        }
    }
    for(int counter = 0, maxCounter = buttons.length;
        counter < maxCounter; counter++) {
        if(buttons[counter].equals(selectedValue)){
            System.out.println("buttons[counter].equals(selectedValue) selectedValue:" + selectedValue);
            //return counter;
        }
    }
    //return CLOSED_OPTION;
}

在构造函数中,我设置按钮的名称:

in the constructor I set the names of the buttons:

    buttons[0]= new JButton("Yes");
    buttons[1]= new JButton("No");
    buttons[0].setName("Yes_Next_Btn");
    buttons[1].setName("No_Back_Btn");

问题是,我需要setNames到组件:JOptionPane和按钮.这样我就不用了:JOptionPane.showConfirmDialog.

The problem was, that I need to setNames to the components: JOptionPane, and the buttons. So that I don't use : JOptionPane.showConfirmDialog.

在这种情况下: 当我单击按钮时(是,否),我没有任何值,只有单击"X",退出时我会得到null.

And in this case: When I click on the buttons(Yes,No) I don't get any value, only if I click on the "X", for exit I get null.

推荐答案

如果将JButtons作为选项放入JOptionPane中,则需要指定单击它们时发生的情况.由于您似乎没有绑定到任何ActionActionListener的按钮,因此没有任何反应.

If you put JButtons as options into the JOptionPane, then YOU need to specify what happens when they are clicked. Since you don't appear to have any Action or ActionListener bound to your buttons, nothing happens.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class JOptionPaneTest {

    public void createAndShowJOptionPane() {
        final JButton b1 = new JButton("Yes, please");
        b1.setName("yes_please");
        final JButton b2 = new JButton("No, thx");
        b2.setName("no_thx");
        final JButton b3 = new JButton("Leave me alone");
        b3.setName("leave_me_alone");
        Object[] options = new Object[]{b1, b2, b3};

        final JOptionPane optionPane = new JOptionPane("Message", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION, null, options, options[2]);
        final JDialog dialog = optionPane.createDialog("Title");
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println(b1.getText());
                optionPane.setValue(0);
                dialog.dispose();
            }
        });
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println(b2.getText());
                optionPane.setValue(1);
                dialog.dispose();
            }
        });
        b3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println(b3.getText());
                optionPane.setValue(2);
                dialog.dispose();
            }
        });
        dialog.setVisible(true);

        if ((Integer)optionPane.getValue() == 0) {
            System.out.println("Woohoo!");
        }
    }

    public static void main(String[] argv) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JOptionPaneTest test = new JOptionPaneTest();
                test.createAndShowJOptionPane();
            }
        });
    }
}

这篇关于我仅从以下对象得到null:对象selectedValue = jPane.getValue(); (jPane =新的JOptionPane(...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆