博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
swing自定义border
阅读量:6935 次
发布时间:2019-06-27

本文共 2359 字,大约阅读时间需要 7 分钟。

public class MyBorder extends AbstractBorder {    private static final long serialVersionUID = 1L;    private int xOff;    private int yOff;    private Insets insets;    public MyBorder(int x, int y) {        this.xOff = x;        this.yOff = y;        this.insets = new Insets(0, 0, this.xOff, this.yOff);    }    @Override    public Insets getBorderInsets(Component c) {        return this.insets;    }    @Override    public void paintBorder(Component c, Graphics g, int x, int y, int width,            int height) {        g.translate(x, y);        BufferedImage rightImage = createBufferedImage(this.xOff, height                - this.yOff, Color.red, 0.5f);        BufferedImage bottomImage = createBufferedImage(width - 2 * this.xOff,                height, Color.green, 0.5f);        g.drawImage(rightImage, width - this.xOff, this.yOff, null);        g.drawImage(bottomImage, this.xOff, height - this.yOff, null);        g.translate(-x, -y);    }    private BufferedImage createBufferedImage(int width, int height,            Color color, float alpha) {        BufferedImage bufferedImage = new BufferedImage(width, height,                BufferedImage.TYPE_INT_BGR);        Graphics2D g2d = bufferedImage.createGraphics();        bufferedImage = g2d.getDeviceConfiguration().createCompatibleImage(                width, height, Transparency.TRANSLUCENT);        g2d.dispose();        g2d = bufferedImage.createGraphics();        g2d.setColor(color);        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,                alpha));        g2d.fillRect(0, 0, width, height);        g2d.dispose();        return bufferedImage;    }    public static void main(String[] args) {        JFrame frame = new JFrame("border");        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        JTextField text = new JTextField("helloKitty");        text.setPreferredSize(new Dimension(100, 30));        JButton button = new JButton("donald duck");        // text.setBorder(new MyBorder(10, 10));        // text.setForeground(Color.yellow);        frame.getContentPane().setLayout(new BorderLayout());        button.setBorder(new MyBorder(5, 5));        frame.getContentPane().add(text, BorderLayout.CENTER);        frame.getContentPane().add(button, BorderLayout.SOUTH);        frame.setSize(300, 300);        frame.setVisible(true);    }}

 

转载地址:http://hdgjl.baihongyu.com/

你可能感兴趣的文章
log4j(七)——log4j.xml简单配置样例说明
查看>>
用express-generator创建express项目骨架
查看>>
Waiting on Groups of Queued Tasks
查看>>
selenium 定制启动 chrome 的选项
查看>>
PHP MySQL Update
查看>>
强大的Vivado IP工具——自定义IP的使用
查看>>
PowerShell让系统可以执行.ps1文件
查看>>
python 在windows 中文显示
查看>>
路由器后面再接一个路由器怎么设置(二级路由)
查看>>
python 调用shell命令的方法
查看>>
.Net转Java.02.数据类型
查看>>
在一个gradle 的maven property 里添加多个URL
查看>>
PHP中的SESSION
查看>>
C#正则表达式的完全匹配、部分匹配及忽略大小写的问题
查看>>
【游戏开发】基于VS2017的OpenGL开发环境搭建
查看>>
洛谷P3960 列队(动态开节点线段树)
查看>>
RESTful到底是什么玩意??
查看>>
PHP的词法解析器:re2c
查看>>
Html5版本的全套股票行情图开源了,附带实现技术简介
查看>>
Our Proof : Page Scraping : Website Data Extraction : Data Mining Analytics : Connotate.com
查看>>