我正在 Mac 上使用 Eclipse 设计一个 GUI,我想让 JButton
只显示我设置的图标。它在 Mac 操作系统上看起来不错,但是当我将它导出到 jar 文件并在 Windows 操作系统上运行时,JButton
看起来像这样:
边框都是EmptyBorder
。
我做错了什么?我怎样才能让后面的方 block 消失?
最佳答案
要正确回答这个问题,一个 SSCCE很可能需要。无论如何,我相信你会想要调用 setContentAreaFilled(false)
在您的 JButton
实例上。那应该有效地删除“正方形”。
但是,请务必注意,调用此函数的确切行为因组件和 L&F 的不同而异。
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public final class JButtonDemo {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI(){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JIconButton());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static final class JIconButton extends JButton{
private static final long serialVersionUID = 7274140930080397481L;
public JIconButton(){
super(UIManager.getIcon("OptionPane.informationIcon"));
setContentAreaFilled(false);
setFocusPainted(false);
setBorder(BorderFactory.createEmptyBorder());
}
}
}
关于java - 即使将 Border 设置为 EmptyBorder,JButton 的边框也不会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7660570/