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 개) 패널의 최대있을 것입니다. 따라서 활성 패널을 유지하는 목록을 작성할 필요가 없습니다.
* * "나는 여러 JFrames를 시작하려고"을 참조하십시오 [복수 JFrames, 좋은/나쁜 연습의 사용?] (http://stackoverflow.com/a/9554657/418556) –
+1 ['Action'] (http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html). – trashgod
@AndrewThompson 나는 뭔가를위한 모델을 만들고 있습니다. 내 신청서의 범위를 변경할 수 있다고 생각합니다. 열 수있는 창은 최대 5 개입니다. – Rhs