自己做的一个JAVA swing按钮

最近一直做JAVA的GUI设计,由于觉得JAVA提供的一些图形控件不好看,就自己做了一些组件,如按钮,JAVA提供的实在太土板了。

自己做好后,可以配置一下JB,把它放到组件工具栏上去。以后就可以到处用了。


import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;

/**
 * Title:按钮组件
 * Description:
 * Copyright:
 * Company:
 * @author:[email protected]
 * @version 1.0
 */

public class NButton extends JButton implements Serializable
{
    private Border borderOut;
    private Border borderIn;
    private Border borderPressed;
    private Cursor cursor;
    public NButton()
    {
        init();
    }
    /**初始化*/
    private void init()
    {
        borderOut     = BorderFactory.createLineBorder(Color.black);//(2,2,2,2);
        borderIn      = BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.BLUE,Color.BLUE,
                            Color.BLUE, Color.BLUE);
        borderPressed = BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.BLUE,Color.BLUE,
                            Color.BLUE,Color.BLUE);
        cursor = new Cursor(Cursor.HAND_CURSOR);
        this.setBorder( borderOut );
        this.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseEntered(MouseEvent e) {
                thisButton_mouseEntered(e);
            }
            public void mouseExited(MouseEvent e) {
                thisButton_mouseExited(e);
            }
            public void mousePressed(MouseEvent e) {
                thisButton_mousePressed(e);
            }
            public void mouseReleased(MouseEvent e) {
                thisButton_mouseReleased(e);
            }
        });
    }
    /**鼠标移入时的外观*/
    void thisButton_mouseEntered(MouseEvent e){
        if( this.isEnabled() )
            this.setBorder( borderIn );
            this.setCursor(cursor);
    }
    /**鼠标移出时的外观*/
    void thisButton_mouseExited(MouseEvent e){
        this.setBorder( borderOut );
    }
    /**鼠标被点击时的外观*/
    void thisButton_mousePressed(MouseEvent e){
        if( this.isEnabled())
            this.setBorder( borderPressed );
    }
    /**鼠标被释放时的外观*/
    void thisButton_mouseReleased(MouseEvent e)
    {
        if( (this.getBounds().contains(e.getX(), e.getY())) && this.isEnabled() )
            this.setBorder( borderIn );
        else
            this.setBorder( borderOut );
    }
    public static void main(String[] args)
    {
     NButton butt = new ZButton();
     JFrame f = new JFrame();
     f.setBounds(10,10,500,500);
     butt.setBounds(100,20,100,20);
     butt.setText("fdsafd");
     f.getContentPane().setLayout(null);
     f.getContentPane().add(butt);
     f.show();
     }
}

猜你喜欢

转载自blog.csdn.net/bluepb/article/details/469321