1

有人知道如何删除 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);
}

}

谢谢, 卢博斯

4

2 回答 2

0

好像是画的问题。您可以使用:

button.setBackground( Color.BLACK );
于 2013-04-22T16:07:11.233 回答
0

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. It seems as if Windows L&F shows a rounded edge, but the button is still rectangular. The space between is only noticable if the BG color of the container is changed to something obvious, like black.

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);
  }
}
于 2013-04-22T16:38:36.013 回答