[英]Remove JButton rectangle white border
Does somebody know how to remove white rectangle border on JButton ? 有人知道如何删除JButton上的白色矩形边框吗? This issue is there only for windows look & feel when the button is a little bit rounded. 仅当按钮稍微变圆时,才出现此窗口外观的感觉。
Please find attached the example on the image 请在图片上找到随附的示例 . 。
Setting the border to empty or null doesn't help. 将边框设置为空或null无效。 The same for margin. 保证金也一样。
The white margin/border dissapear only when I set the opacity of the button to false, but unfortunately in this case also the whole button is opaque on some versions of windows. 仅当我将按钮的不透明度设置为false时,白色边距/边框才会消失,但是不幸的是,在这种情况下,整个按钮在某些版本的Windows上也是不透明的。
When I set opacity to false, it looks like: 当我将不透明度设置为false时,它看起来像:
Code example: 代码示例:
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);
}
} }
Thanks, Lubos 谢谢,Lubos
Seems like a painting problem. 似乎是绘画问题。 You can use: 您可以使用:
button.setBackground( Color.BLACK );
EDIT: See comments below. 编辑:请参阅下面的评论。 This sample still shows the effect, even using a proper layout. 即使使用适当的布局,该示例仍可以显示效果。 Setting the background color seems to show the unwanted border. 设置背景颜色似乎显示了不需要的边框。 This effect doesn't show with Metal. Metal不会显示此效果。 It seems as if Windows L&F shows a rounded edge, but the button is still rectangular. Windows L&F似乎显示了一个圆角的边缘,但是该按钮仍然是矩形的。 The space between is only noticable if the BG color of the container is changed to something obvious, like black. 仅当容器的BG颜色更改为明显的颜色(例如黑色)时,才可以注意到它们之间的间隔。
import java.awt.*;
import javax.swing.*;
public class TestFrame extends JFrame
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
e.printStackTrace();
}
TestFrame inst = new TestFrame();
inst.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public TestFrame()
{
JButton button = new JButton("One");
JButton button2 = new JButton("Two");
JPanel p = new JPanel();
p.setBackground(Color.BLACK);
p.setLayout(new FlowLayout());
p.add(button);
p.add(button2);
add(p);
setSize(400, 300);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.