2013-05-05 2 views
1

subpanel이라는 사용자 정의 패널로 여러 개의 JFrames를 실행하려고합니다. [이름에 대해 궁금한 점이 있으시면 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 개) 패널의 최대있을 것입니다. 따라서 활성 패널을 유지하는 목록을 작성할 필요가 없습니다.

+0

* * "나는 여러 JFrames를 시작하려고"을 참조하십시오 [복수 JFrames, 좋은/나쁜 연습의 사용?] (http://stackoverflow.com/a/9554657/418556) –

+0

+1 ['Action'] (http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html). – trashgod

+0

@AndrewThompson 나는 뭔가를위한 모델을 만들고 있습니다. 내 신청서의 범위를 변경할 수 있다고 생각합니다. 열 수있는 창은 최대 5 개입니다. – Rhs

답변

2

대신 현재 색을 보유하는 PanelColorModel을 만드십시오. 관심있는 패널을 observer pattern 구현 중 하나를 사용하여 here 중 하나를 사용하여이 모델의 수신기로 등록하십시오. 그러면 Action이 모델을 업데이트 할 수 있으며 이에 따라 리스너도 이에 대응할 수 있습니다.

0

static Color 속성을 정의 할 수 있으므로 Enter 키를 누를 때마다 모든 부속 패널의 색상이 동일합니다. 뭔가 같이 :

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

정확하지 않다고 생각합니다. 모든 패널이 정적'color' 변수를 공유하더라도 각 패널에 대해 setBackground를 호출하는 이벤트가 필요합니다. – Rhs