[英]Swing Panels Color Change
I am trying to launch multiple JFrames with a custom panel that I created called subpanel
. 我试图启动与我创建调用自定义面板多JFrames
subpanel
。 [In case you are wondering about the naming, I have another class called masterpanel
, which contains a button that launches a new frame containing a new instance of subpanel
. [如果您想知道命名,我还有另一个名为
masterpanel
类,它包含一个按钮,用于启动一个包含新的subpanel
实例的新框架。
The purpose of the subpanel
is that when the user hits the enter
button, the color changes. subpanel
的目的是当用户按下enter
键时,颜色会改变。 Currently, I have each subpanel
contain an inner class called EnterAction
, which calls setBackground
to change the color. 当前,我每个
subpanel
都包含一个名为EnterAction
的内部类,该类调用setBackground
来更改颜色。
I was wondering how I can modify this so that I can synchronize the color change between all of my subpanels
. 我想知道如何修改它,以便可以同步所有
subpanels
之间的颜色变化。
Currently, I have a variable green
, which I believe I can pass between all of my panels. 目前,我有一个可变的
green
,我相信我可以在所有面板之间传递。 However, I'm not sure how I can get the EnterAction
to change all of the currently active panels? 但是,我不确定如何获取
EnterAction
来更改所有当前活动的面板?
I was thinking of creating a list of active subpanels
? 我在考虑创建活动
subpanels
列表吗? But will this cause an additional problem that I would need to maintain the list if the user closes a subpanel
? 但这是否会引起其他问题,如果用户关闭
subpanel
,我将需要维护列表?
Here is my code: 这是我的代码:
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));
}
}
}
EDIT: There's going to be a maximum of 5 panels. 编辑:最多将有5个面板。 This eliminates the need to create a list maintain active panels.
这样就无需创建维护活动面板的列表。
Instead, create a PanelColorModel
that holds the current color. 而是创建一个保存当前颜色的
PanelColorModel
。 Let interested panels register as a listener to this model, using one of the observer pattern implementations suggested here . 使用此处建议的观察者模式实现之一,让感兴趣的面板注册为该模型的侦听器。 Then your
Action
can update the model and listeners can react accordingly. 然后,您的
Action
可以更新模型,并且侦听器可以做出相应的反应。
You can try to define a static
Color attribute, so every time you hit Enter every subpanel will have the same color. 您可以尝试定义一个
static
Color属性,因此每次单击Enter时,每个子面板将具有相同的颜色。 Something like: 就像是:
static Color subpanelBackgroundColor; //Every instance will have this.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.