在卡片布局中制作某些面板显示
因此,我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 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.