-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardDeck.java
77 lines (71 loc) · 3.06 KB
/
CardDeck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package card_deck_1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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 static void main(String[] args) {
CardDeck frame = new CardDeck();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public CardDeck() {
super( "CardLayout " );
Container container = getContentPane();
// create the JPanel with CardLayout
deck = new JPanel();
cardManager = new CardLayout();
deck.setLayout( cardManager );
// add cards to JPanel deck
deck.add( card1Panel(), "c1" );
deck.add( card2Panel(), "c2" );
deck.add( card3Panel(), "c3" );
// 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 count = 0; count < controls.length; count++ ) {
controls[ count ] = new JButton( names[ count ] );
controls[ count ].addActionListener( this );
buttons.add( controls[ count ] );
}
// add JPanel deck and JPanel buttons to the applet
container.add( buttons, BorderLayout.WEST );
container.add( deck, BorderLayout.EAST );
setSize( 450, 200 );
setVisible( true );
} // end constructor
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;
}
// handle button events by switching cards
public void actionPerformed( ActionEvent event ) {
if ( event.getSource() == controls[ 0 ] )
cardManager.first( deck ); // show first card
else if ( event.getSource() == controls[ 1 ] )
cardManager.next( deck ); // show next card
else if ( event.getSource() == controls[ 2 ] )
cardManager.previous( deck ); // show previous card
else if ( event.getSource() == controls[ 3 ] )
cardManager.last( deck ); // show last card
}
} // end class