如果点击2 JRadiobutton如何检查条件?

时间:2014-05-10 06:40:23

标签: java jradiobutton

我有一个小条件问题,当我点击一个JRadiobutton时,它没有检查if else语句中的条件并直接转到else语句。

这是我的代码,我已经做了if else的条件,并且每件事都在工作除外 if if只跳过其他地方

package javaapplication4;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class RadioApplication extends JFrame implements ItemListener {

    JRadioButton r1 = new JRadioButton("JRadioButton 1");
    JRadioButton r2 = new JRadioButton("JRadioButton 2");
    JRadioButton r3 = new JRadioButton("JRadioButton 3");
    JRadioButton r4 = new JRadioButton("JRadioButton 4");
    JRadioButton r5 = new JRadioButton("JRadioButton 5");
    JRadioButton r6 = new JRadioButton("JRadioButton 6");

    ButtonGroup g1 = new ButtonGroup();
    ButtonGroup g2 = new ButtonGroup();

    RadioApplication() {
        setTitle("Payment Mode");
        setSize(300, 250);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(3, 1));

        g1.add(r1);
        g2.add(r2);

        g1.add(r3);
        g2.add(r4);

        g1.add(r5);
        g2.add(r6);

        r1.addItemListener(this);
        r2.addItemListener(this);
        r3.addItemListener(this);
        r4.addItemListener(this);
        r5.addItemListener(this);
        r6.addItemListener(this);
        add(r1);
        add(r2);
        add(r3);
        add(r4);
        add(r5);
        add(r6);
    }

    @Override
    public void itemStateChanged(ItemEvent e) {

        if (r1.isSelected() && r2.isSelected()) {
            JOptionPane.showMessageDialog(null, "JRadioButton 1 and JRadioButton 2  are Selected ");
        } else if (r3.isSelected() && r4.isSelected()) {
            JOptionPane.showMessageDialog(null, "JRadioButton 3 and JRadioButton 4 are Selected");
        } else if (r5.isSelected() && r6.isSelected()) {
            JOptionPane.showMessageDialog(null, "JRadioButton 5 and JRadioButton 6 are Selected");
        } else {
            JOptionPane.showMessageDialog(null, "No JRadioButton was Selected");
        }

    }

    public static void main(String[] args) {
        RadioApplication app1 = new RadioApplication();
    }
}

2 个答案:

答案 0 :(得分:1)

因为您使用的符号是错误的!使用&&对于两个表达式和&之间的布尔条件仅用于按位操作

答案 1 :(得分:0)

不知道你以前尝试过什么。 如果您的代码无效,请按照

http://docs.oracle.com/javase/tutorial/uiswing/components/button.html

http://www.tutorialspoint.com/swing/swing_jradiobutton.htm

http://examples.javacodegeeks.com/desktop-java/swing/jradiobutton/get-selected-jradiobutton-from-buttongroup/

If-else condition within a JButton ActionEvent

package com.javacodegeeks.snippets.desktop;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRadioButton;

public class GetSelectedJRadioButtonFromButtonGroup extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;

    private JRadioButton java;
    private JRadioButton c;
    private JRadioButton net;
    private JButton button;
    private ButtonGroup buttonGroup;

    public GetSelectedJRadioButtonFromButtonGroup() {

        // set flow layout for the frame
        this.getContentPane().setLayout(new FlowLayout());

        java = new JRadioButton("Java");
        java.setActionCommand("Java");

        c = new JRadioButton("C/C++");
        c.setActionCommand("c");

        net = new JRadioButton(".NET");
        net.setActionCommand("net");

        java.setSelected(true);

        button = new JButton("Check");

        button.addActionListener(this);

        buttonGroup = new ButtonGroup();

        //add radio buttons
        buttonGroup.add(java);
        buttonGroup.add(c);
        buttonGroup.add(net);

        add(java);
        add(c);
        add(net);
        add(button);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Check")) {
            System.out.println("Selected Radio Button: " + buttonGroup.getSelection().getActionCommand());
        }
    }

    private static void createAndShowGUI() {

  //Create and set up the window.

  JFrame frame = new GetSelectedJRadioButtonFromButtonGroup();

  //Display the window.

  frame.pack();

  frame.setVisible(true);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {

  //Schedule a job for the event-dispatching thread:

  //creating and showing this application's GUI.

  javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

    createAndShowGUI(); 

}

  });
    }

}

Source code from

更新了ans

boolean b;
b = 3 > 2 && 5 < 7; // b is true
b = 2 > 3 && 5 < 7; // b is now false

试试这个

您使用的是&,而是使用&&运算符

&是Bitwise,

&&是逻辑的,

if (r1.isSelected() && r2.isSelected()) {
  JOptionPane.showMessageDialog(null,"JRadioButton 1 and JRadioButton 2  are Selected ");
}