gpt4 book ai didi

java - 输入法去除Swing中JFrame的透明效果

转载 作者:行者123 更新时间:2023-11-30 04:32:12 26 4
gpt4 key购买 nike

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

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

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

提前致谢。

最佳答案

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

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

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

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

如果您不想完全禁用 DirectDraw 渲染,可以通过重写其 paintComponent 方法来写入缓冲区来解决特定的 JTextField 问题,如示例中所示下面。

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

关于java - 输入法去除Swing中JFrame的透明效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14374111/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com