【问题标题】:Swing Panels Color Change摇摆面板颜色变化
【发布时间】:2013-04-29 11:26:58
【问题描述】:

我正在尝试使用我创建的名为 subpanel 的自定义面板启动多个 JFrame。 [如果您想知道命名,我还有另一个名为 masterpanel 的类,它包含一个按钮,用于启动一个包含 subpanel 新实例的新框架。

subpanel 的用途是当用户点击enter 按钮时,颜色会发生变化。目前,我让每个subpanel 都包含一个名为EnterAction 的内部类,它调用setBackground 来更改颜色。

我想知道如何修改它,以便我可以在我的所有 subpanels 之间同步颜色变化。

目前,我有一个变量green,我相信我可以在所有面板之间传递它。但是,我不确定如何让EnterAction 更改所有当前活动的面板?

我正在考虑创建一个活跃的subpanels 列表?但是,如果用户关闭subpanel,这会导致我需要维护列表的额外问题吗?

这是我的代码:

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.KeyStroke;

public class SubPanel extends javax.swing.JPanel
{   
    private Action enterAction;

    public SubPanel() 
    {
        initComponents();
        enterAction = new EnterAction();

            //KeyBindings on the enter button
        this.getInputMap().put(KeyStroke.getKeyStroke( "ENTER" ), "doEnterAction");
        this.getActionMap().put( "doEnterAction", enterAction );
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setForeground(new java.awt.Color(1, 1, 1));
        setToolTipText("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>                        
    // Variables declaration - do not modify                     
    // End of variables declaration                   

    private static int green = 240;

    private class EnterAction extends AbstractAction 
    {   
        @Override
        public void actionPerformed(ActionEvent ae) 
        {
            //System.out.println("Enter button is pressed");
            green -= 5;
            if (green <= 0) green = 0;
            setBackground(new java.awt.Color(255, green, 255));
        }
    }
}

编辑:最多有 5 个面板。这消除了创建列表维护活动面板的需要。

【问题讨论】:

标签: java swing events jpanel awt


【解决方案1】:

相反,创建一个保存当前颜色的PanelColorModel。让感兴趣的面板注册为该模型的侦听器,使用建议的observer pattern 实现之一here。然后你的Action 可以更新模型,听众可以做出相应的反应。

【讨论】:

    【解决方案2】:

    您可以尝试定义static Color 属性,这样每次您按 Enter 键时,每个子面板都将具有相同的颜色。比如:

    static Color subpanelBackgroundColor; //Every instance will have this.
    

    【讨论】:

    • 我认为这不准确。即使所有面板共享一个静态 color 变量,仍然需要存在一个为每个面板调用 setBackground 的事件。
    猜你喜欢
    • 2010-09-17
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    相关资源
    最近更新 更多