基本的radiobutton问题 - java

时间:2012-04-30 22:33:14

标签: java swing jbutton jradiobutton

 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.


}

我认为这是一个简单的问题。我有四个radiobuttons和radiobuttons下面的常规提交按钮。当用户点击提交按钮时(我此时选择了一个radioButton),我试图使用Jframe。根据所选的单选按钮启动Jframe。你怎么把它放到代码中?任何想法?

3 个答案:

答案 0 :(得分:1)

在按钮的动作侦听器中,检查选中了哪个单选按钮:

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
    }
}                    

答案 1 :(得分:1)

您需要使用ButtonGroup来控制选择行为(因此一次只能选择一个单选按钮)。将ActionListener添加到JButton,并在侦听器内部,从ButtonGroup中检索所选按钮。

答案 2 :(得分:1)

您可以通过多种方式执行此操作。我会尝试将其中一个发布到这里。

这不一定是最好的选择,但它可能对您有用。 (我没有测试过这个)

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
        }
    }
}

让我们来看看这段代码。我将所有按钮放在一个数组中,以便您可以轻松遍历内容以检查它们是否已被选中。我在按钮上放了一个actionListener,当有人点击它时会触发该按钮。当actionListener触发时,它将遍历数组。在每次迭代中,每个按钮状态都会被检查。如果选择了一个,它将触发一个动作。

就是这样。如果您需要有关此代码的帮助,请告诉我!!

祝你好运!

编辑:

注意到使用此方法,您无法为每个单选按钮指定操作。这是你必须添加的东西:)祝你好运!

相关问题