Source of BorderDemo.java


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