Java 如何使用文本字段,以便它们可以用作RGB颜色的输入,以更改文本的颜色?

Java 如何使用文本字段,以便它们可以用作RGB颜色的输入,以更改文本的颜色?,java,swing,button,colors,rgb,Java,Swing,Button,Colors,Rgb,我已经在下面发布了我的代码,但本质上我遇到了一点小麻烦。 我正在尝试创建一个程序,将采取3个文本输入,这些是红色,绿色和蓝色。 这个想法是文本以红色开始,当按下“更改颜色”按钮时。将获取输入的RGB值,并根据这些值更改颜色。 但是,我在获取输入到文本字段中的值以供程序获取和更改颜色时遇到困难。感谢您的帮助 在代码中手动编辑文本值和颜色值时,我在处理程序中同时更改文本值和颜色值时也遇到了问题。它要么改变颜色,要么改变文字 任何帮助都将不胜感激D package RGBProgram; impor

我已经在下面发布了我的代码,但本质上我遇到了一点小麻烦。 我正在尝试创建一个程序,将采取3个文本输入,这些是红色,绿色和蓝色。 这个想法是文本以红色开始,当按下“更改颜色”按钮时。将获取输入的RGB值,并根据这些值更改颜色。 但是,我在获取输入到文本字段中的值以供程序获取和更改颜色时遇到困难。感谢您的帮助

在代码中手动编辑文本值和颜色值时,我在处理程序中同时更改文本值和颜色值时也遇到了问题。它要么改变颜色,要么改变文字

任何帮助都将不胜感激D

package RGBProgram;

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

public class RGB extends JApplet {
    Color col = new Color(255, 0, 0);
    String str = "Hello";
    JButton butReset, butChange;
    JTextField textR, textG, textB;

    public void init(){
        butReset = new JButton("Reset");
        butChange = new JButton("Change Colour");
        textR = new JTextField("Red", 10);
        textG = new JTextField("Green", 10);
        textB = new JTextField("Blue", 10);

        RGBPanel panel = new RGBPanel(this);
        JPanel butPanel = new JPanel();
        JPanel textPanel = new JPanel();
        butPanel.add(butReset);
        butPanel.add(butChange);
        textPanel.add(textR);
        textPanel.add(textG);
        textPanel.add(textB);
        add(panel, BorderLayout.CENTER);
        add(butReset, BorderLayout.NORTH);
        add(butChange, BorderLayout.SOUTH);
        add(textPanel, BorderLayout.WEST);

        Handler reset = new Handler(this);
        Handler change = new Handler(this);

        textR.addActionListener (new Handler(this));
        textG.addActionListener (new Handler(this));
        textB.addActionListener (new Handler(this));
        butReset.addActionListener(reset);
        butChange.addActionListener(change);

    }

    class RGBPanel extends JPanel{
        RGB theApplet;

            RGBPanel(RGB app){
                theApplet = app;
            }

        public void paintComponent(Graphics g)
        {super.paintComponent(g);
        Color cols = col;
        String str1 = str;
        g.setColor(cols);
        g.drawString(str1, 0, 150);
        }
    }
}

package RGBProgram;

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

public class Handler implements ActionListener {
     RGB theApplet;
    Handler(RGB app){
        theApplet = app;
    }

    public void actionPerformed(ActionEvent e){
        String red = theApplet.textR.getText();
        String green = theApplet.textG.getText();
        String blue = theApplet.textB.getText();
        theApplet.textR.setText("");
        theApplet.textG.setText("");
        theApplet.textB.setText("");

        try{
            int r = Integer.parseInt(red.trim());
            int g = Integer.parseInt(green.trim());
            int b = Integer.parseInt(blue.trim());

        }
        catch (NumberFormatException ex){

        }

        if (e.getSource() == theApplet.butChange)
            theApplet.str = "Goodbye";
            theApplet.col = new Color(r, g, b);
        if (e.getSource() == theApplet.butReset)
            theApplet.str = "Hello";
            theApplet.col = new Color(255, 0, 0);
    theApplet.repaint();
    }

}

我将
Handler
类中的
actionPerformed
方法更改为如下,颜色现在已正确应用:

public void actionPerformed(ActionEvent e) {
    String red = theApplet.textR.getText();
    String green = theApplet.textG.getText();
    String blue = theApplet.textB.getText();
    theApplet.textR.setText("");
    theApplet.textG.setText("");
    theApplet.textB.setText("");

    try {
        int r = Integer.parseInt(red.trim());
        int g = Integer.parseInt(green.trim());
        int b = Integer.parseInt(blue.trim());
        if (e.getSource() == theApplet.butChange)
            theApplet.str = "Goodbye";
        theApplet.col = new Color(r, g, b);
        if (e.getSource() == theApplet.butReset)
            theApplet.str = "Hello";
        theApplet.repaint();
    } catch (NumberFormatException ex) {
        ex.printStackTrace();
    }
}

我将
Handler
类中的
actionPerformed
方法更改为如下,颜色现在已正确应用:

public void actionPerformed(ActionEvent e) {
    String red = theApplet.textR.getText();
    String green = theApplet.textG.getText();
    String blue = theApplet.textB.getText();
    theApplet.textR.setText("");
    theApplet.textG.setText("");
    theApplet.textB.setText("");

    try {
        int r = Integer.parseInt(red.trim());
        int g = Integer.parseInt(green.trim());
        int b = Integer.parseInt(blue.trim());
        if (e.getSource() == theApplet.butChange)
            theApplet.str = "Goodbye";
        theApplet.col = new Color(r, g, b);
        if (e.getSource() == theApplet.butReset)
            theApplet.str = "Hello";
        theApplet.repaint();
    } catch (NumberFormatException ex) {
        ex.printStackTrace();
    }
}

请考虑使用<代码> JColorChooser <代码>。它是用来选择颜色的!我的答案似乎不再有效:)考虑改用
JColorChooser
。它是用来选择颜色的!我的答案似乎不再有效:)