1

我已经在下面发布了我的代码,但基本上我遇到了一些麻烦。我正在尝试创建一个程序,它将接受 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();
    }

}
4

1 回答 1

1

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

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();
    }
}
于 2012-10-27T15:24:05.930 回答