Contoh 1 : JXButton Swing Make Over

Rabu, 25 April 2012
Contoh :
Buat java class dengan nama sesuai selera misalnya dengan nama ButtonMakeOver. Sourcenya :


import java.awt.Color;

import java.awt.GradientPaint;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import javax.swing.ButtonModel;

import javax.swing.JButton;

public class ButtonMakeOver extends JButton {

    public ButtonMakeOver(String text) {

   setText(text);

        setBorderPainted(false);

        setContentAreaFilled(false);

        setFocusPainted(false);

        setOpaque(false);

        setForeground(Color.white);

    }

    @Override

    protected void paintComponent(Graphics g) {

        ButtonModel buttonModel = getModel();

        Graphics2D gd = (Graphics2D) g.create();

                gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                gd.setPaint(new   GradientPaint(0,   0,   Color.white,   0, getHeight(), new Color(0, 0, 0, 0)));

        if (buttonModel.isRollover()) {

            gd.setPaint(new GradientPaint(0, 0, new Color(0, 0, 0, 0), 0, getHeight(), Color.white));

            if (buttonModel.isPressed()) {

                gd.setPaint(new GradientPaint(0, 0, Color.white, 0, getHeight(), Color.white));

                setForeground(Color.BLACK);

            } else {

                setForeground(Color.white);

            }

        }

        gd.fillRoundRect(0, 0, getWidth(), getHeight(), 25, 25);

        gd.dispose();

        super.paintComponent(g);

    }

}
Tulis source codeberikut (Test.java) :

import java.awt.BorderLayout;

import java.awt.Color;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Test extends JFrame {

    private ButtonMakeOver button =new ButtonMakeOver("My Button");

    private JPanel panel;

    public Test() {

        panel = new JPanel(null);

        panel.setBackground(Color.BLACK);

        panel.add(button);

        button.setBounds(20, 20, 120, 23);

        setLayout(new BorderLayout());

        add(panel, BorderLayout.CENTER);

        setSize(200,100);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {

        new Test().setVisible(true);

    }

}


Kemudian run project, dan lihat hasilnya.


source : 
http://itc.himatif.or.id
http://eecchhoo.wordpress

0 komentar:

Posting Komentar