我在Frame中添加一个TextField文本框,现在我想改变我输入文本的颜色,就是点一下鼠标选那个颜色
就变成那种颜色,应该调用什么方法,处理什么事件?希望大家指点.谢谢!

解决方案 »

  1.   

    package squall;import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;public class ButtonTest
    { public static void main(String[] args)
    {
    ButtonFrame frame = new ButtonFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true); }}class ButtonFrame extends JFrame
    {
    public ButtonFrame()
    {
    setTitle("ButtonTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); ButtonPanel panel = new ButtonPanel();
    add(panel);
    } private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200;
    }class ButtonPanel extends JPanel
    {
    JTextField text = null; public ButtonPanel()
    {
    JButton yellowButton = new JButton("<html><b>Yellow</b></html>");
    JButton blueButton = new JButton("Blue");
    JButton redButton = new JButton("Red");
    text = new JTextField(8); yellowButton.addActionListener(new ColorAction(Color.YELLOW));
    blueButton.addActionListener(new ColorAction(Color.BLUE));
    redButton.addActionListener(new ColorAction(Color.RED)); add(yellowButton);
    add(blueButton);
    add(redButton);
    add(text);
    } private class ColorAction implements ActionListener
    {
    public ColorAction(Color c)
    {
    this.backgroundColor = c;
    } public void actionPerformed(ActionEvent event)
    {
    ButtonPanel.this.text.setBackground(backgroundColor);
    ;
    } private Color backgroundColor;
    }
    }
      

  2.   

     public void actionPerformed(ActionEvent event)里多了一个";"
    删掉好了