Is it that the button made by Java is very difficult, especially the toolbar button, if you can make the COOL button like Word, it is not difficult to use Java, it is not difficult, as long as 3 steps can be. So simple? Correct! It is so simple.
Step 1: Prepare a border, JDK does not bring a lot of Border classes, why should I write myself? Because the Border class in JDK is too thick, you can't reach our results, you can only write one, but don't be afraid, I have helped you write well, take it with it.
Step 2: Write a mouselistener to handle the various action of Button (transfer, transfer, press, press to move into, press the shift), how so many actions, carefully observe the Word toolbar and know.
Step 3: Generate a JButton object, set Border, Listener, and other properties.
Let's take a look at how the specific implementation.
Step 1: Write a Border class, we are called ThinBevelBorder, let it inherit bevelborder, then cover the PaintraisedBevel and PaintLowRedbevel methods, let it onlyify the 4 strip line, which looks so thick. 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 begkorder {
Public ThinBevelBorder (int name) {
Super (beveltype);
}
Public ThinBevelBorder (int, color shadow) {
Super (BevelType, Highlight, Shadow);
}
Public ThinBevelBorder (Int Beveltype, Color Highlightoutercolor,
Color HighlightinnerColor, Color Shadowoutercolor,
Color shadowinnercolor) {
Super (BevelType, Highlightoutercolor, HighlightinnerColor,
Shadowoutercolor, ShadowinnerColor;
}
Protected Void PainTraiadbevel (Component C, Graphics G, INT X, INT Y,
Int width, int Height) {
Try {
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 (nullpointersexception e) {
}
}
Protected Void PainTlowRedbevel (Component C, Graphics G, Int 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 (nullpointersexception e) {
}
}
}
Step 2: Write mouselistener, we call the coolbuttonmouselistener, inherit the mouseadapter, cover 4 methods (MouseEntered, MouseExited, MousePressed, and Mousereleased), here you need to pay attention, the last three methods are just simple according to the condition, the Button's Border The first method is specifically, and when the mouse is moved in addition to whether it is necessary to modify the border, it is necessary to determine the state where the mouse is located, if in the prescription, and the first press is the button, set to press Border of the lower state, if in the prescription, but the first time you press this button, no border is set. The specific code is as follows:
Package com.bhr.ioat.coolbutton;
Import java.awt.event.mouseadapter;
Import java.awt.event.mouseevent;
Import javax.swing.abstractButton;
Import javax.swing.border.border;
Import javax.swing.border.emptyborder;
Public Class CoolbuttonMouseListener
Extends mouseadapter {
Public final static border default_border = new EmptyBorder (2, 2, 2, 2);
Public final static border entered_border = new ThinBevelBorder (ThinBevelBorder.raised);
Public final static border pressed_border = new ThinBevelBorder (ThinBevelBorder.lowered);
Private final static coolbuttonmouselistener listener_ = new coolbuttonmouselistener ();
Private coolbuttonmouselistener () {
}
Public static coolbuttonmouselistener getInstance () {return listener_;
}
Public void mouseentered (mouseevent e) {
AbstractButton Button = (AbstractButton) E.GETSOURCE ();
IF ((! button.isenabled ()))) {
Return;
}
IF (button.isselected ()) {
Return;
}
IF (Button.getModel (). ispressed ()) {
Button.setBorder (PRESSED_BORDER);
}
Else {
IF (E.GETMODIFIERS ()! = mouseevent.button1_mask) {
Button.setBorder (entered_border);
}
}
}
Public void mouseexited (mouseevent e) {
AbstractButton Button = (AbstractButton) E.GETSOURCE ();
IF ((! button.isenabled ()))) {
Return;
}
IF (button.isselected ()) {
Return;
}
Else {
Button.setBorder (Default_Border);
}
}
Public void mousepressed (mouseevent e) {
AbstractButton Button = (AbstractButton) E.GETSOURCE ();
IF ((! button.isenabled ()))) {
Return;
}
IF (button.isselected ()) {
Return;
}
Button.setBorder (PRESSED_BORDER);
}
Public void mouseeleased (mouseevent e) {
AbstractButton Button = (AbstractButton) E.GETSOURCE ();
IF ((! button.isenabled ()))) {
Return;
}
IF (button.isselected ()) {
Return;
}
Button.setBorder (Default_Border);
}
}
The last step: Write a test class. Generate a jButton, set Border and Listener, add to JFrame, run, finally told! However, don't be too happy, you will find it is still a bit ugly, there is a small blue box around Button, and the background of Button is dark gray. Remove, first override the JButton's ISFOCUSTRAVERSABLE method, let it return false, do not let it get focus, then call Button's setRequestFocusenabled (false) so that there will be blue boxes around the Button. Then then call the Button's setContentareAfilled (false) method, making the mouse not a dark gray background. This is done. The specific code is as follows:
Package com.bhr.ioat.coolbutton;
Import javax.swing.jframe;
Import java.awt.flowLayout;
Import javax.swing.jbutton;
Public class test extends jframe {
Public test () {
GetContentPane (). setLayout (New flowLayout ()); jbutton button = new jbutton ("coolbutton") {
Public boolean isfocustraversable () {
Return False;
}
}
Button.setBorder (CoolbuttonMouseListener.default_border);
Button.addmouselistener (CoolbuttonMouseListener.getInstance ());
Button.SetRequestFocusenabled (false);
Button.SetContentareafilled (false);
GetContentPane (). Add (button);
}
Public static void main (String [] args) {
Jframe frame = new test ();
Frame.setsize (300, 300);
Frame.setVisible (TRUE);
}
}
Here is just to briefly introduce the implementation process. There are still many places that need to be improved. If you can write a Button class inherit jButton, then automatically set the default Border, Listener, etc. when you initialize. In addition, the Border, which is written, can also be used on other Components, such as status bar, and good results.