改变方形文本字段的颜色

时间:2014-06-13 13:08:19

标签: java swing colors background

我尝试创建一个可以采用3种RGB颜色并更改文本字段背景的布局,但我无法这样做。

package Quizzes_practice;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class quiz3 extends JFrame{
    private JPanel up; 
    private JPanel down; 
    private JPanel left; // as mother container 
    private JPanel right;


    private JLabel font_size; 
    private JLabel color_val;
    private JLabel r;
    private JLabel g;
    private JLabel b;



    private JTextField font_text;
    private JTextField r_text;
    private JTextField g_text;
    private JTextField b_text;
    private JLabel color_text;
    private JTextField red; // the big square


    private JCheckBox bold;
    private JCheckBox italic;


    private JRadioButton fg;
    private JRadioButton bg;
    private JRadioButton reset;
    private ButtonGroup gorup;

    //private JPanel container; // this will sit in the left corner
    private JPanel panel1; // for each row one of these
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;

    private JPanel center;





    public quiz3(){
        JFrame myframe = new JFrame();
        setLayout(new BorderLayout()); // general layout
        JPanel up = new JPanel();
        JPanel down  = new JPanel();
        JPanel left = new JPanel();
        JPanel right = new JPanel();

        // adding components

        // adding the main 4 panels to the their positions

        // panel left will be the container
        JLabel font_size = new JLabel("Font Size ");
        JLabel r = new JLabel("R");
        JLabel g = new JLabel("G");
        JLabel b = new JLabel("B");

        myhandler handler = new myhandler();

        JTextField font_text = new JTextField(10);
        JTextField r_text = new JTextField(4);
        r_text.addActionListener(handler);
        JTextField g_text = new JTextField(4);
        g_text.addActionListener(handler);
        JTextField b_text = new JTextField(4);
        b_text.addActionListener(handler);

        JCheckBox bold = new JCheckBox("Bold");
        JCheckBox italic = new JCheckBox("Italic");


        JRadioButton fg = new JRadioButton("Foreground");
        JRadioButton bg = new JRadioButton("Background");
        JRadioButton reset = new JRadioButton("Reset");
        ButtonGroup group = new ButtonGroup();



        //JPanel center = new JPanel();
        red = new JTextField(); // text field that I want to change its color 
        //center.setLayout(new FlowLayout());
        //center.add(red);
        ///red.setBackground(Color.RED);

        add(up, BorderLayout.NORTH);
        add(down, BorderLayout.SOUTH);
        add(left, BorderLayout.WEST);
        add(red, BorderLayout.CENTER);

        JPanel panel1 = new JPanel ();
        JPanel panel2 = new JPanel ();
        JPanel panel3 = new JPanel ();
        JPanel panel4 = new JPanel ();
        // adding compnents to the frame 
        up.setLayout(new FlowLayout());
        up.add(font_size);
        up.add(font_text);
        up.add(bold);
        up.add(italic);

        down.setLayout(new FlowLayout());
        //down.add(group);
        group.add(fg);
        group.add(bg);
        group.add(reset);

        down.add(fg);
        down.add(bg);
        down.add(reset);


        JLabel color_text = new JLabel("Font size");
        panel1.add(color_text);
        panel2.setLayout(new FlowLayout());
        panel2.add(r);
        panel2.add(r_text);

        panel3.setLayout(new FlowLayout());
        panel3.add(g);
        panel3.add(g_text);

        panel4.setLayout(new FlowLayout());
        panel4.add(b);
        panel4.add(b_text);

        left.setLayout(new GridLayout(5,0));
        left.add(panel1);
        left.add(panel2);
        left.add(panel3);
        left.add(panel4);


    }

    private class myhandler implements ActionListener{
        int red1;
        int blue1;
        int green1;
        public void actionPerformed (ActionEvent event){

            if(event.getSource()== r_text){
                String a = event.getActionCommand();
                int a2 = Integer.parseInt(a);
                Color mycolor = new Color(a2);
                red.setBackground(mycolor);


            }

            if(event.getSource()== b_text){
                String a = event.getActionCommand();
                int a2 = Integer.parseInt(a);
                Color mycolor = new Color(a2);
                red.setBackground(mycolor);
                }

            if(event.getSource()== g_text){
                String a = event.getActionCommand();
                int a2 = Integer.parseInt(a);
                Color mycolor = new Color(a2);
                red.setBackground(mycolor);

            }
            else {System.out.println("error!");}







        }
    }

}

我没有收到错误,只是没有发生错误,我认为情况不正确

1 个答案:

答案 0 :(得分:2)

JTextField JTextField r_text = new JTextField(4); r_text = new JTextField(4); 个变量。取代

JColorChooser

{{1}}

同样适用于其他颜色组件。

除此之外:考虑使用{{1}}。阅读shadowing