如何在 Netbeans 上为 Java 桌面应用程序设置单选按钮初始值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15951883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:24:38  来源:igfitidea点击:

how to set radio buttons initial value on Netbeans for a Java desktop application?

javaswingnetbeansjradiobutton

提问by gtludwig

This app I'm working on has three radio buttons, but I need to open the JFrame with one of them selected and the other two not.

我正在开发的这个应用程序有三个单选按钮,但我需要打开 JFrame 并选择其中一个,而不选择另外两个。

Upon the JFrame load, I call the following method:

在 JFrame 加载时,我调用以下方法:

private void initJRadio() {
    jRadioButton1.setSelected(false);
    jRadioButton2.setSelected(true);
    jRadioButton3.setSelected(false);
}

But I get the following exception upon loading the JFrame:

但是在加载 JFrame 时出现以下异常:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at StockJFrame.initJRadio(StockJFrame.java:139)

StockJFrame.initJRadio(StockJFrame.java:139) 处的线程“AWT-EventQueue-0”java.lang.NullPointerException 中的异常

Where StockJFrame is the class name and 139 is the line number for "jRadioButton1.setSelected(false);"

其中 StockJFrame 是类名,139 是“jRadioButton1.setSelected(false);”的行号。

On the Source pane for this class, Netbeans has added these lines:

在此类的 Source 窗格中,Netbeans 添加了以下几行:

jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();

jRadioButton1.setText(/*label value*/);
jRadioButton1.setToolTipText(/*some tooltip text*/);
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        jRadioButton1ActionPerformed(evt);
    }
});

jRadioButton2.setText(/*label value*/);
jRadioButton2.setToolTipText(/*some tooltiptext*/);
jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        jRadioButton2ActionPerformed(evt);
    }
});

jRadioButton3.setText(/*label value*/);
jRadioButton3.setToolTipText(/*some tooltip text*/);
jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        jRadioButton3ActionPerformed(evt);
    }
});

How to correctly set this up?

如何正确设置?

采纳答案by acdcjunior

At some point in your code, the jRadioButton1(and others) must be (they probably already are) initialized through a jRadioButton1 = new javax.swing.JRadioButton().

在您的代码中的某个时刻,jRadioButton1(和其他)必须(它们可能已经)通过jRadioButton1 = new javax.swing.JRadioButton().

If I'm not mistaken, NetBeans generated code, by default, does that initialization in a method called initComponents(). Also, initComponents()is usually called at the constructor.

如果我没记错的话,默认情况下,NetBeans 生成的代码会在名为initComponents(). 此外,initComponents()通常在构造函数中调用。

Find out where the initialization is taking place (initComponents()or elsewhere) and make sure initJRadio()is only called afterthat.

找出初始化发生的位置(initComponents()或其他地方)并确保initJRadio()之后调用 。

Update:

更新:

After you posted more of your code, you can put the initJRadio()call right after the last command you pasted. (Namely, jRadioButton3.addActionListener(new ... });).

发布更多代码后,您可以initJRadio()在粘贴的最后一个命令之后立即调用。(即,jRadioButton3.addActionListener(new ... });)。

PS.: The java.lang.NullPointerExceptionmeans that your object is yet null, that is, as pointed above, it has not yet been initialized.

PS.:java.lang.NullPointerException表示你的对象还没有null,也就是上面指出的,它还没有被初始化。