is not always feel that the Java button is difficult to look at, especially toolbar buttons, if you can make a cool button like word how good, in fact, with Java to achieve, it is not difficult, as long as 3 steps on it. That simple? Right! It's so simple.
The first step: Prepare a BORDER,JDK not bring a lot of border class, why do you write? Because the JDK border class is too coarse, can not reach our effect, can only write one, but don't be afraid, I have helped you write well, take the use on it.
Step two: Write a mouselistener, handle the various movements of button (move in, move out, press, press, move, press move out), how so many movements, carefully observe the word toolbar to know.
Step three: Generate a JButton object, set border, listener, and some other properties.
Let's see how it's going to be implemented.
The first step: Write a Border class, which we call the Thinbevelborder, let it inherit BevelBorder, and then overwrite the Paintraisedbevel and Paintloweredbevel methods so that it only makes 4 border lines, It won't look so rough. The specific code is as follows:
Package Com.bhr.ioat.coolbutton
Import java.awt.Color;
import java.awt.Component;
Import Java.awt.Graphics;
Import java.awt.Insets;
Import Javax.swing.border.BevelBorder;
public class Thinbevelborder extends BevelBorder
{
Public thinbevelborder (int beveltype)
{
Super ( Beveltype);
}
Public thinbevelborder (int beveltype, color highlight, color shadow)
{
Super (Beveltype, highlight, Shadow);
}
Public thinbevelborder (int beveltype, color highlightoutercolor, color highlightinnercolor, color shadowouter color, color Shadowinnercolor)
{
Super (Beveltype, Highlightoutercolor, Highlightinnercolor, Shadowoutercolor, Shadowinnercolor);
}
protected void Paintraisedbevel (Component C, Graphics G, int x, int y, int width, int height)
{
T Ry
{
Color oldcolor = G.getcolor ();
int h = height;
int w = width;
G.translate (x, y);
G.setcolor (GethighlightiNnercolor (c));
G.drawline (0, 0, 0, h-1);
G.drawline (1, 0, w-1, 0);
G.setcolor (Getshadowinnercolor (c));
G.drawline (1, h-1, w-1, h-1);
G.drawline (w-1, 1, w-1, h-2);
G.translate (-X,-y);
G.setcolor (Oldcolor);
}
catch (NullPointerException e) {}
}
protected void Paintloweredbevel (Component C, Graphics G, in t x, int y, int width, int height)
{
Try
{
Color oldcolor = G.getcolor ();
int h = height;
int w = width;
G.translate (x, y);
G.setcolor (Getshadowinnercolor (c));
G.drawline (0, 0, 0, h-1);
G.drawline (1, 0, w-1, 0);
G.setcolor (Gethighlightoutercolor (c));
G.drawline (1, h-1, w-1, h-1);
G.drawline (w-1, 1, w-1, h-2);
G.translate (-X,-y);
G.setcolor (Oldcolor);
}
catch (NullPointerException e) {}
}
}