【发布时间】:2013-04-22 15:13:01
【问题描述】:
有人知道如何去除 JButton 上的白色矩形边框吗?当按钮有点圆润时,此问题仅适用于 Windows 外观。
请在图片上找到示例。
将边框设置为空或 null 无济于事。保证金也是如此。
只有当我将按钮的不透明度设置为 false 时,白色边距/边框才会消失,但不幸的是,在这种情况下,整个按钮在某些版本的窗口上也是不透明的。
当我将不透明度设置为 false 时,它看起来像:
代码示例:
public class TestFrame extends javax.swing.JFrame {
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
TestFrame inst = new TestFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public TestFrame() {
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setLayout(null);
this.getContentPane().setBackground(Color.BLACK);
JButton button = new JButton();
button.setBounds(10, 10, 100, 50);
button.setBorder(BorderFactory.createEmptyBorder()); // not working
button.setBorder(null); // not working
button.setMargin(new Insets(0,0,0,0)); // not working
add(button);
pack();
setSize(400, 300);
}
}
谢谢, 卢布斯
【问题讨论】:
-
那些 Insets 可能因 Win7 中使用的不同主题而不同(没有人知道用户在您的 PC 中设置),(也许,我希望)与 Java 版本无关,尝试更改 Insets在 UImanager 中,或来自容器(此 JButton 的父级),以获得更好的帮助,尽快发布 SSCCE,简短,可运行,可编译,大约上午问题
-
这发生在所有 Windows PC 上。您是指哪个 UI 管理器属性?我找不到 Button.margin 或类似的东西。
-
@splunebob 请看第二张图片。确实有一些白色边框或边距,问题是我不能只将不透明度设置为 false,因为它不是多平台的。
-
@Lubos:我仍然只看到 1 个按钮。
-
不要发布图片。发布您的 SSCCE,以便我们可以清楚地看到您的代码在做什么。
标签: java swing windows-7 look-and-feel insets