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