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.*;

导入 java.awt.event。; 导入 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 );

} }

在 public void ActionPerformed 我能够使用 cardManager.first、next、previous、last。我只希望显示“处理”面板(所以 card2),而不是用户首先看到、最后看到或循环浏览所有面板。

4

1 回答 1

2

Check out the Java Trail for CardLayout. The cardManager.show() call in the ActionListener should be

cardManager.show( deck, "c2" );

as the first parameter is the parent container with a CardLayout, not the component that you want to display.

于 2011-11-29T21:24:28.410 回答