- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在 JPanel 中放置了一个 JTextArea。这个 JPanel 在背景上有一张图片,而 JTextArea 是半透明的(半透明红色)以显示背景。我不希望用户能够编辑或选择文本,我希望它充当 JLabel(但具有多行并且易于自动换行和调整屏幕大小)。
我尝试了所有这些选项:
text.setEditable(false);
text.setFocusable(false);
text.setEnabled(false);
text.setHighlighter(null);
但是当用户将鼠标拖到 JTextArea 上时颜色仍然会发生一些变化。有谁知道这是怎么回事吗?
最佳答案
您不能简单地将组件的背景色设置为“透明”并期望 Swing 对其进行处理。您需要将组件标记为透明 (setOpaque(false)
),只有这样 Swing 的重绘管理器才会知道它必须更新其下的组件。
这就引出了如何绘制背景的问题(因为 Swing 只有完全不透明或完全透明的概念)。
为此,您需要提供自己的绘制例程(覆盖 paintComponent
、填充背景、更新组件)...这本质上就是 Rob Camick's解决方案正在做,它只是为您提供了一个很好的包装器组件......
下面是使用 JLabel
和 JTextArea
的示例,这两个示例都更新为支持“半透明”...
使用JLabel
使用JTextArea
现在,使用 Rob 的包装器类实现起来要容易得多,但这让您了解出了什么问题以及需要采取什么措施来修复它。
public class MultiLineLabel {
public static void main(String[] args) {
new MultiLineLabel();
}
public MultiLineLabel() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new BackgroundPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TransclucentLabel extends JLabel {
public TransclucentLabel(String text) {
super(text);
setVerticalAlignment(TOP);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Insets insets = getInsets();
int x = insets.left;
int y = insets.top;
int width = getWidth() - (insets.left + insets.right);
int height = getHeight() - (insets.top + insets.bottom);
g2d.setColor(new Color(255, 0, 0, 128));
g2d.fillRect(x, y, width, height);
super.paintComponent(g);
}
}
public class TransclucentTextArea extends JTextArea {
public TransclucentTextArea(String text) {
super(text);
setOpaque(false);
setLineWrap(true);
setWrapStyleWord(true);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Insets insets = getInsets();
int x = insets.left;
int y = insets.top;
int width = getWidth() - (insets.left + insets.right);
int height = getHeight() - (insets.top + insets.bottom);
g2d.setColor(new Color(255, 0, 0, 128));
g2d.fillRect(x, y, width, height);
super.paintComponent(g);
}
}
public class BackgroundPane extends JPanel {
private BufferedImage background;
public BackgroundPane() {
setLayout(new BorderLayout());
// addLabel();
addTextArea();
setBorder(new EmptyBorder(24, 24, 24, 24));
try {
background = ImageIO.read(new File("/path/to/your/image"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
protected void addTextArea() {
StringBuilder sb = new StringBuilder(128);
sb.append("I put a JTextArea in a JPanel. This JPanel has a picture on the background, and the JTextArea is translucent (translucid red) to show the background through. I don't want the user to be able to edit or select the text, I want it to act just as a JLabel (but with multiple lines and easy to word wrap and adjust to screen resize).\n\n");
sb.append("I tried all these options:\n\n");
sb.append("text.setEditable(false);\n");
sb.append("text.setFocusable(false);\n");
sb.append("text.setEnabled(false);\n");
sb.append("text.setHighlighter(null);\n\n");
sb.append("but still some change of color happens as the user drags the mouse over the JTextArea. Anyone knows what is going on?\n");
add(new TransclucentTextArea(sb.toString()));
}
protected void addLabel() {
StringBuilder sb = new StringBuilder(128);
sb.append("<html>");
sb.append("<p>I put a JTextArea in a JPanel. This JPanel has a picture on the background, and the JTextArea is translucent (translucid red) to show the background through. I don't want the user to be able to edit or select the text, I want it to act just as a JLabel (but with multiple lines and easy to word wrap and adjust to screen resize).</p><br>");
sb.append("<p>I tried all these options:</p><br>");
sb.append("<p>text.setEditable(false);<br>");
sb.append("text.setFocusable(false);<br>");
sb.append("text.setEnabled(false);<br>");
sb.append("text.setHighlighter(null);</p><br>");
sb.append("<p>but still some change of color happens as the user drags the mouse over the JTextArea. Anyone knows what is going on?</p>");
add(new TransclucentLabel(sb.toString()));
}
@Override
public Dimension getPreferredSize() {
return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g.drawImage(background, x, y, this);
}
}
}
}
关于java - JTextArea 不可选择,但仍显示 "ghost"光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14329310/
Ghost 是否存在与 Wordpress 的“帖子类型”和高级自定义字段插件等价的插件? 作为一个例子,我建立了一个旅游网站,它有多个旅行(一种自定义帖子类型),每次旅行都有各种序列化的行程目的地、
我正在尝试使用 Ghost 博客中的“获取帮助程序”按标签获取相关帖子的列表。 我尝试遵循cookbook in Ghost docs按标签获取相关帖子,但似乎某些语法略有变化(我知道这是测试版功能!
我正在尝试使用 Ghost 博客中的“获取帮助程序”按标签获取相关帖子的列表。 我尝试遵循cookbook in Ghost docs按标签获取相关帖子,但似乎某些语法略有变化(我知道这是测试版功能!
有没有什么方法可以使用 api 和 get 助手获取 Ghost 中的所有内部标签? 在我的 post.hbs 模板中,我可以这样做并且有效: {{#post}} {{#foreach tags
是否有机会运行 Ghost blogging platform 的单个安装为可在许多不同的顶级域或子域下访问的许多博客提供服务? 我尝试过browse source code , Ghost's fo
我正在为 https://ghost.org/ 在本地开发一个主题 我遇到了很多问题。 但是没有日志。我不知道为什么会失败。 是否有任何选项可以在开发时显示日志一个https://ghost.org/
我正在为 https://ghost.org/ 在本地开发一个主题 我遇到了很多问题。 但是没有日志。我不知道为什么会失败。 是否有任何选项可以在开发时显示日志一个https://ghost.org/
在最新版本中,您允许我们在 post slug 中添加日期。是否可以只添加年和月?甚至通过编辑代码?因为我正在尝试从 Wordpress 迁移,所以我需要它。在 WP 中,我使用该格式。 注意:我使用
我在 Windows 10 上安装了 ghost 和 ghost-CLI。当我运行时 ghost start我收到以下错误。如何解决这个问题?它似乎与检查文件和文件夹权限的命令有关。请注意,我在 D:
我正在将 Ghost 平台安装到 Ubuntu 20.4 上。但是,存在阻止应用程序启动的身份验证模式错误。 有谁知道这是要求修复什么?该系统符合技术先决条件。我也大部分时间使用 Virtualmin
我有一台 CentOS 7 机器,带有 mysql 服务器和 nginx。我使用 yum install Nodejs 和 Ghost-cli 安装了 NodeJS。 我想用 Ghost-cli 安装
我使用 Ghost 作为博客平台,使用 nginx 作为 ghost 的反向代理,详见文档。 Ghost 安装在一个子目录中,并在域 http://example.com/blog 上提供服务而静态网
使用 Ghost 博客 routes.yaml 文件,可以使用 collections block 来创建由某些标签和/或其他数据组成的自定义集合。您还可以告诉此集合使用自定义主题模板,请参阅: ht
每当我在本地系统中运行脚本时,游标都能正常工作,当我在docker中运行时,我获取了一个错误,所以任何人都会告诉我哪里出了问题,或者这个问题是不是包端的问题。。当我在当地跑的时候,我无头:假,当时无头
我刚开始使用Jetpack Compose,我有这个问题。我有一个LazyVerticalGrid,它是从FireStore数据库中获取的列表填充的,当我通过应用程序删除一个项目时,视图会更新,删除的
在安装Win7系统的时候,有非常多的版本可以选择,不少用户都会看花眼,经常出现的是ghost win7这个版本,ghost win7是什么意思?ghost win7和win7原版有什么区别呢?下面小
通常,BIOS损坏的电脑是完全无法启动引导的,用户没办法自行恢复,但在具备GHOST BIOS 功能的主板上,系统会在EP1308芯片的引导下进行“EPoX GHOST BIOS Recovery
我无法让我的自定义错误页面正常工作。我已按照 http://docs.ghost.org/themes/ 上的说明进行操作并在我的主题根目录中添加了一个 error.hbs 模板。这个模板中的代码非常
运行此命令时出现错误: npm install --production 请帮忙。 npm ERR! install Couldn't read dependencies npm ERR! packa
我已成功安装新的博客平台Ghost在我的 MediaTemple VPS 服务器上。它工作正常,但我在配置端口号时遇到问题。您会看到,我在 VPS 上托管了大约 10 个域,因此我必须始终启用 Apa
我是一名优秀的程序员,十分优秀!