public class VisibilityDemoExercise extends JFrame
1: import javax.swing.*;
2: import java.awt.*;
3: import java.awt.event.*;
4:
5: public class VisibilityDemoExercise extends JFrame
6: implements ActionListener
7: {
8: public static final int WIDTH = 300;
9: public static final int HEIGHT = 200;
10:
11: private JLabel upLabel;
12: private JLabel downLabel;
13: private JButton upButton;
14: private JButton downButton;
15:
16: public VisibilityDemoExercise()
17: {
18: setSize(WIDTH, HEIGHT);
19: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
20: setTitle("Visibility Demonstration");
21: Container contentPane = getContentPane();
22: contentPane.setLayout(new BorderLayout());
23: contentPane.setBackground(Color.WHITE);
24:
25: upLabel = new JLabel("Here I am up here!");
26: contentPane.add(upLabel, BorderLayout.NORTH);
27: upLabel.setVisible(false);
28: downLabel = new JLabel("Here I am down here!");
29: contentPane.add(downLabel, BorderLayout.SOUTH);
30: downLabel.setVisible(false);
31:
32: JPanel buttonPanel = new JPanel();
33: buttonPanel.setBackground(Color.WHITE);
34: buttonPanel.setLayout(new FlowLayout());
35: upButton = new JButton("Up");
36: upButton.addActionListener(this);
37: buttonPanel.add(upButton);
38: downButton = new JButton("Down");
39: downButton.addActionListener(this);
40: buttonPanel.add(downButton);
41: contentPane.add(buttonPanel, BorderLayout.CENTER);
42: }
43:
44: public void actionPerformed(ActionEvent e)
45: {
46: if (e.getActionCommand().equals("Up"))
47: {
48: upLabel.setVisible(true);
49: downLabel.setVisible(false);
50: upButton.setVisible(false);
51: downButton.setVisible(true);
52: validate();
53: }
54: else if (e.getActionCommand().equals("Down"))
55: {
56: downLabel.setVisible(true);
57: upLabel.setVisible(false);
58: downButton.setVisible(false);
59: upButton.setVisible(true);
60: validate();
61: }
62: else
63: System.out.println(
64: "Error in VisibilityDemoExercise interface.");
65: }
66:
67: public static void main(String[] args)
68: {
69: VisibilityDemoExercise demoGui = new VisibilityDemoExercise();
70: demoGui.setVisible(true);
71: }
72: }