栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

如果将

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

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402032.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号