使用 Java Swing 控件的派生类为自身创建侦听器

发布于 2024-11-25 12:58:13 字数 2278 浏览 0 评论 0原文

首先:我知道我所做的设计很糟糕。我尝试这样做是为了更好地了解 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() 正在被调用,并且它具有预期的值。我无法理解这里发生了什么。有人可以帮我吗?

At the outset: I know that what I am doing is bad design. I am trying this to get a better feel of Java - what is possible, what is not and why?

I have written the following code:

    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);
        }
    }

But this is not working. I see from the debugger that actionPerformed() is being invoked and it has the expected values. I am not able to understand what is happening here. Can someone please help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不醒的梦 2024-12-02 12:58:13

您正在设置 JFrame 的背景颜色,但是 JFrame 包含的 JPanel(然后保存您的按钮)占据了所有内容JFrame 的空间,因此您看不到背景颜色的变化。

这有效。

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);
    }
}

You are setting the background color of the JFrame, but the JPanel that is contained by the JFrame, which then holds your buttons, takes up all of the space of the JFrame, so you can't see the background color changing.

This works.

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);
    }
}
骷髅 2024-12-02 12:58:13

您可以跳过传递容器并直接执行

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

You can skip passing the container and just do

@Override
public void actionPerformed(ActionEvent event)
{
    this.getParent().setBackground(buttonColor);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文