2016-10-30 14 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

関連する問題