java文本区域透明,如何使JButton具有透明背景和常规文本?

Is there a way to make a JButton which has a transparent fill(30% transparent) and a text which is not transparent?

For now I discovered the following options:

make both the button and the text transparent.

make both of them not transparent.

is there an option in between?

解决方案

Here is one possible implementation using JButton#setIcon(Icon_30%_transparent):

zv1E1.png

import java.awt.*;

import java.awt.geom.*;

import java.awt.image.*;

import javax.swing.*;

public final class TranslucentButtonIconTest {

private static final TexturePaint TEXTURE = makeCheckerTexture();

public JComponent makeUI() {

JPanel p = new JPanel() {

@Override protected void paintComponent(Graphics g) {

super.paintComponent(g);

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

g2.setPaint(TEXTURE);

g2.fillRect(0, 0, getWidth(), getHeight());

g2.dispose();

}

};

p.add(makeButton("aaa"));

p.add(makeButton("bbbbbbb"));

p.add(makeButton("ccccccccccc"));

p.add(makeButton("ddddddddddddddddddddddddddddd"));

return p;

}

private static AbstractButton makeButton(String title) {

return new JButton(title) {

@Override public void updateUI() {

super.updateUI();

setVerticalAlignment(SwingConstants.CENTER);

setVerticalTextPosition(SwingConstants.CENTER);

setHorizontalAlignment(SwingConstants.CENTER);

setHorizontalTextPosition(SwingConstants.CENTER);

setBorder(BorderFactory.createEmptyBorder(2, 8, 2, 8));

setMargin(new Insets(2, 8, 2, 8));

setContentAreaFilled(false);

setFocusPainted(false);

setOpaque(false);

setForeground(Color.WHITE);

setIcon(new TranslucentButtonIcon());

}

};

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

@Override public void run() {

createAndShowGUI();

}

});

}

public static void createAndShowGUI() {

JFrame f = new JFrame();

f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

f.getContentPane().add(new TranslucentButtonIconTest().makeUI());

f.setSize(320, 240);

f.setLocationRelativeTo(null);

f.setVisible(true);

}

private static TexturePaint makeCheckerTexture() {

int cs = 6;

int sz = cs * cs;

BufferedImage img = new BufferedImage(sz, sz, BufferedImage.TYPE_INT_ARGB);

Graphics2D g2 = img.createGraphics();

g2.setPaint(new Color(120, 120, 220));

g2.fillRect(0, 0, sz, sz);

g2.setPaint(new Color(200, 200, 200, 20));

for (int i = 0; i * cs < sz; i++) {

for (int j = 0; j * cs < sz; j++) {

if ((i + j) % 2 == 0) {

g2.fillRect(i * cs, j * cs, cs, cs);

}

}

}

g2.dispose();

return new TexturePaint(img, new Rectangle(0, 0, sz, sz));

}

}

class TranslucentButtonIcon implements Icon {

private static final int R = 8;

private int width;

private int height;

@Override public void paintIcon(Component c, Graphics g, int x, int y) {

if (c instanceof AbstractButton) {

AbstractButton b = (AbstractButton) c;

Insets i = b.getMargin();

int w = c.getWidth();

int h = c.getHeight();

width = w - i.left - i.right;

height = h - i.top - i.bottom;

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

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

Shape area = new RoundRectangle2D.Float(x - i.left, y - i.top, w - 1, h - 1, R, R);

Color color = new Color(0f, 0f, 0f, .3f);

ButtonModel m = b.getModel();

if (m.isPressed()) {

color = new Color(0f, 0f, 0f, .3f);

} else if (m.isRollover()) {

color = new Color(1f, 1f, 1f, .3f);

}

g2.setPaint(color);

g2.fill(area);

g2.setPaint(Color.WHITE);

g2.draw(area);

g2.dispose();

}

}

@Override public int getIconWidth() {

return Math.max(width, 100);

}

@Override public int getIconHeight() {

return Math.max(height, 24);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值