简体   繁体   English

基本的单选按钮问题-Java

[英]basic radiobutton issue - java

 package pkg411project;

 import javax.swing.ButtonGroup;

 public class CrudMain extends javax.swing.JInternalFrame {

public CrudMain() {
    initComponents();
}

@SuppressWarnings("unchecked")

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // TODO add your handling code here:
}                                             

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   BankRecordFrame bdFrame = new BankRecordFrame(); 
    bdFrame.setVisible(true);
    this.jDesktopPane2.add(bdFrame); 

}                                        

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JDesktopPane jDesktopPane2;
private javax.swing.JLabel jLabel1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JRadioButton jRadioButton4;
private javax.swing.JRadioButton jRadioButton5;
// End of variables declaration                   

//ButtonGroup group = new ButtonGroup();
//group.


}

This is a simple question I think. 我认为这是一个简单的问题。 I have four radiobuttons and a regular submit button below the radiobuttons. 我有四个单选按钮,并在单选按钮下面有一个常规的提交按钮。 I am trying to lauch a Jframe when the user clicks on the submit button (one of the radioButton is selected at this point) The Jframe is launched depending on the radiobutton selected. 当用户单击“提交”按钮(此时已选择单选按钮之一)时,我正在尝试取消Jframe。根据选择的单选按钮,将启动Jframe。 How do you put that into code ? 您如何将其放入代码中? any ideas? 有任何想法吗?

In the action listener of the button, check which radio button is checked: 在按钮的动作侦听器中,检查选中了哪个单选按钮:

private void jButton1ActionPerformed(ActionEvent evt) {                                         
    if (jRadioButton1.isSelected()) {
        // show frame 1
    }
    else if (jRadioButton2.isSelected()) {
        // show frame 2
    }
    else if (jRadioButton3.isSelected()) {
        // show frame 3
    }
    else if (jRadioButton4.isSelected()) {
        // show frame 4
    }
}                    

You'll want to use a ButtonGroup to control the selection behavior (so only one radio button is selected at a time). 您将要使用ButtonGroup来控制选择行为(因此一次只能选择一个单选按钮)。 Add an ActionListener to the JButton, and inside the listener, retrieve the selected button from your ButtonGroup. 将动作侦听器添加到JButton,然后在侦听器内部,从ButtonGroup中检索选定的按钮。

You can do this in multiple ways. 您可以通过多种方式执行此操作。 I'll try and post one of them out here. 我将尝试在其中发布其中之一。

This is not necessarily the best option but it might just work for you. 这不一定是最佳选择,但它可能对您有用。 (I didn't test this) (我没有测试过)

public class CrudMain extends javax.swing.JInternalFrame {

public CrudMain() {
    initRadioButtons();
    initOtherStuff();
}

private JRadioButton[] radioButtons = new JRadioButton[4];
private JRadioButton btn1 = new JRadioButton();
private JRadioButton btn2 = new JRadioButton();
private JRadioButton btn3 = new JRadioButton();
private JRadioButton btn4 = new JRadioButton();
private JButton submitBtn = new JButton("Submit"); 

public void initRadioButtons() {
    radioButtons[0] = btn1;
    radioButtons[1] = btn2;
    radioButtons[2] = btn3;
    radioButtons[3] = btn4;
}

public void initOtherStuff() {
    //add stuff to your frame
    .......
    submitBtn.addActionListener(this)
}

public void actionPerformed(ActionEvent e) {
    for(int i =0; i < radioButtons.length; i++){
        if(radioButtons[i].isSelected()){
            //Open your frame here
            break; //Place break if you only want one radiobutton to be checked.
        } else {
            //This button was not selected
        }
    }
}

So let's take a look at this piece of code. 因此,让我们看一下这段代码。 I put all the buttons inside an array so that you can easily loop through the contents to check whether they have been selected. 我将所有按钮放入数组中,以便您可以轻松地循环浏览内容以检查是否已选择它们。 I put an actionListener on the button which will fire when someone clicks on it. 我将actionListener放在按钮上,当有人单击该按钮时将触发它。 When the actionListener fires it will loop through the array. 当actionListener触发时,它将遍历数组。 Inside every iteration every buttons state gets checked. 在每次迭代中,都会检查每个按钮状态。 If one has been selected it will fire an action. 如果选择了一个,它将触发一个动作。

And that's about it. 就是这样。 If you need help with this code please let me know!! 如果您需要有关此代码的帮助,请告诉我!

Good luck! 祝好运!

EDIT : 编辑:

Just noticed that with this method you can't specify an action per radiobutton. 刚刚注意到,使用这种方法不能为每个单选按钮指定操作。 That's something you will have to add to this :) good luck! 这就是您必须添加的内容:)祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM