Swing透明遇到输入法等于白屏

JDK1.7仍然存在~~】

// 最简单的方法:禁止使用DirectDraw

System.setProperty("sun.java2d.noddraw", "true"); // 防止激活输入法时白屏

// 或者重写JTextField的paintComponent()方法:重绘缓存

 

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;

public class Test {

    public static class JTextField2 extends JTextField {
        private static final long serialVersionUID = 1L;
        private BufferedImage buffer = null;

        @Override public void paintComponent(Graphics g) {
            Component window = this.getTopLevelAncestor();
            if (window instanceof Window && !((Window)window).isOpaque()) {
                // This is a translucent window, so we need to draw to a buffer
                // first to work around a bug in the DirectDraw rendering in Swing.
                int w = this.getWidth();
                int h = this.getHeight();
                if (buffer == null || buffer.getWidth() != w || buffer.getHeight() != h) {
                    // Create a new buffer based on the current size.
                    GraphicsConfiguration gc = this.getGraphicsConfiguration();
                    buffer = gc.createCompatibleImage(w, h, BufferedImage.TRANSLUCENT);
                }

                // Use the super class's paintComponent implementation to draw to
                // the buffer, then write that buffer to the original Graphics object.
                Graphics bufferGraphics = buffer.createGraphics();
                try {
                    super.paintComponent(bufferGraphics);
                } finally {
                    bufferGraphics.dispose();
                }
                g.drawImage(buffer, 0, 0, w, h, 0, 0, w, h, null);
            } else {
                // This is not a translucent window, so we can call the super class
                // implementation directly.
                super.paintComponent(g);
            }        
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                final JFrame frame = new JFrame();
                frame.setUndecorated(true);
                frame.setBackground(new Color(96, 128, 160, 192));

                JTextField textField = new JTextField2();                
                JButton exitButton = new JButton("Exit");
                exitButton.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        frame.dispose();
                    }
                });

                frame.add(exitButton, BorderLayout.PAGE_START);
                frame.add(textField, BorderLayout.PAGE_END);

                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}


 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值