首页 > 怎么修改JButton的样式?

怎么修改JButton的样式?

我想自己修改JButton的样式,按钮里面有图片、文本标签等,怎么才能随心所欲的修改呢?
下面图片里面灰色的区域就是我想要的按钮样式,请问大家我得用什么思路才做呢?
谢谢


别用swing了。。


自己回答了,这是我的代码,希望给遇到相同问题的人以帮助。

package youcanyoubb;

import javax.swing.*;
import java.awt.*;

/**
 * Created by mcxinyu on 2015/12/8.
 */ //主页功能按钮
class HomeFunctionBtn extends JButton {
    JTextArea textJTA;
    JPanel btnArea;
    JButton button;
    //参数:图片路径,按钮名称,说明文本,坐标x,坐标y。
    public HomeFunctionBtn(String image,String btnName,String text,int x,int y){
        //设置按钮里面图片与标签的位置垂直居中.
        button = new JButton(btnName,new ImageIcon(image));
        button.setBackground(new Color(230,230,230));
        button.setSize(198,153);
        button.setFocusPainted(false);
        button.setFont(new Font("微软雅黑",Font.BOLD,18));
        button.setVerticalAlignment(SwingConstants.BOTTOM);
        button.setVerticalTextPosition(SwingConstants.BOTTOM);
        button.setHorizontalAlignment(SwingConstants.CENTER);
        button.setHorizontalTextPosition(SwingConstants.CENTER);
        //设置说明文本的内容及显示.
        textJTA= new JTextArea(text);
        textJTA.setFont(new Font("微软雅黑",Font.PLAIN,13));
        textJTA.setBounds(0,153,200,47);
        textJTA.setLineWrap(true);
        textJTA.setEditable(false);
        textJTA.setBackground(new Color(230,230,230));
        //添加组件
        btnArea = new JPanel(null);//new FlowLayout()??
        btnArea.add(button);
        btnArea.add(textJTA);
        this.setBorder(null);
        this.setLocation(x,y);
        this.add(btnArea);
        this.setSize(200,200);
    }
}

继承JButton,重写其中的绘制方法。方法名我不记得了,去翻一下JDK吧。

【热门文章】
【热门文章】