在卡片布局中制作某些面板显示

发布于 2024-12-18 15:58:13 字数 3387 浏览 2 评论 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.; 导入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),而不是用户看到第一个、最后一个或循环浏览所有面板。

So I'm working on a research assignment with a gui and I have 3 boxes on the left panel "Input", "Processing", and "Display", if "Input" is selected using CardLayout then the first panel is shown on the right, if "Display" is selected then the last panel is displayed, but I can't get "Processing" to display in this part:

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

The code in it's enitre form:

import java.awt.*;

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

public class BookCentre extends JFrame implements 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 I was able to use cardManager.first, next, previous, last. I only want the "Processing" panel to be displayed though (so card2), not the user seeing first, last or cycling through all panels.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2024-12-25 15:58:13

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

cardManager.show( Deck, "c2" );

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文