当前位置: 技术问答>java相关
哪位大侠能做一个CardLayout 的示例程序?
来源: 互联网 发布时间:2017-03-15
本文导语: 在学布局管理器时CardLayout 始终做不出来 哪位哥哥能给做一个示例程序?让小弟走出迷雾 谢谢 | import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CardDeck extends JFrame ...
在学布局管理器时CardLayout 始终做不出来 哪位哥哥能给做一个示例程序?让小弟走出迷雾
谢谢
谢谢
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CardDeck extends JFrame
implements ActionListener {
private CardLayout cardManager;
private JPanel deck;
private JButton controls[];
private String names[] = { "First card", "Next card",
"Previous card", "Last card" };
public CardDeck()
{
super( "CardLayout " );
Container c = getContentPane();
// create the JPanel with CardLayout
deck = new JPanel();
cardManager = new CardLayout();
deck.setLayout( cardManager );
// set up card1 and add it to JPanel deck
JLabel label1 =
new JLabel( "card one", SwingConstants.CENTER );
JPanel card1 = new JPanel();
card1.add( label1 );
deck.add( card1, label1.getText() ); // add card to deck
// set up card2 and add it to JPanel deck
JLabel label2 =
new JLabel( "card two", SwingConstants.CENTER );
JPanel card2 = new JPanel();
card2.setBackground( Color.yellow );
card2.add( label2 );
deck.add( card2, label2.getText() ); // add card to deck
// set up card3 and add it to JPanel deck
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 );
deck.add( card3, label3.getText() ); // add card to deck
// create and layout buttons that will control deck
JPanel buttons = new JPanel();
buttons.setLayout( new GridLayout( 2, 2 ) );
controls = new JButton[ names.length ];
for ( int i = 0; i
import java.awt.*;
import java.awt.event.*;
public class CardDeck extends JFrame
implements ActionListener {
private CardLayout cardManager;
private JPanel deck;
private JButton controls[];
private String names[] = { "First card", "Next card",
"Previous card", "Last card" };
public CardDeck()
{
super( "CardLayout " );
Container c = getContentPane();
// create the JPanel with CardLayout
deck = new JPanel();
cardManager = new CardLayout();
deck.setLayout( cardManager );
// set up card1 and add it to JPanel deck
JLabel label1 =
new JLabel( "card one", SwingConstants.CENTER );
JPanel card1 = new JPanel();
card1.add( label1 );
deck.add( card1, label1.getText() ); // add card to deck
// set up card2 and add it to JPanel deck
JLabel label2 =
new JLabel( "card two", SwingConstants.CENTER );
JPanel card2 = new JPanel();
card2.setBackground( Color.yellow );
card2.add( label2 );
deck.add( card2, label2.getText() ); // add card to deck
// set up card3 and add it to JPanel deck
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 );
deck.add( card3, label3.getText() ); // add card to deck
// create and layout buttons that will control deck
JPanel buttons = new JPanel();
buttons.setLayout( new GridLayout( 2, 2 ) );
controls = new JButton[ names.length ];
for ( int i = 0; i