wang1421769505
2021-04-17 18:57
采纳率: 50%
浏览 134

如何在java窗口中添加半透明显示文本框

我图形界面开发用的是swing,想在所显示的图片下半部分显示一个半透明的文本框,然后在里面显示多行文字,SWING能做到吗?求求大家帮帮我!!!!!!

(图片随便找的,别在意)

就类似于这种,半透明,只显示一部分,不占据整个窗口,在其中显示文字。

 

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • allway2 2021-04-17 20:24

    package sample;
    
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import java.io.*;
    
    import javax.imageio.*;
    import javax.swing.*;
    
    public class TransparentImage2 extends JPanel {
    	private BufferedImage backImage, frontImage;
    	private float alpha = 1;
    
    	public TransparentImage2() {
    		try {
    			backImage = ImageIO.read(new File("C:\\1618576427308.png"));
    
    			String text = "中文测试";
    			BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    			Graphics2D graphics2d = image.createGraphics();
    			Font font = new Font("TimesNewRoman", Font.BOLD, 24);
    			graphics2d.setFont(font);
    			FontMetrics fontmetrics = graphics2d.getFontMetrics();
    			int width = fontmetrics.stringWidth(text);
    			int height = fontmetrics.getHeight();
    			graphics2d.dispose();
    
    			image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    			graphics2d = image.createGraphics();
    			graphics2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
    					RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    			graphics2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    			graphics2d.setFont(font);
    			fontmetrics = graphics2d.getFontMetrics();
    			graphics2d.setColor(Color.BLACK);
    			graphics2d.drawString(text, 0, fontmetrics.getAscent());
    			graphics2d.dispose();
    
    			frontImage = image;
    
    		} catch (Exception e) {
    			System.out.println(e);
    		}
    	}
    
    	@Override
    	public Dimension getPreferredSize() {
    		return new Dimension(backImage.getWidth(), backImage.getHeight());
    	}
    
    	public void setAlpha(float alpha) {
    		this.alpha = alpha;
    		repaint();
    	}
    
    	@Override
    	protected void paintComponent(Graphics g) {
    		super.paintComponent(g);
    
    		Graphics2D g2 = (Graphics2D) g;
    		int x = (getWidth() - backImage.getWidth()) / 2;
    		int y = (getHeight() - backImage.getHeight()) / 2;
    		g2.drawRenderedImage(backImage, AffineTransform.getTranslateInstance(x, y));
    
    		Composite old = g2.getComposite();
    		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
    		x = (getWidth() - frontImage.getWidth()) / 10;
    		y = (getHeight() - frontImage.getHeight()) / 10;
    		g2.drawRenderedImage(frontImage, AffineTransform.getTranslateInstance(x, y));
    		g2.setComposite(old);
    	}
    
    	private static void createAndShowUI() {
    		final TransparentImage2 app = new TransparentImage2();
    		app.setAlpha(0.3f);
    
    		JFrame frame = new JFrame("Transparent Image");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add(app);
    
    		frame.setLocationByPlatform(true);
    		frame.pack();
    		frame.setVisible(true);
    	}
    
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				createAndShowUI();
    			}
    		});
    	}
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题