1  import java.awt.*;
  2  import java.awt.event.ActionListener;
  3  import java.awt.event.ActionEvent;
  4  import javax.swing.*;
  5  import javax.swing.border.*;
  6  
  7  public class BorderDemo extends JApplet {
  8    // Declare a label for displaying message
  9    private JLabel jLabel1 = new JLabel("Display the border type",
 10      JLabel.CENTER);
 11  
 12    // A check box for selecting a border with or without a title
 13    private JCheckBox jchkTitled;
 14  
 15    // Radio buttons for border styles
 16    private JRadioButton jrbLoweredBevel, jrbRaisedBevel,
 17      jrbEtched, jrbLine, jrbMatte, jrbEmpty;
 18  
 19    // Radio buttons for titled border options
 20    private JRadioButton jrbAboveBottom, jrbBottom,
 21      jrbBelowBottom, jrbAboveTop, jrbTop, jrbBelowTop,
 22      jrbLeft, jrbCenter, jrbRight;
 23  
 24    // TitledBorder for the label
 25    private TitledBorder jLabel1Border;
 26  
 27    /** Constructor */
 28    public BorderDemo() {
 29      // Create a JLabel instance and set colors
 30      jLabel1.setBackground(Color.yellow);
 31      jLabel1.setBorder(jLabel1Border);
 32  
 33      // Place title position radio buttons
 34      JPanel jpPosition = new JPanel();
 35      jpPosition.setLayout(new GridLayout(3, 2));
 36      jpPosition.add(
 37        jrbAboveBottom = new JRadioButton("ABOVE_BOTTOM"));
 38      jpPosition.add(jrbAboveTop = new JRadioButton("ABOVE_TOP"));
 39      jpPosition.add(jrbBottom = new JRadioButton("BOTTOM"));
 40      jpPosition.add(jrbTop = new JRadioButton("TOP"));
 41      jpPosition.add(
 42        jrbBelowBottom = new JRadioButton("BELOW_BOTTOM"));
 43      jpPosition.add(jrbBelowTop = new JRadioButton("BELOW_TOP"));
 44      jpPosition.setBorder(new TitledBorder("Position"));
 45  
 46      // Place title justification radio buttons
 47      JPanel jpJustification = new JPanel();
 48      jpJustification.setLayout(new GridLayout(3,1));
 49      jpJustification.add(jrbLeft = new JRadioButton("LEFT"));
 50      jpJustification.add(jrbCenter = new JRadioButton("CENTER"));
 51      jpJustification.add(jrbRight = new JRadioButton("RIGHT"));
 52      jpJustification.setBorder(new TitledBorder("Justification"));
 53  
 54      // Create panel jpTitleOptions to hold jpPosition and
 55      // jpJustification
 56      JPanel jpTitleOptions = new JPanel();
 57      jpTitleOptions.setLayout(new BorderLayout());
 58      jpTitleOptions.add(jpPosition, BorderLayout.CENTER);
 59      jpTitleOptions.add(jpJustification, BorderLayout.EAST);
 60  
 61      // Create Panel jpTitle to hold a check box and title position
 62      // radio buttons, and title justification radio buttons
 63      JPanel jpTitle = new JPanel();
 64      jpTitle.setBorder(new TitledBorder("Border Title"));
 65      jpTitle.setLayout(new BorderLayout());
 66      jpTitle.add(jchkTitled = new JCheckBox("Titled"),
 67        BorderLayout.NORTH);
 68      jpTitle.add(jpTitleOptions, BorderLayout.CENTER);
 69  
 70      // Group radio buttons for title position
 71      ButtonGroup btgTitlePosition = new ButtonGroup();
 72      btgTitlePosition.add(jrbAboveBottom);
 73      btgTitlePosition.add(jrbBottom);
 74      btgTitlePosition.add(jrbBelowBottom);
 75      btgTitlePosition.add(jrbAboveTop);
 76      btgTitlePosition.add(jrbTop);
 77      btgTitlePosition.add(jrbBelowTop);
 78  
 79      // Group radio buttons for title justification
 80      ButtonGroup btgTitleJustification = new ButtonGroup();
 81      btgTitleJustification.add(jrbLeft);
 82      btgTitleJustification.add(jrbCenter);
 83      btgTitleJustification.add(jrbRight);
 84  
 85      // Create Panel jpBorderStyle to hold border style radio buttons
 86      JPanel jpBorderStyle = new JPanel();
 87      jpBorderStyle.setBorder(new TitledBorder("Border Style"));
 88      jpBorderStyle.setLayout(new GridLayout(6, 1));
 89      jpBorderStyle.add(jrbLoweredBevel =
 90        new JRadioButton("Lowered Bevel"));
 91      jpBorderStyle.add(jrbRaisedBevel =
 92        new JRadioButton("Raised Bevel"));
 93      jpBorderStyle.add(jrbEtched = new JRadioButton("Etched"));
 94      jpBorderStyle.add(jrbLine = new JRadioButton("Line"));
 95      jpBorderStyle.add(jrbMatte = new JRadioButton("Matte"));
 96      jpBorderStyle.add(jrbEmpty = new JRadioButton("Empty"));
 97  
 98      // Group radio buttons for border styles
 99      ButtonGroup btgBorderStyle = new ButtonGroup();
100      btgBorderStyle.add(jrbLoweredBevel);
101      btgBorderStyle.add(jrbRaisedBevel);
102      btgBorderStyle.add(jrbEtched);
103      btgBorderStyle.add(jrbLine);
104      btgBorderStyle.add(jrbMatte);
105      btgBorderStyle.add(jrbEmpty);
106  
107      // Create Panel jpAllChoices to place jpTitle and jpBorderStyle
108      JPanel jpAllChoices = new JPanel();
109      jpAllChoices.setLayout(new BorderLayout());
110      jpAllChoices.add(jpTitle, BorderLayout.CENTER);
111      jpAllChoices.add(jpBorderStyle, BorderLayout.EAST);
112  
113      // Place panels in the frame
114      setLayout(new BorderLayout());
115      add(jLabel1, BorderLayout.CENTER);
116      add(jpAllChoices, BorderLayout.SOUTH);
117  
118      // Register listeners
119      ActionListener listener = new EventListener();
120      jchkTitled.addActionListener(listener);
121      jrbAboveBottom.addActionListener(listener);
122      jrbBottom.addActionListener(listener);
123      jrbBelowBottom.addActionListener(listener);
124      jrbAboveTop.addActionListener(listener);
125      jrbTop.addActionListener(listener);
126      jrbBelowTop.addActionListener(listener);
127      jrbLeft.addActionListener(listener);
128      jrbCenter.addActionListener(listener);
129      jrbRight.addActionListener(listener);
130      jrbLoweredBevel.addActionListener(listener);
131      jrbRaisedBevel.addActionListener(listener);
132      jrbLine.addActionListener(listener);
133      jrbEtched.addActionListener(listener);
134      jrbMatte.addActionListener(listener);
135      jrbEmpty.addActionListener(listener);
136    }
137  
138    private class EventListener implements ActionListener {
139      @Override /** Handle ActionEvents on check box and radio buttons */
140      public void actionPerformed(ActionEvent e) {
141        // Get border style
142        Border border = new EmptyBorder(2, 2, 2, 2);
143  
144        if (jrbLoweredBevel.isSelected()) {
145          border = new BevelBorder(BevelBorder.LOWERED);
146          jLabel1.setText("Lowered Bevel Style");
147        }
148        else if (jrbRaisedBevel.isSelected()) {
149          border = new BevelBorder(BevelBorder.RAISED);
150          jLabel1.setText("Raised Bevel Style");
151        }
152        else if (jrbEtched.isSelected()) {
153          border = new EtchedBorder();
154          jLabel1.setText("Etched Style");
155        }
156        else if (jrbLine.isSelected()) {
157          border = new LineBorder(Color.black, 5);
158          jLabel1.setText("Line Style");
159        }
160        else if (jrbMatte.isSelected()) {
161          border = new MatteBorder(15, 15, 15, 15,
162            new ImageIcon(getClass().getResource
163              ("/image/caIcon.gif")));
164          jLabel1.setText("Matte Style");
165        }
166        else if (jrbEmpty.isSelected()) {
167          border = new EmptyBorder(2, 2, 2, 2);
168          jLabel1.setText("Empty Style");
169        }
170  
171        if (jchkTitled.isSelected()) {
172          // Get the title position and justification
173          int titlePosition = TitledBorder.DEFAULT_POSITION;
174          int titleJustification = TitledBorder.DEFAULT_JUSTIFICATION;
175  
176          if (jrbAboveBottom.isSelected())
177            titlePosition = TitledBorder.ABOVE_BOTTOM;
178          else if (jrbBottom.isSelected())
179            titlePosition = TitledBorder.BOTTOM;
180          else if (jrbBelowBottom.isSelected())
181            titlePosition = TitledBorder.BELOW_BOTTOM;
182          else if (jrbAboveTop.isSelected())
183            titlePosition = TitledBorder.ABOVE_TOP;
184          else if (jrbTop.isSelected())
185            titlePosition = TitledBorder.TOP;
186          else if (jrbBelowTop.isSelected())
187            titlePosition = TitledBorder.BELOW_TOP;
188  
189          if (jrbLeft.isSelected())
190            titleJustification = TitledBorder.LEFT;
191          else if (jrbCenter.isSelected())
192            titleJustification = TitledBorder.CENTER;
193          else if (jrbRight.isSelected())
194            titleJustification = TitledBorder.RIGHT;
195  
196          jLabel1Border = new TitledBorder("A Title");
197          jLabel1Border.setBorder(border);
198          jLabel1Border.setTitlePosition(titlePosition);
199          jLabel1Border.setTitleJustification(titleJustification);
200          jLabel1.setBorder(jLabel1Border);
201        }
202        else {
203          jLabel1.setBorder(border);
204        }
205      }
206    }
207  
208    public static void main(String[] args) {
209      BorderDemo applet = new BorderDemo();
210      JFrame frame = new JFrame();
211      //EXIT_ON_CLOSE == 3
212      frame.setDefaultCloseOperation(3);
213      frame.setTitle("BorderDemo");
214      frame.getContentPane().add(applet, BorderLayout.CENTER);
215      applet.init();
216      applet.start();
217      frame.setSize(400,320);
218      frame.setLocationRelativeTo(null);
219      frame.setVisible(true);
220    }
221  }