2016-10-30 5 views
0

JPanel(). next와 JPanel().을 사용하여 다음 패널로 이동하는 대신 이전에 버튼을 사용하여 특정 패널로 전환하고 싶습니다.버튼을 사용하여 특정 JPanel로 전환하는 방법은 무엇입니까?

'3 페이지로 이동'이라는 버튼을 사용하여 3 페이지가 있다고하면 3 페이지에 대해 생성 한 패널로 이동하게됩니다. 그 페이지에는 페이지 1이나 2 페이지로 돌아갈 수있는 버튼이 더 있습니다. 10 번째 페이지가 있다면 버튼 하나만 있으면 바로 잡을 수 있고 다음 버튼을 클릭하지 않아도됩니다.

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

/* Here we are first declaring our class that will act as the 
* base for other panels or in other terms the base for CardLayout. 
*/ 

public class CardLayoutExample 
{ 
    private static final String CARD_JBUTTON = "Card JButton"; 
    private static final String CARD_JTEXTFIELD = "Card JTextField";  
    private static final String CARD_JRADIOBUTTON = "Card JRadioButton"; 

private static void createAndShowGUI() 
{ 
    JFrame frame = new JFrame("Card Layout Test"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLocationRelativeTo(null); 

    // This JPanel is the base for CardLayout for other JPanels. 
    final JPanel contentPane = new JPanel(); 
    contentPane.setLayout(new CardLayout(20, 20)); 

    /* Here we are making objects of the Window Series classes 
    * so that, each one of them can be added to the JPanel 
    * having CardLayout. 
    */ 
    Window1 win1 = new Window1(); 
    contentPane.add(win1, CARD_JBUTTON); 
    Window2 win2 = new Window2(); 
    contentPane.add(win2, CARD_JTEXTFIELD); 
    Window3 win3 = new Window3(); 
    contentPane.add(win3, CARD_JRADIOBUTTON); 

    /* We need two JButtons to go to the next Card 
    * or come back to the previous Card, as and when 
    * desired by the User. 
    */ 
    JPanel buttonPanel = new JPanel(); 
    final JButton page1Button = new JButton("Go to page 1"); 
    final JButton page5Button = new JButton("Go to Page 5"); 
    final JButton page10Button = new JButton("Go to Page 10"); 
    buttonPanel.add(page1Button); 
    buttonPanel.add(page5Button); 
    buttonPanel.add(page10Button); 

    /* Adding the ActionListeners to the JButton, 
    * so that the user can see the next Card or 
    * come back to the previous Card, as desired. 
    */ 
    page1Button.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent ae) 
     { 
      CardLayout cardLayout = (CardLayout) contentPane.getLayout(); 
      cardLayout.previous(contentPane); 
     } 
    }); 
    page5Button.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent ae) 
     { 
      CardLayout cardLayout = (CardLayout) contentPane.getLayout(); 
      cardLayout.next(contentPane); 
     } 
    }); 

    //page10Button.addActionListener(new ActionListener(); 
    //Code to navigate to page 10... 

    // Adding the contentPane (JPanel) and buttonPanel to JFrame. 
    frame.add(contentPane, BorderLayout.CENTER); 
    frame.add(buttonPanel, BorderLayout.PAGE_END); 

    frame.pack(); 
    frame.setVisible(true); 
} 

public static void main(String... args) 
{ 
    SwingUtilities.invokeLater(new Runnable() 
    { 
     public void run() 
     { 
      createAndShowGUI(); 
     } 
    }); 
} 
} 

나는 나의 방법은 내가 버튼을 클릭 할 때를 위해 설정되어 있지만, 그것은 단지 다음 페이지가 아닌 내가 원하는 일에 이동합니다.

.next와 .previous의 다른 대안은 무엇입니까? 나는 특정 페이지로 가고 싶다.

도움 주셔서 감사합니다.

+1

https://docs.oracle.com/javase/8/docs/api/java/awt/CardLayout을 확인해야합니다. html # show-java.awt.Container-java.lang.String-, https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html –

답변

2

카드 레이아웃에 추가 할 때 나중에 특정 패널을 표시 할 때 참조 할 수있는 "키"를 지정할 수 있습니다. 아래의 샘플은 시작할 수 있어야합니다

CardLayout myCardLayout = new CardLayout(); 
JPanel myCardLayoutPanel = new JPanel(myCardLayout); 
myCardLayoutPanel.add(myComponent, "A_KEY"); 
myCardLayout.show(myCardLayoutPanel,"A_KEY"); 

은 또한 당신은 docs

관련 문제