public class BorderDemoWithIcon extends JFrame implements ActionListener
1: import javax.swing.*;
2: import java.awt.*;
3: import java.awt.event.*;
4: import javax.swing.border.*;
5:
6: /**
7: Class to demonstrate adding icons to a border.
8: */
9: public class BorderDemoWithIcon extends JFrame implements ActionListener
10: {
11: public static final int WIDTH = 400;
12: public static final int HEIGHT = 300;
13:
14: private JTextField name;
15:
16: public BorderDemoWithIcon()
17: {
18: setTitle("Name Tester with Borders");
19: setSize(WIDTH, HEIGHT);
20: addWindowListener(new WindowDestroyer());
21: Container content = getContentPane();
22: content.setLayout(new GridLayout(2, 1));
23:
24: JPanel namePanel = new JPanel();
25: namePanel.setLayout(new BorderLayout());
26: namePanel.setBackground(Color.WHITE);
27:
28: name = new JTextField(20);
29: //The following border is not as dramatic as others,
30: //but look closely and you will see it.
31: name.setBorder(new EtchedBorder(Color.GREEN, Color.BLUE));
32: namePanel.add(name, BorderLayout.SOUTH);
33: JLabel nameLabel = new JLabel("Enter your name here:");
34: //The following does insert space around the label.
35: //To see the difference, comment out the following line:
36: nameLabel.setBorder(new EmptyBorder(20, 10, 0, 0));
37: namePanel.add(nameLabel, BorderLayout.CENTER);
38:
39: namePanel.setBorder(new LineBorder(Color.BLACK, 10));
40: content.add(namePanel);
41:
42: JPanel buttonPanel = new JPanel();
43: buttonPanel.setLayout(new FlowLayout());
44: JButton testButton = new JButton("Test");
45: testButton.addActionListener(this);
46: testButton.setBorder(new BevelBorder(BevelBorder.LOWERED));
47: buttonPanel.add(testButton);
48:
49: JButton clearButton = new JButton("Clear");
50: clearButton.addActionListener(this);
51: clearButton.setBorder(new BevelBorder(BevelBorder.RAISED));
52: buttonPanel.add(clearButton);
53:
54:
55: ImageIcon smileyIcon = new ImageIcon("smiley.gif");
56: buttonPanel.setBorder(
57: new MatteBorder(60, 40, 30, 20, smileyIcon));
58: content.add(buttonPanel);
59: }
60:
61: public void actionPerformed(ActionEvent e)
62: {
63: if (e.getActionCommand().equals("Test"))
64: name.setText("A very good name!");
65: else if (e.getActionCommand().equals("Clear"))
66: name.setText("");
67: else
68: name.setText("Error in window interface.");
69: }
70:
71: public static void main(String[] args)
72: {
73: BorderDemoWithIcon w = new BorderDemoWithIcon();
74: w.setVisible(true);
75: }
76: }