繁体   English   中英

当我单击单选按钮时,它没有显示我想要的输出?

[英]when I click on radio button it does not showing output that I want?

当该程序运行时,默认情况下会显示非活动输出,而当我单击活动按钮时,则不会显示任何内容。 请帮助我解决此问题。

我想开发一个可以像该应用程序一样工作的应用程序。 这是一个演示应用程序,但我无法解决此问题。

演示应用程序:

    import javax.swing.JRadioButton;
    import javax.swing.ButtonGroup;
    import java.awt.event.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.FlowLayout;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Cursor;
    public class GraphicRadioButton extends JFrame{

        public  static JRadioButton active ,deactive;
        private ButtonGroup rdoBtn;
        private JFrame frame;
        private JPanel panel;
        public GraphicRadioButton()
        {
            super("Testing");
            active = new  JRadioButton("Active");
            deactive = new  JRadioButton("Deactive ", true);
            frame = new JFrame();
            this.setLocationRelativeTo(null);

            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            panel = new JPanel();
            panel.setLayout(new FlowLayout());
            rdoBtn = new ButtonGroup();
            rdoBtn.add(active);
            rdoBtn.add(deactive);

            panel.add(active);
            panel.add(deactive);
            this.add(panel,BorderLayout.CENTER);
            this.setResizable(false);
            Cursor c = new Cursor(Cursor.HAND_CURSOR);
            this.setCursor(c); 
            pack();
        }
    }



    import javax.swing.*;
    public class Main {

        public static void main(String ar[]){
            GraphicRadioButton g = new GraphicRadioButton();
            g.setVisible(true);
            boolean test = true;
            boolean ch = true;

            do{
                 if(GraphicRadioButton.active.isSelected()&&!test){

                        System.out.println("active");
                        test = true;
                        ch = true;
                  }

                 if(GraphicRadioButton.deactive.isSelected()  &&ch ){

                        System.out.println("deactive");
                        test = false;
                        ch = false;
                    }

            }while(true);

        }//end main method
    }//end Main class

更改此行

if (GraphicRadioButton.active.isSelected() && !test) {

if (GraphicRadioButton.active.isSelected() && test) {

您需要更改!test来进行test

我已经使用以下代码进行了测试,并且工作正常:

package ljm.aplication;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.FlowLayout;

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

public class GraphicRadioButton extends JFrame {

    public static void main(String ar[]) {
        GraphicRadioButton g = new GraphicRadioButton();
        g.setVisible(true);
        boolean test = true;
        boolean ch = true;

        do {
            if (GraphicRadioButton.active.isSelected() && !test) {

                System.out.println("active");
                test = true;
                ch = true;
            }

            if (GraphicRadioButton.deactive.isSelected() && ch) {

                System.out.println("deactive");
                test = false;
                ch = false;
            }

        }
        while (true);

    }// end main method

    private static final long serialVersionUID = -3366748875509458022L;
    public static JRadioButton active, deactive;
    private ButtonGroup rdoBtn;
    private JFrame frame;
    private JPanel panel;

    public GraphicRadioButton() {
        super("Testing");
        active = new JRadioButton("Active");
        deactive = new JRadioButton("Deactive ", true);
        frame = new JFrame();
        this.setLocationRelativeTo(null);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        panel.setLayout(new FlowLayout());
        rdoBtn = new ButtonGroup();
        rdoBtn.add(active);
        rdoBtn.add(deactive);

        panel.add(active);
        panel.add(deactive);
        this.add(panel, BorderLayout.CENTER);
        this.setResizable(false);
        Cursor c = new Cursor(Cursor.HAND_CURSOR);
        this.setCursor(c);
        pack();
    }
}

暂无
暂无

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

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