Borders with a BevelBorder used on JLabels as a highlight : Border « Swing JFC « Java






Borders with a BevelBorder used on JLabels as a highlight

Borders with a BevelBorder used on JLabels as a highlight
   
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;

public class BevelExample extends JPanel {

  BevelBorder bevel;

  EmptyBorder empty;

  JLabel label[] = new JLabel[4];

  public BevelExample() {
    super(true);
    setLayout(new GridLayout(1, 4));

    bevel = new BevelBorder(BevelBorder.RAISED);
    empty = new EmptyBorder(5, 5, 5, 5);

    label[0] = new JLabel("Home");
    label[1] = new JLabel("Back");
    label[2] = new JLabel("Forward");
    label[3] = new JLabel("Stop");

    for (int i = 0; i < label.length; i++) {
      label[i].setHorizontalAlignment(JLabel.CENTER);
      label[i].addMouseListener(new RolloverListener());
      label[i].setBorder(empty);
      add(label[i]);
    }
  }

  public static void main(String s[]) {
    JFrame frame = new JFrame("Bevel Border");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 100);
    frame.setContentPane(new BevelExample());
    frame.setVisible(true);
  }

  // Inner class to respond to mouse events for the "rollover" effect
  class RolloverListener extends MouseAdapter {
    public void mouseEntered(MouseEvent e) {
      ((JLabel) e.getComponent()).setBorder(bevel);
      repaint();
    }

    public void mouseExited(MouseEvent e) {
      ((JLabel) e.getComponent()).setBorder(empty);
      repaint();
    }

    public void mouseClicked(MouseEvent e) {
      String text = ((JLabel) e.getComponent()).getText();
      System.out.println("You clicked " + text + "!");
    }
  }
}

           
         
    
    
  








Related examples in the same category

1.Border Demo Border Demo
2.Different Swing bordersDifferent Swing borders
3.demonstrate the custom CurvedBorder classdemonstrate the custom CurvedBorder class
4.An example of the MatteBorder classAn example of the MatteBorder class
5.A simple demonstration of the LineBorder class built with rounded cornersA simple demonstration of the LineBorder class built with rounded corners
6.An example of using a titled border on a labelAn example of using a titled border on a label
7.MatteBorder DemoMatteBorder Demo
8.TitledBorder DemoTitledBorder Demo
9.Custom Border SampleCustom Border Sample
10.How to create the borderHow to create the border
11.Solid border button border 3D borderSolid border button border 3D border
12.Oval borderOval border
13.Border of all kindsBorder of all kinds
14.EtchedBorderEtchedBorder
15.Red Green BorderRed Green Border
16.TitledBorderTitledBorder
17.TitledBorder 3TitledBorder 3
18.BevelBorder 2BevelBorder 2
19.Icon MatteBorderIcon MatteBorder
20.Soft BevelBorderSoft BevelBorder
21.Another TitledBorder 1Another TitledBorder 1
22.BevelBorderBevelBorder
23.EmptyBorderEmptyBorder
24.LineBorder DemoLineBorder Demo
25.CompoundBorder DemoCompoundBorder Demo
26.Adding a Title to a Border
27.Create a title border from another border
28.Change border text Justification alignment style
29.Bottom Border
30.Right border
31.Rectangle border
32.Set title position
33.Flex Border