gpt4 book ai didi

java - 更改方形文本字段的颜色

转载 作者:行者123 更新时间:2023-12-02 05:44:39 24 4
gpt4 key购买 nike

我正在尝试创建一个布局,该布局将采用 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!");}







}
}

}

我没有收到错误,它只是没有发生,我认为的条件不正确

最佳答案

你是shadowing您的 JTextField 变量。替换

JTextField r_text = new JTextField(4);

r_text = new JTextField(4);

这同样适用于其他颜色分量。

旁白:考虑使用JColorChooser。阅读 How to Use Color Choosers

关于java - 更改方形文本字段的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24206125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com