在 JButton 上放置 ImageIcon 时的边框问题
Border problems when placing an ImageIcon on a JButton
我正在创建如下图像按钮
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Demo {
private JFrame mainFrame;
static public Color BGCOLOUR1 = new Color(240, 240, 240);
public Demo() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
ex.printStackTrace();
}
mainFrame = new JFrame("DEMO");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBackground(BGCOLOUR1);
mainFrame.setSize(300, 300);
mainFrame.setLayout(new BorderLayout());
JPanel smButtonContainer = new JPanel();
smButtonContainer.setLayout(new FlowLayout());
smButtonContainer.setOpaque(true);
smButtonContainer.setBackground(Color.WHITE);
smButtonContainer.setBorder(BorderFactory.createEmptyBorder(25, 10, 5, 0));
ImageIcon emailLogo = new ImageIcon(getClass().getResource("/resources/email.png"));
JButton emailButton = new JButton(emailLogo);
emailButton.setBorder(null);
emailButton.setBorderPainted(false);
emailButton.setBackground(Color.WHITE);
emailButton.setMargin(new Insets(0, 0, 0, 0));
smButtonContainer.add(emailButton);
mainFrame.add(smButtonContainer);
}
public static void main(String[] args) {
Demo demo = new Demo();
demo.showUI();
}
private void showUI() {
mainFrame.setVisible(true);
}
}
在我的开发机器 (Fedora 23 / KDE) 上看起来不错:
但是当我 运行 在 Windows 7 机器上应用时,按钮看起来是凸起的:
为什么会发生这种情况?我该如何预防?
编辑:更新了示例。
您还需要添加:
emailButton.setFocusPainted(false);
在 Windows 上绘制了一个虚线矩形以指示按钮具有焦点。
我正在创建如下图像按钮
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Demo {
private JFrame mainFrame;
static public Color BGCOLOUR1 = new Color(240, 240, 240);
public Demo() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
ex.printStackTrace();
}
mainFrame = new JFrame("DEMO");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBackground(BGCOLOUR1);
mainFrame.setSize(300, 300);
mainFrame.setLayout(new BorderLayout());
JPanel smButtonContainer = new JPanel();
smButtonContainer.setLayout(new FlowLayout());
smButtonContainer.setOpaque(true);
smButtonContainer.setBackground(Color.WHITE);
smButtonContainer.setBorder(BorderFactory.createEmptyBorder(25, 10, 5, 0));
ImageIcon emailLogo = new ImageIcon(getClass().getResource("/resources/email.png"));
JButton emailButton = new JButton(emailLogo);
emailButton.setBorder(null);
emailButton.setBorderPainted(false);
emailButton.setBackground(Color.WHITE);
emailButton.setMargin(new Insets(0, 0, 0, 0));
smButtonContainer.add(emailButton);
mainFrame.add(smButtonContainer);
}
public static void main(String[] args) {
Demo demo = new Demo();
demo.showUI();
}
private void showUI() {
mainFrame.setVisible(true);
}
}
在我的开发机器 (Fedora 23 / KDE) 上看起来不错:
但是当我 运行 在 Windows 7 机器上应用时,按钮看起来是凸起的:
为什么会发生这种情况?我该如何预防?
编辑:更新了示例。
您还需要添加:
emailButton.setFocusPainted(false);
在 Windows 上绘制了一个虚线矩形以指示按钮具有焦点。