简体   繁体   English

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

[英]How to make JButton with transparent background and regular text?

Is there a way to make a JButton which has a transparent fill(30% transparent) and a text which is not transparent? 有没有办法使JButton具有透明的填充(30%透明)和不透明的文本?

For now I discovered the following options: 现在,我发现了以下选项:

  1. make both the button and the text transparent. 使按钮和文本都透明。
  2. make both of them not transparent. 使它们都不透明。

is there an option in between? 两者之间有选择吗?

Here is one possible implementation using JButton#setIcon(Icon_30%_transparent) : 这是使用JButton#setIcon(Icon_30%_transparent)一种可能的实现:

在此处输入图片说明

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);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM