本文介绍了JButton无法识别是否选择了JRadioButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此JButton检查是否选择了某些JRadioButtons.因此,如果从下面的列表中选择了JRadionButtons,则消息对话框将打印正确答案的消息,否则它将打印不正确答案的消息.

This JButton examines does is some of JRadioButtons is selected. So if JRadionButtons from list below is selected message dialog box will print message that's correct answer else it should print that isn't correct answer.

我当时有40个JRadioButtons,10个按钮组,4个JRadioButtons显示在屏幕上.

I have 40 JRadioButtons, 10 Button Groups, 4 JRadioButtons is showing on screen at the time.

我注意到此JButton(btn2)记得先检查一下,如果我单击了错误的JRadioButton,然后单击此btn2,它将向我显示消息对话框,显示这是不正确的答案"-是正确的,接下来是我继续选择错误的JRadioButton,它将继续显示这不是正确的答案-是正确的,但是,如果我选择正确的JRadioButton,它将显示带有这是正确答案"的消息对话框-是正确的,但是如果我继续现在选择错误的JRadioButton

I noted that this JButton (btn2) remember first examines so if I click on wrong JRadioButton and then I click on this btn2 it's will show me message dialog box with "this isn't correct answer"- that's correct, next if I continue selecting wrong JRadioButton it will continue to show that's isn't correct answer - that's correct, but if I select correct JRadioButton it will show me message dialog box with "this is correct answer" - that's correct but it will continue that if I now select wrong JRadioButton

        btn2.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            tim.stop();
            btn1.setEnabled(true);

            //Ne radi kako treba ne pravi razliku da li su   selektovani ili nisu
            //ne znam u čemu je problem
            if(nizRButtona[1].isSelected()
                    ||nizRButtona[5].isSelected()
                    ||nizRButtona[11].isSelected()
                    ||nizRButtona[13].isSelected()
                    ||nizRButtona[16].isSelected()
                    ||nizRButtona[23].isSelected()
                    ||nizRButtona[27].isSelected()
                    ||nizRButtona[29].isSelected()
                    ||nizRButtona[32].isSelected()){
                s+=10;
                JOptionPane.showMessageDialog(null, "To je tačan odgovor"); //This is correct answer
            }else if(nizRButtona[36].isSelected()){
                s+=10;
                JOptionPane.showMessageDialog(null, "To je tačan odgovor osvoji ste ukupno "+s+" poena"); // This is correct answer you won s points.
                btn1.setText("Započni opet igru");
            }else if(nizRButtona[33].isSelected()
                    ||nizRButtona[34].isSelected()
                    ||nizRButtona[35].isSelected()){
        JOptionPane.showMessageDialog(null, "To nije tačan odgovor osvoji ste ukupno "+s+" poena"); //This isn't correct answer you won s points
                btn1.setText("Započni opet igru");
            }else{
                JOptionPane.showMessageDialog(null, "To nije tačan odgovor"); //This isn't correct answer
            }

        }

    });

