在卡布局中显示某些面板

时间:2011-11-29 20:57:25

标签: user-interface label panel frame cardlayout

所以我正在用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 );

其中的代码是enitre形式:

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 );

} }

in public void ActionPerformed我能够使用cardManager.first,next,previous,last。我只希望显示“处理”面板(所以卡2),而不是用户首先看到,持续看到或循环显示所有面板。

1 个答案:

答案 0 :(得分:2)

查看CardLayout的Java Trail。 ActionListener中的cardManager.show()调用应为

cardManager.show( deck, "c2" );

作为第一个参数是具有CardLayout的父容器,而不是您要显示的组件。