1
import javax.swing.JFrame;
import javax.swing.JTextField;

import com.sun.awt.AWTUtilities;

@SuppressWarnings("serial")
public class TranslucentIssueTest extends JFrame
{
    public static void main(String[] args)
    {
        (new TranslucentIssueTest()).setVisible(true);
    }

    public TranslucentIssueTest()
    {
        super();
        setUndecorated(true);
        setLayout(null);
        setSize(300, 300);
        AWTUtilities.setWindowOpaque(this, false);

        JTextField box = new JTextField();
        box.setBounds(30, 150, 100, 25);
        add(box);
    }
}

上面的代码在透明框架上创建了一个文本字段。

但是当我使用输入法在框中输入一些汉字时,透明效果自动消失了。我的代码有什么问题吗?

提前致谢。

4

1 回答 1

2

我遇到了似乎相同的问题,并认为我会为在搜索时发现此问题的任何人发布我的解决方案。该问题似乎是由于 Swing 中 DirectDraw 管道中的错误所致。我使用的是较旧版本的 Java 7(更新 5),因此这可能已在更高版本之一中得到修复。

问题似乎不在于 IME,而在于 IME 处于活动状态时由文本字段进行的渲染调用。

解决此问题的最简单方法是在运行任何 GUI 代码之前通过进行以下调用来禁用 DirectDraw 渲染:

System.setProperty("sun.java2d.noddraw", "true");

如果您不想完全禁用 DirectDraw 渲染,则可以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);
            }
        });
    }
}
于 2013-04-07T19:16:19.943 回答