所有代码(将被删除)

   package Kviz;
   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;

   public class Kviz {

private JFrame okvir;
private JPanel p1,p2,p3;
private JLabel [] nizLabela;
private JRadioButton [] nizRButtona;
private ButtonGroup[] nizGrupa;
private JPanel [] nizPanela;
private CardLayout cl;
private JLabel lb1,lb2;
private JButton btn1,btn2;
private Timer tim;
private int t,s;

public Kviz(){
    pripremiGUI();
}

private void pripremiGUI(){

    okvir=new JFrame();
    okvir.setSize(400, 400);
    okvir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    okvir.setLocationRelativeTo(null);
    okvir.setLayout(new BorderLayout());

    p1=new JPanel();
    p1.setLayout(new FlowLayout());

    lb1=new JLabel("Dobrodošli u kviz");

    cl=new CardLayout();

    p2=new JPanel();
    p2.setLayout(cl);

    nizPanela = new JPanel[10];

    //Niz panela na kome će se smenjivati pitanja i odgovori
    for(int i=0;i<nizPanela.length;i++){

        nizPanela[i] = new JPanel();
        p2.add(nizPanela[i], String.valueOf(i));
        nizPanela[i].setLayout(new GridLayout(0,1));

    }

    //Niz odgovora

    nizRButtona = new JRadioButton[40];

    for(int i=0;i<nizRButtona.length;i++){
        nizRButtona[i] = new JRadioButton();

        switch(i){
            case 0:nizRButtona[i].setText("Mesopotamiji");
                break;
            case 1:nizRButtona[i].setText("Grčkoj");
                break;
            case 2:nizRButtona[i].setText("Kini");
                break;
            case 3:nizRButtona[i].setText("Egiptu");
                break;
            case 4:nizRButtona[i].setText("Kinšasa");
                break;
            case 5:nizRButtona[i].setText("Lagos");
                break;
            case 6:nizRButtona[i].setText("Ulan Bator");
                break;
            case 7:nizRButtona[i].setText("Abu Dabi");
                break;
            case 8:nizRButtona[i].setText("Velikoj Britaniji");
                break;
            case 9:nizRButtona[i].setText("Norveškoj");
                break;
            case 10:nizRButtona[i].setText("SAD");
                break;
            case 11:nizRButtona[i].setText("Danskoj");
                break;
            case 12:nizRButtona[i].setText("Step mom");
                break;
            case 13:nizRButtona[i].setText("Runaway Bride");
                break;
            case 14:nizRButtona[i].setText("Grease");
                break;
            case 15:nizRButtona[i].setText("Nothing Hill");
                break;
            case 16:nizRButtona[i].setText("Neil Armstrong");
                break;
            case 17:nizRButtona[i].setText("John Lenon");
                break;
            case 18:nizRButtona[i].setText("Martin Luter King");
                break;
            case 19:nizRButtona[i].setText("Winston Churchill");
                break;
            case 20:nizRButtona[i].setText("Meka");
                break;
            case 21:nizRButtona[i].setText("Medina");
                break;
            case 22:nizRButtona[i].setText("Dubai");
                break;
            case 23:nizRButtona[i].setText("Rijad");
                break;
            case 24:nizRButtona[i].setText("Španije");
                break;
            case 25:nizRButtona[i].setText("Venecije");
                break;
            case 26:nizRButtona[i].setText("Portugala");
                break;
            case 27:nizRButtona[i].setText("Đenove");
                break;
            case 28:nizRButtona[i].setText("Holokaust");
                break;
            case 29:nizRButtona[i].setText("Perl Harbour");
                break;
            case 30:nizRButtona[i].setText("Napad na Francusku");
                break;
            case 31:nizRButtona[i].setText("Blitzkrieg na Poljsku");
                break;
            case 32:nizRButtona[i].setText("Bon");
                break;
            case 33:nizRButtona[i].setText("Minhen");
                break;
            case 34:nizRButtona[i].setText("Lajpcig");
                break;
            case 35:nizRButtona[i].setText("Hamburg");
                break;
            case 36:nizRButtona[i].setText("Čile");
                break;
            case 37:nizRButtona[i].setText("Istočni Timor");
                break;
            case 38:nizRButtona[i].setText("Portugal");
                break;
            case 39:nizRButtona[i].setText("Brazil");
                break;

        }
    }

    //Niz button grupa i dodavanje komponenti u njega
    nizGrupa=new ButtonGroup[10];

    int j=0;
    for(int i=0;i<nizGrupa.length;i++){

    nizGrupa[i] = new ButtonGroup();

    nizGrupa[i].add(nizRButtona[j]);
    nizGrupa[i].add(nizRButtona[j+1]);
    nizGrupa[i].add(nizRButtona[j+2]);
    nizGrupa[i].add(nizRButtona[j+3]);

    j+=4;
    }

    //Niz pitanja
    nizLabela = new JLabel[10];

    for(int i=0;i<nizLabela.length;i++){
        nizLabela[i] = new JLabel();
        switch(i){
            case 0:nizLabela[i].setText("U kojoj zemlji su rođene Olimpijske igre");
                break;
            case 1:nizLabela[i].setText("Koji je glavni grad Nigerije");
                break;
            case 2:nizLabela[i].setText("Kojoj državi pripada Grenland");
                break;
            case 3:nizLabela[i].setText("Osim filma \"Pretty Women\" u kojem filmu zajedno glume Julia Roberts i Richard Gere");
                break;
            case 4:nizLabela[i].setText("Kako se zove prvi čovek koji je sleteo na mesec");
                break;
            case 5:nizLabela[i].setText("Glavni grad Saudijske Arabije");
                break;
            case 6:nizLabela[i].setText("Kristofer Kolumob je iz");
                break;
            case 7:nizLabela[i].setText("Koji događaj je naveo SAD da se uključe u rat");
                break;
            case 8:nizLabela[i].setText("Koji je grad bio glavni pri ujedinjenju Nemačke");
                break;
            case 9:nizLabela[i].setText("U kojoj zemlji je bio na vlasti diktator Salazar");
                break;
        }
    }

    p3=new JPanel();
    p3.setLayout(new FlowLayout());

    //Menja panele sa pitanjima i odgovorima
    btn1=new JButton("Sledeće pitanje");
    btn1.setEnabled(false);
    btn1.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            cl.next(p2);
            btn1.setEnabled(false);
            btn2.setEnabled(true);
            t=10;
            lb2.setText(String.valueOf(t));
            tim.start();
            btn1.setText("Sledeće pitanje");
        }
    });

    //Proverava da li su odgovori tačni
    btn2=new JButton("Odgovori");
    btn2.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            tim.stop();
            btn1.setEnabled(true);

            //
            //
            if(nizRButtona[1].isSelected()
                    ||nizRButtona[5].isSelected()
                    ||nizRButtona[11].isSelected()
                    ||nizRButtona[13].isSelected()
                    ||nizRButtona[16].isSelected()
                    ||nizRButtona[23].isSelected()
                    ||nizRButtona[27].isSelected()
                    ||nizRButtona[29].isSelected()
                    ||nizRButtona[32].isSelected()){
                s+=10;
                JOptionPane.showMessageDialog(null, "To je tačan odgovor");
            }else if(nizRButtona[36].isSelected()){
                s+=10;
                JOptionPane.showMessageDialog(null, "To je tačan odgovor osvoji ste ukupno "+s+" poena");
                btn1.setText("Započni opet igru");
            }else if(nizRButtona[33].isSelected()
                    ||nizRButtona[34].isSelected()
                    ||nizRButtona[35].isSelected()){
                JOptionPane.showMessageDialog(null, "To nije tačan odgovor osvoji ste ukupno "+s+" poena");
                btn1.setText("Započni opet igru");
            }else{
                JOptionPane.showMessageDialog(null, "To nije tačan odgovor");
            }

        }

    });

    t=10;

    //Tajmer
    lb2=new JLabel("10");
    tim=new Timer(1000,new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            if(t>0){
            t--;
            lb2.setText(String.valueOf(t));
            }else{
                tim.stop();
                JOptionPane.showMessageDialog(null, "Vreme je isteklo");
                btn2.setEnabled(false);
                btn1.setEnabled(true);
            }
        }
    });
}

