Java 我仅从以下位置获取空值:Object selectedValue=jPane.getValue();(jPane=newjoptionpane(…)

Java 我仅从以下位置获取空值:Object selectedValue=jPane.getValue();(jPane=newjoptionpane(…),java,swing,null,joptionpane,Java,Swing,Null,Joptionpane,问题是,我需要setNames到组件:JOptionPane和按钮。这样我就不用:JOptionPane.showConfirmDialog 在这种情况下: 当我点击按钮(是,否)时,我没有得到任何值,只有当我点击“X”时,对于退出,我得到null如果将JButtons作为选项放入JOptionPane,则需要指定单击按钮时发生的情况。由于您的按钮上似乎没有绑定任何操作或操作侦听器,因此不会发生任何事情 导入java.awt.event.ActionEvent; 导入java.awt.event

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

在这种情况下:
当我点击按钮(是,否)时,我没有得到任何值,只有当我点击“X”时,对于退出,我得到
null

如果将
JButtons
作为选项放入
JOptionPane
,则需要指定单击按钮时发生的情况。由于您的按钮上似乎没有绑定任何
操作
操作侦听器
,因此不会发生任何事情

导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JDialog;
导入javax.swing.JOptionPane;
导入javax.swing.SwingUtilities;
公共类JOptionPaneTest{
public void createAndShowJopOptions窗格(){
最终JButton b1=新JButton(“是的,请”);
b1.设置名称(“请选择”);
最终JButton b2=新JButton(“否,thx”);
b2.设置名称(“无thx”);
final JButton b3=新JButton(“别管我”);
b3.setName(“别管我”);
对象[]选项=新对象[]{b1、b2、b3};
final JOptionPane optionPane=new JOptionPane(“Message”,JOptionPane.QUESTION_Message,JOptionPane.YES_NO_CANCEL_OPTION,null,options,options[2]);
最终JDialog dialog=optionPane.createDialog(“标题”);
b1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
System.out.println(b1.getText());
optionPane.setValue(0);
dialog.dispose();
}
});
b2.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
System.out.println(b2.getText());
optionPane.setValue(1);
dialog.dispose();
}
});
b3.addActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件e){
System.out.println(b3.getText());
optionPane.setValue(2);
dialog.dispose();
}
});
对话框.setVisible(true);
if((整数)optionPane.getValue()==0){
System.out.println(“呜呼!”);
}
}
公共静态void main(字符串[]argv){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
JOptionPaneTest测试=新的JOptionPaneTest();
test.createAndShowJOptionPane();
}
});
}
}

如果将
JButtons
作为选项放入
JOptionPane
中,则需要指定单击按钮时发生的操作。由于您似乎没有任何
操作
操作侦听器
绑定到按钮,因此不会发生任何操作

导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JDialog;
导入javax.swing.JOptionPane;
导入javax.swing.SwingUtilities;
公共类JOptionPaneTest{
public void createAndShowJopOptions窗格(){
最终JButton b1=新JButton(“是的,请”);
b1.设置名称(“请选择”);
最终JButton b2=新JButton(“否,thx”);
b2.设置名称(“无thx”);
final JButton b3=新JButton(“别管我”);
b3.setName(“别管我”);
对象[]选项=新对象[]{b1、b2、b3};
final JOptionPane optionPane=new JOptionPane(“Message”,JOptionPane.QUESTION_Message,JOptionPane.YES_NO_CANCEL_OPTION,null,options,options[2]);
最终JDialog dialog=optionPane.createDialog(“标题”);
b1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
System.out.println(b1.getText());
optionPane.setValue(0);
dialog.dispose();
}
});
b2.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
System.out.println(b2.getText());
optionPane.setValue(1);
dialog.dispose();
}
});
b3.addActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件e){
System.out.println(b3.getText());
optionPane.setValue(2);
dialog.dispose();
}
});
对话框.setVisible(true);
if((整数)optionPane.getValue()==0){
System.out.println(“呜呼!”);
}
}
公共静态void main(字符串[]argv){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
JOptionPaneTest测试=新的JOptionPaneTest();
test.createAndShowJOptionPane();
}
});
}
}

问题是,
JOptionPane
没有向按钮添加任何侦听器,因此当您单击按钮时,什么都不会发生

尝试将按钮替换为
String
s

    buttons[0]= new JButton("Yes");
    buttons[1]= new JButton("No");
    buttons[0].setName("Yes_Next_Btn");
    buttons[1].setName("No_Back_Btn");
公共类测试选项窗格06{
公共静态void main(字符串[]args){
新的TestOptionPane06();
}
公共测试选项窗格06(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
//JButton按钮[]=新JButton[2];
//                
//按钮[0]=新的JButton(“是”);
//按钮[1]=新按钮(“否”);
//按钮[0]。设置名称(“是”下一个按钮“);
//钮扣[
    buttons[0]= new JButton("Yes");
    buttons[1]= new JButton("No");
    buttons[0].setName("Yes_Next_Btn");
    buttons[1].setName("No_Back_Btn");
public class TestOptionPane06 {

    public static void main(String[] args) {
        new TestOptionPane06();
    }

    public TestOptionPane06() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

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

                String values[] = new String[2];
                values[0] = "Yes";
                values[1] = "No";

                JOptionPane jPane = new JOptionPane("quit?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, values, values[1]);
                jPane.setName("WPane");
                JDialog 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 (values == 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 = values.length;
                                counter < maxCounter; counter++) {
                    if (values[counter].equals(selectedValue)) {
                        System.out.println("buttons[counter].equals(selectedValue) selectedValue:" + selectedValue);
                        //return counter;
                    }
                }
            }

        });
    }

}