输入法从Swing中的JFrame中删除透明效果

时间:2013-01-17 07:27:51

标签: java swing ime

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

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

但是当我使用输入法在框中键入一些中文字符时,会自动删除透明效果。我的代码有什么问题吗?

提前致谢。

1 个答案:

答案 0 :(得分: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);
            }
        });
    }
}