private void pokreniGUI(){
    p1.add(lb1);

    p3.add(btn1);
    p3.add(btn2);
    p3.add(lb2);

    int q=0;
    for(int w=0;w<10;w++){

    nizGrupa[w] = new ButtonGroup();

    nizPanela[w].add(nizLabela[w]);
    nizPanela[w].add(nizRButtona[q]);
    nizPanela[w].add(nizRButtona[q+1]);
    nizPanela[w].add(nizRButtona[q+2]);
    nizPanela[w].add(nizRButtona[q+3]);

    q+=4;
    }

    okvir.add(p1,BorderLayout.NORTH);
    okvir.add(p2,BorderLayout.CENTER);
    okvir.add(p3,BorderLayout.SOUTH);

    cl.show(p2, "0");

    tim.start();
    okvir.setVisible(true);
}

public static void main(String[] args) {
    Kviz pokreni=new Kviz();
    pokreni.pokreniGUI();

}

   }

推荐答案

正如我在发布代码之前所想的那样,此错误是由于您将所有JRadioButtons作为单个数组而选择仍然存在的缘故,您需要每次完成评估时都会清除此选择.下次也尝试使用调试器.

As I thought before you posted the code, this error comes from you having all your JRadioButtons as a single array and the selection is still there, you need to clear this selection each time you finish to evaluate it. Next time try to use a debugger too.

