如何更改颜色变量?

时间:2015-04-18 11:20:05

标签: java colors jframe actionlistener

我正在尝试制作JFrame,当用户点击按钮时,JFrame中显示的圆圈每秒都会改变颜色。但目前我无法更改窗口中当前显示的颜色,存储在变量中。

Color lastColor = Color.ORANGE;            
g.setColor(lastColor);

smallerButton.addActionListener(new ActionListener()
{
      public void actionPerformed(ActionEvent e)
      {
            String action = e.getActionCommand();
            if (action.equals("Flash")) {

                  //when clicked change color of the circle listed above.
                  //or change the variable of last color.
            }
      }});
     }
};

这不是所有代码。我想要做的就是当用户点击按钮时,变量lastColor然后被更改为让GRAY。我在尝试执行此操作时遇到了麻烦,因为当我将变量名称放在动作侦听器中时,它无法找到变量lastColor以更改为新变量。如何更改动作侦听器中的变量lastColor

2 个答案:

答案 0 :(得分:1)

您必须将lastColor声明为您班级的成员变量。您正在本地创建它,因此clickListener无法看到它。

编辑:

public class foo(){
Color lastColor;

public foo(){
lastColor = Color.ORANGE();
}

public void paintFoo(){
 // do your paint stuff here and access lastColor
}

}

答案 1 :(得分:0)

您只需创建一个实例变量:

private Color backColor;

点击JButton,只需调用一个方法,即更改此变量的值,然后调用repaint (),以便视图更改绘制的圆的颜色。< / p>

这是一个小例子:

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

public class ColourExample {

    private JPanel drawingBoard;
    private JButton button;
    private JPanel customPanel;

    private static final int GAP = 5;

    private Color [] colours = {
        Color.red,
        Color.blue,
        Color.cyan,
        Color.magenta,
        Color.gray
    };
    private int counter;

    public ColourExample () {
        counter = 0;
    }

    private void displayGUI () {
        JFrame frame = new JFrame ( "" );
        frame.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );

        JPanel contentPane = new JPanel ();
        contentPane.setLayout ( new BorderLayout ( GAP, GAP) );

        customPanel = new CustomPanel ();
        contentPane.add ( customPanel, BorderLayout.CENTER );

        button = new JButton ( "Change Colour" );
        button.addActionListener ( new ActionListener () {
            @Override
            public void actionPerformed ( ActionEvent ae ) {
                System.out.println ( "Counter: " + counter );
                ( ( CustomPanel ) customPanel ).setValues ( colours [ counter++ ] );
                counter %= colours.length;
            }
        } );
        contentPane.add ( button, BorderLayout.PAGE_END );

        frame.setContentPane ( contentPane );
        frame.pack ();
        frame.setLocationByPlatform ( true );
        frame.setVisible ( true );
    }

    public static void main ( String [] args ) {
        Runnable runnable = new Runnable () {
            @Override
            public void run () {
                new ColourExample ().displayGUI ();
            }
        };
        EventQueue.invokeLater ( runnable );
    }
}

class CustomPanel extends JPanel {

    private static final int WIDTH = 400;
    private static final int HEIGHT = 400;
    private static final int RADIUS = 400;

    private Color backColour;

    public CustomPanel ( ) {
        setOpaque ( true );
        backColour = Color.green;       
    }

    public void setValues ( Color backColour ) {
        this.backColour = backColour;
        repaint ();
    }

    @Override
    public Dimension getPreferredSize () {
        return new Dimension ( WIDTH, HEIGHT );
    }

    @Override
    protected void paintComponent ( Graphics g ) {
        super.paintComponent ( g );
        g.setColor ( backColour );
        g.fillOval ( 0, 0, RADIUS, RADIUS );
    }
}