Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用单选按钮更改GUI的颜色_Java_Swing_Button_User Interface_Radio Button - Fatal编程技术网

Java 使用单选按钮更改GUI的颜色

Java 使用单选按钮更改GUI的颜色,java,swing,button,user-interface,radio-button,Java,Swing,Button,User Interface,Radio Button,我写了一个程序,它有两个数组的图形显示。一个是充满随机数,另一个是通过气泡排序排列的数字。然后,程序获取这两个数组,并根据数组第i个位置的数字值,通过对正方形进行着色来显示它们。然后,我尝试修改代码,加入一些单选按钮,以更改显示屏的主题颜色,红色、绿色或蓝色。当选择一个按钮时,显示良好,但当按下另一个按钮时,颜色变为所选按钮的组合(例如,红色和绿色为黄色,所有三个按钮均为白色),并且您也不能取消选择颜色 我的问题是:如何修改代码,使GUI上的单选按钮可以在三种颜色之间反复切换,而不是它们的任何组

我写了一个程序,它有两个数组的图形显示。一个是充满随机数,另一个是通过气泡排序排列的数字。然后,程序获取这两个数组,并根据数组第i个位置的数字值,通过对正方形进行着色来显示它们。然后,我尝试修改代码,加入一些单选按钮,以更改显示屏的主题颜色,红色、绿色或蓝色。当选择一个按钮时,显示良好,但当按下另一个按钮时,颜色变为所选按钮的组合(例如,红色和绿色为黄色,所有三个按钮均为白色),并且您也不能取消选择颜色

我的问题是:如何修改代码,使GUI上的单选按钮可以在三种颜色之间反复切换,而不是它们的任何组合

主要内容:

气泡:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;




public class Bubble {

private double[] list;

public Bubble(double[] list){

    this.list = list;
}

public double[] getArray(){
    return list;
}

public static double[] randPop(double[] template){

    for(int i = 0; i < template.length; i++){
        template[i] = Math.random();
    }

    return template;
}


public static double[] randRangePop(double[] rangeTemplate, double xMin, double xMax){

    for(int i = 0; i < rangeTemplate.length; i++){
        Random r = new Random();
        double randomValue = xMin + (xMax - xMin) * r.nextDouble();
        rangeTemplate[i] = (xMin ) + (Math.random()* ( xMax - xMin + 1));;
    }

    //Arrays.
    return rangeTemplate;
}

public static double[] bubbleSort(double[] mess){

    double[] tidy = new double[mess.length];

    for(int i=0; i<mess.length; i++)
    {
        for(int j=i + 1; j<mess.length; j++)
        {
            if(mess[i] > mess[j])
            {
                double temp = mess[i];
                mess[i] = mess[j];
                mess[j] = temp;
            }
        }
        tidy[i] = mess[i];
    }
    return tidy;
}




public static void printOut(double[] list){

    for(int i = 0; i < list.length; i++){
        System.out.printf("%.3f \n", list[i]);
    }
}



public static void printFile(double[] mess, double[] tidy, String filename)throws IOException{

    FileWriter outputfile = new FileWriter(filename);
    BufferedWriter out = new BufferedWriter(outputfile);

    String origIntro = "Original Array: \n";
    out.write(origIntro, 0, origIntro.length());

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

        String orig = ""+mess[i]+"\n";
        out.write(orig, 0, orig.length());
    }


    String sortIntro = "Sorted Array: \n";
    out.write(sortIntro, 0, sortIntro.length());

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

        String sort = ""+tidy[i]+"\n";
        out.write(sort, 0, sort.length());
    }

    out.close();
    outputfile.close();

}
}
导入java.io.BufferedWriter;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.Random;
公共阶级泡沫{
私人双[]名单;
公共泡沫(双[]列表){
this.list=列表;
}
公共双[]getArray(){
退货清单;
}
公共静态双[]randPop(双[]模板){
对于(int i=0;i对于
JRadioButton
侦听器中的(inti=0;i),您还需要清除其他颜色。否则,您将混合使用它们

例如:

B.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        if(B.isSelected()){
            bubPan.setR(0);  //added this
            bubPan.setG(0);  //added this
            bubPan.setB(1);         
        }else if(!B.isSelected()){
            bubPan.setB(0);
        }
        repaint();
    }
});

并对其他按钮执行相同操作。

您正在设置R、G和B值,但未设置其他2个。请将不同口味的操作替换为以下内容

public void redFlavour(){
         R.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
                 if(R.isSelected()){
                 bubPan.setR(1);
                 bubPan.setG(0);
                 bubPan.setB(0);
                 }else if(!R.isSelected()){
                     bubPan.setR(0);
                 }
                 repaint();
             }
         });
     }

     public void greenFlavour(){
         G.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
                 if(G.isSelected()){
                     bubPan.setR(0);
                     bubPan.setG(1);
                     bubPan.setB(0);
                 }else if(!G.isSelected()){
                     bubPan.setG(0);
                 }
                 repaint();
             }
         });
     }

     public void blueFlavour(){
         B.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
                 if(B.isSelected()){
                     bubPan.setR(0);
                     bubPan.setG(0);
                     bubPan.setB(1);
                 }else if(!B.isSelected()){
                     bubPan.setB(0);
                 }
                 repaint();
             }
         });
     }
B.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        if(B.isSelected()){
            bubPan.setR(0);  //added this
            bubPan.setG(0);  //added this
            bubPan.setB(1);         
        }else if(!B.isSelected()){
            bubPan.setB(0);
        }
        repaint();
    }
});
public void redFlavour(){
         R.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
                 if(R.isSelected()){
                 bubPan.setR(1);
                 bubPan.setG(0);
                 bubPan.setB(0);
                 }else if(!R.isSelected()){
                     bubPan.setR(0);
                 }
                 repaint();
             }
         });
     }

     public void greenFlavour(){
         G.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
                 if(G.isSelected()){
                     bubPan.setR(0);
                     bubPan.setG(1);
                     bubPan.setB(0);
                 }else if(!G.isSelected()){
                     bubPan.setG(0);
                 }
                 repaint();
             }
         });
     }

     public void blueFlavour(){
         B.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
                 if(B.isSelected()){
                     bubPan.setR(0);
                     bubPan.setG(0);
                     bubPan.setB(1);
                 }else if(!B.isSelected()){
                     bubPan.setB(0);
                 }
                 repaint();
             }
         });
     }