在您的代码上,您需要修改2件事:

On your code you need to modify 2 things:

  1. 清除您的每个组选择,如下所示:

这是因为您有一个JRadioButton的全局数组,而是应该有一个较小的数组,该数组将从全局String数组中获取文本,并且每次继续下一个问题时都会更新,因此只必须管理一个单独的ButtonGroup而不是10.这是可以轻松"使程序运行的方法,但是解决该问题的最佳"方法是重新执行所有程序,以便拥有一个JRadioButton大小为4的数组,一个单独的ButtonGroup和一个String数组,每个JRadioButton

This is because you have a global array of JRadioButtons, instead you should have a smaller array which would get it's text from a global String array, and updates each time you continue to next question, so you only have to manage a single ButtonGroup instead of 10. This is the way you can "easily" make your program to work, but the "best" way to fix it is re-do all your program so you have a JRadioButton array of size 4, a single ButtonGroup and a String array with the text for each JRadioButton

//Your if-else codes are above here
else {
    JOptionPane.showMessageDialog(null, "To nije tačan odgovor");
}
for (int i = 0; i < 10; i++) {
    nizGrupa[i].clearSelection();
}
  1. 从您的pokreniGUI方法中删除此行nizGrupa[w] = new ButtonGroup();
  1. Remove this line nizGrupa[w] = new ButtonGroup(); from your pokreniGUI method

因为您之前已对其进行了初始化,所以您无需再次进行它,否则,上述选择清除将不起作用,因为它将引用不再存在的ButtonGroup.

Because you initialized it before, so you don't need to do it again, if you don't, then the above selection clear won't work because it will make reference to a ButtonGroup that no longer exists.

更新,包含完整代码:

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

