当前位置: 首页 > 面试题库 >

我仅从以下对象得到null:对象selectedValue = jPane.getValue(); (jPane =新的JOptionPane(...)

谷梁淇
2023-03-14
问题内容
    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;
}

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

    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

并且在这种情况下:当我单击按钮(是,否)时,只有单击“ X”,我不会得到任何值,退出时我得到null


问题答案:

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

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();
            }
        });
    }
}


 类似资料:
  • 如您所知,< code>java.util.Objects是 这个类由用于操作对象的静态实用程序方法组成。 其中一个方法是< code>Objects.isNull()。 我的理解是< code>Objects.isNull()会通过省略第二个< code>=来消除意外地将空值赋给object的可能性。 但是,API注释指出: 此方法存在,可用作谓词、筛选器(对象::isNull) 是否有任何原因

  • 我尝试使用json对象更新sharePreference值。首先创建带有name的json对象,然后在SharedPreference中更新该对象。但是,我无法获得json对象名。请帮帮我. 我想像这个,。

  • 问题内容: 如你所知,是 此类包含用于对对象进行操作的静态实用程序方法。 此类方法之一是。 我的理解是,通过省略第二个,可以消除意外地为对象分配空值的机会。 但是,API注释指出: 存在此方法用作谓词filter(Objects :: isNull) 会不会有任何理由/环境对我应该使用了在 if语句 ? 应该只限于谓词吗? 问题答案: 应该在if语句中的Objects.isNull()上使用obj

  • 本文向大家介绍null是对象吗?相关面试题,主要包含被问及null是对象吗?时的应答技巧和注意事项,需要的朋友参考一下 根据ECMA规范,虽然返回,但实质上是值类型,其类型为,并且在中返回并不会做其他转换。

  • 问题内容: 就像是 这是我想象的格式,但事实并非如此。什么会退回到对象的父级? 问题答案: JavaScript本身不提供此功能。而且我怀疑您是否可以创建这种类型的功能。例如: 鲍比属于谁?