Using Java three steps to realize cool Button

Source: Internet
Author: User
Tags getcolor

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) {}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.