使用Java Swing控件的Derived类为self创建监听器

时间:2011-07-23 18:03:36

标签: java swing jpanel jbutton actionlistener

一开始:我知道我所做的是糟糕的设计。我正在尝试这样做以更好地了解Java - 什么是可能的,什么不是,为什么?

我写了以下代码:

    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    public class ButtonTest
    {
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    ButtonFrame frame = new ButtonFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }
    }

    @SuppressWarnings("serial")
    class ButtonFrame extends JFrame
    {
        private final static int DEFAULT_WIDTH = 300;
        private final static int DEFAULT_HEIGHT = 200;
        private final JPanel buttonPanel;

        public ButtonFrame()
        {
            this.setTitle("Button Frame");
            this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

            PanelButton yellowButton = new PanelButton("Yellow", Color.YELLOW, this);
            PanelButton redButton = new PanelButton("Red", Color.RED, this);
            PanelButton blueButton = new PanelButton("Blue", Color.BLUE, this);

            this.buttonPanel = new JPanel();
            this.buttonPanel.add(yellowButton);
            this.buttonPanel.add(redButton);
            this.buttonPanel.add(blueButton);

            // add panel to frame
            this.add(this.buttonPanel);
        }
    }

    @SuppressWarnings("serial")
    class PanelButton extends JButton implements ActionListener
    {
        private final Color buttonColor;
        private final ButtonFrame containingFrame;

        public PanelButton(String title, Color buttonColor,
                ButtonFrame containingFrame)
        {
            super(title);
            this.buttonColor = buttonColor;
            this.containingFrame = containingFrame;
            this.addActionListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent event)
        {
            this.containingFrame.setBackground(this.buttonColor);
        }
    }

但这不起作用。我从调试器中看到正在调用actionPerformed()并且它具有预期的值。我无法理解这里发生了什么。有人可以帮助我吗?

2 个答案:

答案 0 :(得分:4)

您正在设置JFrame的背景颜色,JPanel所包含的JFrame会占据您的按钮,占据JFrame的所有空间{1}},因此您无法看到背景颜色发生变化。

这很有效。

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ButtonTest
{
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                ButtonFrame frame = new ButtonFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

@SuppressWarnings("serial")
class ButtonFrame extends JFrame
{
    private final static int DEFAULT_WIDTH = 300;
    private final static int DEFAULT_HEIGHT = 200;
    private final JPanel buttonPanel;

    public ButtonFrame()
    {
        this.setTitle("Button Frame");
        this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        this.buttonPanel = new JPanel();

        PanelButton yellowButton = new PanelButton("Yellow", Color.YELLOW, buttonPanel);
        PanelButton redButton = new PanelButton("Red", Color.RED, buttonPanel);
        PanelButton blueButton = new PanelButton("Blue", Color.BLUE, buttonPanel);

        this.buttonPanel.add(yellowButton);
        this.buttonPanel.add(redButton);
        this.buttonPanel.add(blueButton);

        // add panel to frame
        this.add(this.buttonPanel);
    }
}

@SuppressWarnings("serial")
class PanelButton extends JButton implements ActionListener
{
    private final Color buttonColor;
    private final JPanel buttonPanel;

    public PanelButton(String title, Color buttonColor,
            JPanel buttonPanel)
    {
        super(title);
        this.buttonColor = buttonColor;
        this.buttonPanel = buttonPanel;
        this.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent event)
    {
        buttonPanel.setBackground(this.buttonColor);
    }
}

答案 1 :(得分:3)

您可以跳过传递容器,然后执行

@Override
public void actionPerformed(ActionEvent event)
{
    this.getParent().setBackground(buttonColor);
}