2017-02-04 1 views
0

Java Swing을 사용하여 Java로 프로그램을 작성하고 있습니다. 클래스 JPanel (내 클래스 JPanel 확장) 사용자 지정 JPanel, 어떤 로그인 페이지입니다. 패널에는 "Enter"버튼이 있습니다.다른 클래스에서 버튼을 눌렀을 때 Java 스윙 변경 패널

메인 JFrame을 만들 때 로그인 패널을 추가합니다. "Enter"버튼을 누르면 Log In 패널이 제거되고 다음 패널로 진행됩니다.

내 프레임 은 로그인 패널의 "Enter"버튼을 누르면 (다른 클래스에 있음) 다음 페이지로 진행할 때을 이해하려면 어떻게해야합니까?

+2

어떻게? 내가 계산 방법을 ...합시다. 진지하게, 너무 많습니다. 당신은 부모 클래스가'LogIn' 패널에'ActionListener'를 등록하게 할 수 있습니다. 그러나 좀 더 견고한 해결책은 MVC의 일종을 사용하는 것입니다 [예를 들어] (http://stackoverflow.com/questions/26517856/java) -and-gui-do-do-actionlistener-mvc-pattern/26518274 # 26518274) 및 [example] (http://stackoverflow.com/questions/27663306/open-a-jpanel-after- press-a-button-in-a-jframe/27663749 # 27663749) – MadProgrammer

+1

[Observer Pattern] (http://www.oodesign.com/observer-pattern.html) – MadProgrammer

+0

감사합니다. 그 사실을 알지 못했습니다. – user3309479

답변

0

JPanel의 그들을 둘러싸 beetween CardLayout에로 전환 할 수 있습니다 :

JPanel cards; 
final static String BUTTONPANEL = "Card with JButtons"; 
final static String TEXTPANEL = "Card with JTextField"; 

//Where the components controlled by the CardLayout are initialized: 
//Create the "cards". 
JPanel card1 = new JPanel(); 
... 
JPanel card2 = new JPanel(); 
... 

//Create the panel that contains the "cards". 
cards = new JPanel(new CardLayout()); 
cards.add(card1, BUTTONPANEL); 
cards.add(card2, TEXTPANEL); 

당신은 "입력"버튼 ActonListener을 추가해야합니다 :

JButton enterButton = ... 
enterButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
       CardLayout cl = (CardLayout)// your reference to the CardLayout here eg. yourJFrame.getContentPane().getLayout(); 
       cl.show(cards, "The name of panel to show, you gave it with the add operation on cardLayout eg. BUTTONPANEL OR TEXTPANEL"); 
     } 

}); 
+2

개념적으로 "작업"관리는 "패널"의 책임이며 탐색은 "컨테이너"의 책임이므로 "패널"은 작업이 완료되면 "컨테이너"를 알려야하고 " 그런 다음해야 할 일을 결정해야합니다. 이것은 단일 책임 패러다임에 대한 느슨한 결합과 준수를 유지합니다. 나는 어디에서'enterButton'이 hierarcy에 있는지 전혀 모르기 때문에 언급합니다;) – MadProgrammer

+0

그래,하지만 질문에 대한 간단한 대답으로 그것을 자세히 설명하려고합니다 : 버튼 클릭시 패널을 전환하는 방법은 무엇입니까? 내 손님이되고 대답을 향상 시키십시오 :) –

+2

[나는 필요가 없습니다] (http://stackoverflow.com/questions/26517856/java-and-gui-where-do-actionlisteners-belong-according-to-mvc -pattern/26518274 # 26518274) 또는 적어도 [다시는] (http://stackoverflow.com/questions/27663306/open-a-jpanel-after-pressing-a-button-in-a-jframe/27663749#27663749) : P – MadProgrammer

관련 문제