【问题标题】:Using a Derived class of a Java Swing control to create listener for self使用 Java Swing 控件的派生类为自己创建侦听器
【发布时间】:2011-10-11 18:00:36
【问题描述】:

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

【问题讨论】:

    标签: java swing jpanel jbutton actionlistener


    【解决方案1】:

    您正在设置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);
        }
    }
    

    【讨论】:

    • 感谢您解释小姐。我更喜欢另一种解决方案,因为它隐藏了容器属于什么类型的事实,因此在其他场景中也更有用。
    • 但是,当然!是的,其他解决方案更好,但我觉得你应该明白你遇到问题的原因(因为那是你问的!)。 :P。编程愉快!
    • 我喜欢最复杂的例子 +1
    【解决方案2】:

    你可以跳过容器直接做

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

    【讨论】:

    • +1 用于单独的解决方案。我的目的是解释为什么他的不起作用,而你的则说明如何做得更好。那里有很好的二元性。
    猜你喜欢
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多