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

时间:2014-07-02 16:24:23

标签: java swing button user-interface radio-button

我编写了一个程序,其中包含两个数组的图形显示。一个是随机数字,另一个是通过冒泡排序排列的数字。然后程序接受这两个数组并通过基于数组第i位数字值的方形阴影显示它们。然后,我尝试修改我的代码以包含一些单选按钮来更改显示的主题颜色,红色,绿色或蓝色。当选择一个按钮时,显示效果很好,但是当按下另一个按钮时,颜色将成为所选颜色的组合(例如,黄色表示红色和绿色,白色表示所有三个),您无法取消选择颜色要么。

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

主:

import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;


public class BubbleMain {

public static void main(String args[])throws IOException{

    int n;
    Scanner keyboard = new Scanner(System.in);

    while(true){
        try{
            System.out.println("Enter the size of the array");
            n = keyboard.nextInt();

            if(n >= 2){
                break;
            }
            System.out.println("Size must be 2 or greater");
        }catch(InputMismatchException e){
            System.out.println("Value must be an integer");
            keyboard.nextLine();
        }
    }


    double[] template = new double[n];
    double[] mess = Bubble.randPop(template);

    double[] tidy = Arrays.copyOf(mess, mess.length);
    tidy = Bubble.bubbleSort(tidy);

    System.out.println("original: ");
    Bubble.printOut(mess);
    System.out.println("sorted: ");
    Bubble.printOut(tidy);

    String filename = "bubresult.txt";
    Bubble.printFile(mess, tidy, filename);


    double xMax = 0.0;
    double xMin = 0.0;

    while(true){
        try{
            System.out.println("Enter the minimum value of the range: ");
            xMin = keyboard.nextDouble();

            System.out.println("Enter the max value: ");
            xMax = keyboard.nextDouble();

            if(xMax > xMin){
                break;
            }
            System.out.println("The max value must be greater than the minimum");           
        }catch(InputMismatchException e){
            System.out.println("Value must be a double");
            keyboard.nextLine();
        }
    }

    double[] rangeTemplate = new double[n];
    double[] messRange = Bubble.randRangePop(rangeTemplate, xMin, xMax);

    double[] tidyRange = Arrays.copyOf(messRange, messRange.length);
    tidyRange = Bubble.bubbleSort(tidyRange);

    System.out.println("original: ");
    Bubble.printOut(messRange);
    System.out.println("sorted");
    Bubble.printOut(tidyRange);

    BubFrame bf = new BubFrame(mess, tidy);
    bf.setVisible(true);

}
}
泡泡:

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();

}
}

图形:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

class BubPanel extends JPanel{

private int X, Y;
private int wM, hM, wT, hT;
private int R, G, B;

private double[] mess, tidy;

public BubPanel(double[] mess, double[] tidy){

    this.mess = mess;
    this.tidy = tidy;

    setBackground(Color.GRAY);


}

public void paintComponent(Graphics g){

    g.setColor(getBackground());
    super.paintComponent(g);


    wM = getWidth()/mess.length;
    hM = 40;
    wT = getWidth()/tidy.length;
    hT = 40;

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

        Color clr = new Color((int)(255*mess[i])*R, (int)(255*mess[i])*G, (int)(255*mess[i])*B);
        g.setColor(clr);
        g.fillRect(wM*i, 20, wM, hM);
    }


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

        Color clr = new Color((int)(255*tidy[i])*R, (int)(255*tidy[i])*G, (int)(255*tidy[i])*B);
        g.setColor(clr);
        g.fillRect(wT*i, getHeight()-20-hT, wT, hT);
    }

}

public int getR(){
    return R;
}
public void setR(int R){
    this.R = R;
}
public int getG(){
    return G;
}
public void setG(int G){
    this.G = G;
}
public int getB(){
    return B;
}
public void setB(int B){
    this.B = B;
}
}






class BubFrame extends JFrame{

private int X = 800;
private int Y = 200;

double[] mess, tidy;

BubPanel bubPan;

JRadioButton R = new JRadioButton("R");
JRadioButton G = new JRadioButton("G");
JRadioButton B = new JRadioButton("B");

public BubFrame(double[] mess, double[] tidy){

    this.mess = mess;
    this.tidy = tidy;

    bubPan = new BubPanel(mess, tidy);

    setLayout(new BorderLayout());

    getContentPane().add(bubPan, BorderLayout.CENTER);
    pack();

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e){
            System.exit(0);
        }
    });

    ButtonGroup group = new ButtonGroup();
    group.add(R);
    group.add(G);
    group.add(B);


    JPanel buttonPanel = new JPanel();
    buttonPanel.add(R);
    buttonPanel.add(G);
    buttonPanel.add(B);
    add(buttonPanel, BorderLayout.PAGE_END);




    setTitle("Bubble Sort");
    setBackground(Color.PINK);
    setSize(X, Y);
    setLocation(200, 200);
    redFlavour();
    greenFlavour();
    blueFlavour();

}


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

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

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

}

2 个答案:

答案 0 :(得分:2)

您正在设置R,G和B值,但不设置其他值2.请使用以下内容替换不同口味的actionPerformed

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();
             }
         });
     }

答案 1 :(得分:0)

JRadioButton听众中,您还需要清除其他颜色。否则,你会混合使用它们。

所以,例如:

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();
    }
});

对其他按钮也这样做。