Java 删除JButton矩形白色边框

Java 删除JButton矩形白色边框,java,swing,windows-7,look-and-feel,insets,Java,Swing,Windows 7,Look And Feel,Insets,有人知道如何删除JButton上的白色矩形边框吗?此问题仅适用于按钮稍微圆一点时的windows外观 请在图片上找到附件中的示例 将边框设置为空或null没有帮助。保证金也一样 只有当我将按钮的不透明度设置为false时,白边/边框才会消失,但不幸的是,在这种情况下,在某些版本的windows上,整个按钮也是不透明的 当我将“不透明度”设置为false时,它看起来像: 代码示例: public class TestFrame extends javax.swing.JFrame { /**

有人知道如何删除JButton上的白色矩形边框吗?此问题仅适用于按钮稍微圆一点时的windows外观

请在图片上找到附件中的示例

将边框设置为空或null没有帮助。保证金也一样

只有当我将按钮的不透明度设置为false时,白边/边框才会消失,但不幸的是,在这种情况下,在某些版本的windows上,整个按钮也是不透明的

当我将“不透明度”设置为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);
}
}

谢谢,
卢布斯似乎是个绘画问题。您可以使用:

button.setBackground( Color.BLACK );
编辑:见下面的评论。此示例仍然显示了效果,即使使用了正确的布局。设置背景色似乎会显示不需要的边框。这种效果在金属上看不出来。Windows L&F似乎显示了一条圆形边缘,但按钮仍然是矩形的。只有当容器的背景颜色更改为明显的颜色(如黑色)时,才会注意到之间的间距

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

Win7中使用了不同的主题,这些插入可能会有所不同。没有人知道用户在您的PC中的设置,也许,我希望Java版本与此无关,请尝试在UImanager中更改插入,或者来自此JButton的容器父级,以便更快地发布一个简短、可运行、可编译的,就在上午一点左右,所有windows PC上都会发生这种情况。您指的是哪个UI manager属性?我找不到Button.margin或类似的东西。@splungebob请看第二张图片。确实有一些白色边框或边距,问题是我不能将“不透明度”设置为false,因为它不是多平台的。@Lubos:我仍然只看到1个按钮。不要发布图像。发布你的SSCCE,这样我们可以清楚地看到你的代码在做什么。是的,你是对的。看起来像是油漆问题。对于你的解决方案,这是可行的,但有点困难:我现在需要找到一种方法来解决它,例如,当我使用梯度作为背景时…+1用于直接的方法。卢布斯:另请参阅相关内容。-1这并不能解决我的Win XP机器上的问题。它隐藏了大部分,但现在看起来有点邋遢,底部的一些边框仍在窥视。你意识到你正在将按钮直接添加到框架中吗?-frame.add方法将组件添加到内容窗格。内容窗格是一个JPanel。这是一个SSCCE,所以这不是一个大问题。一个更大的问题是使用空布局。@camickr:true,但它确实会在视觉上产生影响。OP的代码为框架使用了空布局,添加了一个按钮,并显示了一个边框。我的更改以相同的方式构造了按钮,将其添加到具有空布局的面板,而不是具有空布局的框架,然后使用默认布局将该面板添加到框架。在本例中,按钮放置在何处并不重要。这只是一个测试示例。无论按钮放置在何处,问题都是相同的。这仅在ContentPane中引起,1像素处有白色边框。我的示例删除了边框,但我忽略了将BG颜色设置为黑色。这似乎是罪魁祸首。我编辑了我的帖子,使用了一个合适的布局管理器,但仍然产生了边界效果。只需对挫折进行注释/取消注释。。。打电话看看有什么不同。