public class Kviz {

private JFrame okvir;
private JPanel p1,p2,p3;
private JLabel [] nizLabela;
private JRadioButton [] nizRButtona;
private ButtonGroup[] nizGrupa;
private JPanel [] nizPanela;
private CardLayout cl;
private JLabel lb1,lb2;
private JButton btn1,btn2;
private Timer tim;
private int t,s;

public Kviz(){
    pripremiGUI();
}

private void pripremiGUI(){

    okvir=new JFrame();
    okvir.setSize(400, 400);
    okvir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    okvir.setLocationRelativeTo(null);
    okvir.setLayout(new BorderLayout());

    p1=new JPanel();
    p1.setLayout(new FlowLayout());

    lb1=new JLabel("Dobrodošli u kviz");

    cl=new CardLayout();

    p2=new JPanel();
    p2.setLayout(cl);

    nizPanela = new JPanel[10];

    //Niz panela na kome će se smenjivati pitanja i odgovori
    for(int i=0;i<nizPanela.length;i++){

        nizPanela[i] = new JPanel();
        p2.add(nizPanela[i], String.valueOf(i));
        nizPanela[i].setLayout(new GridLayout(0,1));

    }

    //Niz odgovora

    nizRButtona = new JRadioButton[40];

    for(int i=0;i<nizRButtona.length;i++){
        nizRButtona[i] = new JRadioButton();

        switch(i){
            case 0:nizRButtona[i].setText("Mesopotamiji");
                break;
            case 1:nizRButtona[i].setText("Grčkoj");
                break;
            case 2:nizRButtona[i].setText("Kini");
                break;
            case 3:nizRButtona[i].setText("Egiptu");
                break;
            case 4:nizRButtona[i].setText("Kinšasa");
                break;
            case 5:nizRButtona[i].setText("Lagos");
                break;
            case 6:nizRButtona[i].setText("Ulan Bator");
                break;
            case 7:nizRButtona[i].setText("Abu Dabi");
                break;
            case 8:nizRButtona[i].setText("Velikoj Britaniji");
                break;
            case 9:nizRButtona[i].setText("Norveškoj");
                break;
            case 10:nizRButtona[i].setText("SAD");
                break;
            case 11:nizRButtona[i].setText("Danskoj");
                break;
            case 12:nizRButtona[i].setText("Step mom");
                break;
            case 13:nizRButtona[i].setText("Runaway Bride");
                break;
            case 14:nizRButtona[i].setText("Grease");
                break;
            case 15:nizRButtona[i].setText("Nothing Hill");
                break;
            case 16:nizRButtona[i].setText("Neil Armstrong");
                break;
            case 17:nizRButtona[i].setText("John Lenon");
                break;
            case 18:nizRButtona[i].setText("Martin Luter King");
                break;
            case 19:nizRButtona[i].setText("Winston Churchill");
                break;
            case 20:nizRButtona[i].setText("Meka");
                break;
            case 21:nizRButtona[i].setText("Medina");
                break;
            case 22:nizRButtona[i].setText("Dubai");
                break;
            case 23:nizRButtona[i].setText("Rijad");
                break;
            case 24:nizRButtona[i].setText("Španije");
                break;
            case 25:nizRButtona[i].setText("Venecije");
                break;
            case 26:nizRButtona[i].setText("Portugala");
                break;
            case 27:nizRButtona[i].setText("Đenove");
                break;
            case 28:nizRButtona[i].setText("Holokaust");
                break;
            case 29:nizRButtona[i].setText("Perl Harbour");
                break;
            case 30:nizRButtona[i].setText("Napad na Francusku");
                break;
            case 31:nizRButtona[i].setText("Blitzkrieg na Poljsku");
                break;
            case 32:nizRButtona[i].setText("Bon");
                break;
            case 33:nizRButtona[i].setText("Minhen");
                break;
            case 34:nizRButtona[i].setText("Lajpcig");
                break;
            case 35:nizRButtona[i].setText("Hamburg");
                break;
            case 36:nizRButtona[i].setText("Čile");
                break;
            case 37:nizRButtona[i].setText("Istočni Timor");
                break;
            case 38:nizRButtona[i].setText("Portugal");
                break;
            case 39:nizRButtona[i].setText("Brazil");
                break;

        }
    }

    //Niz button grupa i dodavanje komponenti u njega
    nizGrupa=new ButtonGroup[10];

    int j=0;
    for(int i=0;i<nizGrupa.length;i++){

    nizGrupa[i] = new ButtonGroup();

    nizGrupa[i].add(nizRButtona[j]);
    nizGrupa[i].add(nizRButtona[j+1]);
    nizGrupa[i].add(nizRButtona[j+2]);
    nizGrupa[i].add(nizRButtona[j+3]);

    j+=4;
    }

    //Niz pitanja
    nizLabela = new JLabel[10];

    for(int i=0;i<nizLabela.length;i++){
        nizLabela[i] = new JLabel();
        switch(i){
            case 0:nizLabela[i].setText("U kojoj zemlji su rođene Olimpijske igre");
                break;
            case 1:nizLabela[i].setText("Koji je glavni grad Nigerije");
                break;
            case 2:nizLabela[i].setText("Kojoj državi pripada Grenland");
                break;
            case 3:nizLabela[i].setText("Osim filma \"Pretty Women\" u kojem filmu zajedno glume Julia Roberts i Richard Gere");
                break;
            case 4:nizLabela[i].setText("Kako se zove prvi čovek koji je sleteo na mesec");
                break;
            case 5:nizLabela[i].setText("Glavni grad Saudijske Arabije");
                break;
            case 6:nizLabela[i].setText("Kristofer Kolumob je iz");
                break;
            case 7:nizLabela[i].setText("Koji događaj je naveo SAD da se uključe u rat");
                break;
            case 8:nizLabela[i].setText("Koji je grad bio glavni pri ujedinjenju Nemačke");
                break;
            case 9:nizLabela[i].setText("U kojoj zemlji je bio na vlasti diktator Salazar");
                break;
        }
    }

    p3=new JPanel();
    p3.setLayout(new FlowLayout());

    //Menja panele sa pitanjima i odgovorima
    btn1=new JButton("Sledeće pitanje");
    btn1.setEnabled(false);
    btn1.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            cl.next(p2);
            btn1.setEnabled(false);
            btn2.setEnabled(true);
            t=10;
            lb2.setText(String.valueOf(t));
            tim.start();
            btn1.setText("Sledeće pitanje");
        }
    });

    //Proverava da li su odgovori tačni
    btn2=new JButton("Odgovori");
    btn2.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            tim.stop();
            btn1.setEnabled(true);

            if(nizRButtona[1].isSelected()
                ||nizRButtona[5].isSelected()
                ||nizRButtona[11].isSelected()
                ||nizRButtona[13].isSelected()
                ||nizRButtona[16].isSelected()
                ||nizRButtona[23].isSelected()
                ||nizRButtona[27].isSelected()
                ||nizRButtona[29].isSelected()
                ||nizRButtona[32].isSelected()){
                s+=10;
                JOptionPane.showMessageDialog(null, "Correct");
            } else if(nizRButtona[36].isSelected()){
                s+=10;
                JOptionPane.showMessageDialog(null, "Correct, points earned: " + s);
                btn1.setText("Započni opet igru");
            }else if(nizRButtona[33].isSelected()
                    ||nizRButtona[34].isSelected()
                    ||nizRButtona[35].isSelected()){
                JOptionPane.showMessageDialog(null, "Incorrect, points earned " + s);
                btn1.setText("Započni opet igru");
            }else{
                JOptionPane.showMessageDialog(null, "Incorrect");
            }
            for (int i = 0; i < 10; i++) {
                nizGrupa[i].clearSelection();
            }
        }

    });

    t=10;

    //Tajmer
    lb2=new JLabel("10");
    tim=new Timer(1000,new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            if(t>0){
            t--;
            lb2.setText(String.valueOf(t));
            }else{
                tim.stop();
                JOptionPane.showMessageDialog(null, "Vreme je isteklo");
                btn2.setEnabled(false);
                btn1.setEnabled(true);
            }
        }
    });
}

private void pokreniGUI(){
    p1.add(lb1);

    p3.add(btn1);
    p3.add(btn2);
    p3.add(lb2);

    int q=0;
    for(int w=0;w<10;w++){

    //nizGrupa[w] = new ButtonGroup();

    nizPanela[w].add(nizLabela[w]);
    nizPanela[w].add(nizRButtona[q]);
    nizPanela[w].add(nizRButtona[q+1]);
    nizPanela[w].add(nizRButtona[q+2]);
    nizPanela[w].add(nizRButtona[q+3]);

    q+=4;
    }

    okvir.add(p1,BorderLayout.NORTH);
    okvir.add(p2,BorderLayout.CENTER);
    okvir.add(p3,BorderLayout.SOUTH);

    cl.show(p2, "0");

    tim.start();
    okvir.setVisible(true);
}

public static void main(String[] args) {
    Kviz pokreni=new Kviz();
    pokreni.pokreniGUI();

}
}

这篇关于JButton无法识别是否选择了JRadioButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 11:52