【问题标题】:oval not changing color when clicking a JButton - Java Swing单击 JButton 时椭圆不改变颜色 - Java Swing
【发布时间】:2016-03-03 14:44:35
【问题描述】:

我在JFrame 中有 3 个JPanel。两个用于JButton,另一个用于绘图画布。 我为每个JButton 生成一个随机颜色,在JPanel 的中间绘制一个椭圆形,并在每次单击JButton 时更改椭圆形的颜色。 问题是颜色没有改变。

CirclePanel.java:

public class CirclePanel extends JPanel
{
    private static final long serialVersionUID = 1L;

    private Color color;

    public Color getColor()
    {
        return this.color;
    }

    public void setColor(Color color)
    {
        this.color = color;
        repaint();
    }

    public CirclePanel()
    {

    }

    @Override
    protected void paintComponent(Graphics g) 
    {
        super.paintComponent(g);

        final int OVAL_WIDTH = this.getWidth() / 2;
        final int OVAL_HEIGHT = this.getHeight() / 2;
        final int xPosition =  OVAL_WIDTH / 2;
        final int yPosition =  OVAL_HEIGHT / 2;

        g.setColor(this.color);
        g.fillOval(xPosition,yPosition,OVAL_WIDTH,OVAL_WIDTH);
    }
}

RightPanel.java 和 LeftPanel.java:

public class RightPanel extends JPanel implements ActionListener
{
    private static final long serialVersionUID = 1L;

    private static final int BUTTONS = 6;

    private CirclePanel circlePanel;

    public RightPanel(CirclePanel circlePanel)
    {
        this.circlePanel = circlePanel;

        this.setLayout(new GridLayout(BUTTONS,0));

        // Adding buttons to the layout
        for (int i = 0;i < RightPanel.BUTTONS;i++)
        {
            JButton button = new JButton();
            button.setBackground(generateColor());
            button.addActionListener(this);
            this.add(button);
        }

        this.setVisible(true);
    }

    public Color generateColor()
    {
        Random random = new Random();
        // 255 - maximum value. 0 - minimum value
        int randomNumber1 = random.nextInt((255 - 0) + 1) + 0;
        int randomNumber2 = random.nextInt((255 - 0) + 1) + 0;
        int randomNumber3 = random.nextInt((255 - 0) + 1) + 0;
        Color color = new Color(randomNumber1,randomNumber2,randomNumber3);
        return color;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        Color color = ((JButton)e.getSource()).getBackground();
        this.circlePanel.setColor(color);
    }
}

public class LeftPanel extends JPanel implements ActionListener
{
    private static final long serialVersionUID = 1L;

    private static final int BUTTONS = 6;

    private CirclePanel circlePanel;

    public LeftPanel(CirclePanel circlePanel)
    {
        this.circlePanel = circlePanel;

        this.setLayout(new GridLayout(BUTTONS,0));

        // Adding buttons to the layout
        for (int i = 0;i < LeftPanel.BUTTONS;i++)
        {
            JButton button = new JButton();
            button.setBackground(generateColor());
            button.addActionListener(this);
            this.add(button);
        }

        this.setVisible(true);
    }

    public Color generateColor()
    {
        Random random = new Random();
        // 255 - maximum value. 0 - minimum value
        int randomNumber1 = random.nextInt((255 - 0) + 1) + 0;
        int randomNumber2 = random.nextInt((255 - 0) + 1) + 0;
        int randomNumber3 = random.nextInt((255 - 0) + 1) + 0;
        Color color = new Color(randomNumber1,randomNumber2,randomNumber3);
        return color;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        Color color = ((JButton)e.getSource()).getBackground();
        this.circlePanel.setColor(color);
    }
}

MyFrame.java:

public class MyFrame extends JFrame 
{
    private static final long serialVersionUID = 1L;
    private CirclePanel circlePanel;

    public MyFrame()
    {
        super("paintComponent");

        this.circlePanel = new CirclePanel();
        this.setLayout(new BorderLayout());
        this.setSize(400,400);
        this.setResizable(false);
        this.add(new LeftPanel(this.circlePanel),BorderLayout.WEST);
        this.add(new RightPanel(this.circlePanel),BorderLayout.EAST);
        this.add(new CirclePanel(),BorderLayout.CENTER);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args)
    {
        new MyFrame();
    }
}

我检查了程序是否获取到每个actionPerformed() 方法,并且确实如此。

那有什么问题呢?

【问题讨论】:

  • 您可能应该执行 `this.circlePanel.revalidate()` 以使更改生效

标签: java swing jbutton onclicklistener paintcomponent


【解决方案1】:

您显示的CirclePanel 不是您传递给左/右面板的那个,请参阅:

this.add(new LeftPanel(this.circlePanel),BorderLayout.WEST);
this.add(new RightPanel(this.circlePanel),BorderLayout.EAST);
this.add(new CirclePanel(),BorderLayout.CENTER);

你可能想这样做:

this.add(new LeftPanel(this.circlePanel),BorderLayout.WEST);
this.add(new RightPanel(this.circlePanel),BorderLayout.EAST);
this.add(this.circlePanel,BorderLayout.CENTER);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-18
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    相关资源
    最近更新 更多