java文本区域透明,java - 半透明文本字段 - 堆栈内存溢出

在油漆链中的某个点,你需要控制。

最简单的方法是覆盖文本字段的paint方法并调整图形合成以提供所需的透明度。

这带来了一些额外的问题。 文本字段必须是透明的(opaque == false),否则最终会产生重绘工件。 您还需要手动填充字段的背景,因为将字段设置为透明会停止绘制它自己的背景...

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS8xWTNtei5wbmc=

import java.awt.AlphaComposite;

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.GridBagLayout;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

public class TestTransparentTextField {

public static void main(String[] args) {

new TestTransparentTextField();

}

public TestTransparentTextField() {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {

}

JFrame frame = new JFrame("Testing");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new BorderLayout());

frame.add(new TestPane());

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

});

}

public class TestPane extends JPanel {

private BufferedImage background;

public TestPane() {

setLayout(new GridBagLayout());

add(new TransparentTextField("Look ma, no opacity!", 20));

try {

background = ImageIO.read(new File("/get/your/own/image"));

} catch (IOException ex) {

ex.printStackTrace();

}

}

@Override

public Dimension getPreferredSize() {

return new Dimension(200, 200);

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

if (background != null) {

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

int x = (getWidth() - background.getWidth()) / 2;

int y = (getHeight()- background.getHeight()) / 2;

g2d.drawImage(background, x, y, this);

g2d.dispose();

}

}

}

public class TransparentTextField extends JTextField {

public TransparentTextField(String text) {

super(text);

init();

}

public TransparentTextField(int columns) {

super(columns);

init();

}

public TransparentTextField(String text, int columns) {

super(text, columns);

init();

}

protected void init() {

setOpaque(false);

}

@Override

public void paint(Graphics g) {

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

g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));

super.paint(g2d);

g2d.dispose();

}

@Override

protected void paintComponent(Graphics g) {

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

g2d.setColor(getBackground());

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

super.paintComponent(g2d);

g2d.dispose();

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值