2011-11-29 159 views
0

因此,我正在使用gui進行研究任務,如果選擇「輸入」,則在左側面板「輸入」,「處理」和「顯示」上有3個框CardLayout那麼第一個面板顯示在右邊,將「屏幕顯示」被選中,則顯示最後一個面板,但我不能得到「處理」這部分顯示:在卡片佈局中製作某些面板

public void actionPerformed(ActionEvent event) { 
    // show first card 
    if (event.getSource() == controls[ 0 ])  
     cardManager.first(deck); 

    else if (event.getSource() == controls[ 1 ])  
     cardManager.show(card2Panel(), "c2"); 
    // show previous card 
    else if (event.getSource() == controls[ 2 ]) 
     cardManager.last(deck); 

在它的代碼是輸入表格:

import java.awt.*; 

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

公共類BookCentre延伸的JFrame實現的ActionListener {

private CardLayout cardManager; 
private JPanel deck; 
private JButton controls[]; 
private String names[] = { "Input", "Processing", "Display"}; 

public BookCentre(){ 
    super("CardLayout"); 
    Container container = getContentPane(); 
    deck = new JPanel(); 
    cardManager = new CardLayout(); 
    deck.setLayout(cardManager); 
    deck.add(card1Panel(), "c1"); 
    deck.add(card2Panel(), "c2"); 
    deck.add(card3Panel(), "c3"); 
    JPanel buttons = new JPanel(); 
    buttons.setLayout(new GridLayout(2, 2)); 
    controls = new JButton[ names.length ]; 
    for (int count = 0; count < controls.length; count++) { 
     controls[ count ] = new JButton(names[ count ]); 
     controls[ count ].addActionListener(this); 
     buttons.add(controls[ count ]); 
     container.add(buttons, BorderLayout.WEST); 
     container.add(deck, BorderLayout.EAST); 
     setSize(450, 200); 
     setVisible(true);} 

}

public JPanel card1Panel(){ 
JLabel label1 = new JLabel("card one", SwingConstants.CENTER); 
JPanel card1 = new JPanel(); 
card1.add(label1); 
return card1; 

}

public JPanel card2Panel(){ 
    JLabel label2 = new JLabel("card two", SwingConstants.CENTER); 
    JPanel card2 = new JPanel(); 
    card2.setBackground(Color.yellow); 
    card2.add(label2); 
    return card2; 

}

public JPanel card3Panel(){ 
    JLabel label3 = new JLabel("card three"); 
    JPanel card3 = new JPanel(); 
    card3.setLayout(new BorderLayout()); 
    card3.add(new JButton("North"), BorderLayout.NORTH); 
    card3.add(new JButton("West"), BorderLayout.WEST); 
    card3.add(new JButton("East"), BorderLayout.EAST); 
    card3.add(new JButton("South"), BorderLayout.SOUTH); 
    card3.add(label3, BorderLayout.CENTER); 
    return card3; 

}

public void actionPerformed(ActionEvent event) { 
    // show first card 
    if (event.getSource() == controls[ 0 ])  
     cardManager.first(deck); 

    else if (event.getSource() == controls[ 1 ])  
     cardManager.show(card2Panel(), "c2"); 
    // show previous card 
    else if (event.getSource() == controls[ 2 ]) 
     cardManager.last(deck);   

}

public static void main(String args[]) { 
    BookCentre cardDeckDemo = new BookCentre(); 
    cardDeckDemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

}}

在公共無效的actionPerformed我能夠使用cardManager.first,接下來,以前的,過去的。我只想要顯示「處理」面板,但不是用戶首先看,最後一個或者騎自行車穿過所有面板。

回答

2

查看Java Trail的CardLayout。在的ActionListener的cardManager.show()調用應該是

cardManager.show(deck, "c2");

的第一個參數是一個CardLayout父容器,不是說你要顯示的組件。

+0

這正是我所需要的,不能相信我錯過了這麼簡單的東西,謝謝! – Clozecall