- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 GUI,并且主面板有透明背景,所以人们可以通过它们看到背景图像。问题是,点击在 Mac OS 上,此面板之一上的按钮具有圆形边框,其背景变为不透明颜色。我怎样才能避免这种情况?
这是一张图片:
最佳答案
只需使用setOpaque(false)
使按钮透明即可。永远不要使用 alpha 颜色作为背景颜色,Swing 只处理不透明或不透明的组件,它不知道如何处理基于 alpha 的颜色。
如果您使用基于 alpha 的背景颜色,Swing 不知道 1- 它应该在绘制组件之前正确准备 Graphics
上下文,2- 组件下面的组件也需要当组件更改时更新。
随着 UI 复杂性的增加和变化开始发生(UI 更新),使用 Alpha 背景颜色会生成随机且烦人的绘画伪影
参见Painting in AWT and Swing和 Performing Custom Painting了解更多详情
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private BufferedImage background;
public TestPane() {
JButton btn = new JButton("I'm a transparent button");
btn.setOpaque(false);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(btn, gbc);
add(new JButton("I'm not a transparent button"), gbc);
try {
background = ImageIO.read(new File("C:\\hold\\thumbnails\\issue522.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return background == null ? new Dimension(200, 200) : new Dimension(background.getWidth(), background.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g2d.drawImage(background, x, y, this);
g2d.dispose();
}
}
}
}
如果要绘制半透明背景,则需要重写组件 paintComponent
方法并自行绘制,确保先将组件标记为透明(而不是不透明)
关于java - 丑陋的 Swing 按钮背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26854010/
kable(head(mtcars) %>% kable_styling(bootstrap_options = c("striped", "hover")) 使用 kable 表(见上文)的普通
我试图很好地显示 NSTextView 中突出显示的段落。现在,我通过创建一个带有背景颜色的 NSAttributedString 来做到这一点。这是一些简化的代码: NSDictionary *at
我的站点位于 http://www.idataresearch.net 我正在为导航菜单使用 Dynamic Drive 的 ddmoothmenu 脚本。我真的很喜欢这个脚本,很高兴看到以前的开发人
我在 Ubuntu EC2 实例上运行 nginx、gunicorn、django。整个网站运行良好。除了管理员。管理员无法正常显示。我运行“python manage.py Collectstati
在一个本身嵌套在一个或多个父数组/结构/union 中的数组/结构/union 中读取多个数字/字符串的最佳方法是什么? 没有临时变量的第一个例子: printf("%d %d\n", a[9][3]
我正在编写一个与 mingw 一起使用的包装层,它为应用程序提供了一个虚拟的 UTF-8 环境。处理文件名的函数是从 UTF-8 转换并调用相应的“_w”函数等的包装器。我遇到的最大问题是 Windo
在我的回答中Clojure For Comprehension example我有一个处理自己输出的函数: (defn stream [seed] (defn helper [slow]
我使用的是 bootstrap 4.0 alpha,没有其他样式。布局非常丑陋,日历只显示一列而不是表格。知道为什么以及如何做吗? 我从网站上注意到以下内容,但我不明白我应该怎么做: note: Th
我是一名优秀的程序员,十分优秀!