JButton 图标有点偏离
JButton Icon a little bit off
我尝试编写一个小的 TicTacToe 程序,并根据哪个玩家标记了该按钮(您知道,传统的十字和圆圈)为我在游戏中使用的按钮提供了一个图标。
现在,当我检查我的按钮时 "in game",图标有点偏离;图标和按钮边框之间有一个小间隙(可能大 10 像素)。
我已经试过了,但没用:
button.setHorizontalAlignement(SwingConstants.RIGHT)
示例代码:
JButton button = new JButton();
button.setPreferredSize(new Dimension(175,175)); //Note: Image is also 175x175
button.addActionListener(new MyOnClickListener());
...
class MyOnClickListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
JButton button = (JButton) e.getSource();
ImageIcon myIcon = new ImageIcon("source");
button.setEnabled(false);
button.setIcon(myIcon);
button.setDisabledIcon(myIcon);
}
}
Screenshot of button
看到右边那个小白边了吗?这是我不想要的。我希望图标完全填满按钮。
这是图标:
Icon
要删除无关的 space,请将按钮的边框设置为 null
。这可能需要(在某些 PLAF 中)更改图标本身的外观以指示焦点、悬停、按下等。
在此屏幕截图中,右侧栏中的中间按钮获得焦点,同时鼠标悬停在底行的中间按钮上。
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.imageio.ImageIO;
import java.net.URL;
public class TicTacToeButtons {
private JComponent ui = null;
private String path = "https://i.stack.imgur.com/sAU9n.png";
private BufferedImage image;
Image transparentImage;
private JButton getButton(int i) {
Image img = (i%2==0 ? image : transparentImage);
JButton b = new JButton(new ImageIcon(img));
b.setBorder(null);
return b;
}
TicTacToeButtons() {
try {
initUI();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void initUI() throws Exception {
if (ui!=null) return;
image = ImageIO.read(new URL(path));
transparentImage = new BufferedImage(
image.getWidth(),image.getHeight(),BufferedImage.TYPE_INT_ARGB);
ui = new JPanel(new GridLayout(3,3));
ui.setBorder(new EmptyBorder(4,4,4,4));
for (int ii=0; ii<9; ii++) {
ui.add(getButton(ii));
}
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
TicTacToeButtons o = new TicTacToeButtons();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
我尝试编写一个小的 TicTacToe 程序,并根据哪个玩家标记了该按钮(您知道,传统的十字和圆圈)为我在游戏中使用的按钮提供了一个图标。
现在,当我检查我的按钮时 "in game",图标有点偏离;图标和按钮边框之间有一个小间隙(可能大 10 像素)。
我已经试过了,但没用:
button.setHorizontalAlignement(SwingConstants.RIGHT)
示例代码:
JButton button = new JButton();
button.setPreferredSize(new Dimension(175,175)); //Note: Image is also 175x175
button.addActionListener(new MyOnClickListener());
...
class MyOnClickListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
JButton button = (JButton) e.getSource();
ImageIcon myIcon = new ImageIcon("source");
button.setEnabled(false);
button.setIcon(myIcon);
button.setDisabledIcon(myIcon);
}
}
Screenshot of button
看到右边那个小白边了吗?这是我不想要的。我希望图标完全填满按钮。 这是图标: Icon
要删除无关的 space,请将按钮的边框设置为 null
。这可能需要(在某些 PLAF 中)更改图标本身的外观以指示焦点、悬停、按下等。
在此屏幕截图中,右侧栏中的中间按钮获得焦点,同时鼠标悬停在底行的中间按钮上。
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.imageio.ImageIO;
import java.net.URL;
public class TicTacToeButtons {
private JComponent ui = null;
private String path = "https://i.stack.imgur.com/sAU9n.png";
private BufferedImage image;
Image transparentImage;
private JButton getButton(int i) {
Image img = (i%2==0 ? image : transparentImage);
JButton b = new JButton(new ImageIcon(img));
b.setBorder(null);
return b;
}
TicTacToeButtons() {
try {
initUI();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void initUI() throws Exception {
if (ui!=null) return;
image = ImageIO.read(new URL(path));
transparentImage = new BufferedImage(
image.getWidth(),image.getHeight(),BufferedImage.TYPE_INT_ARGB);
ui = new JPanel(new GridLayout(3,3));
ui.setBorder(new EmptyBorder(4,4,4,4));
for (int ii=0; ii<9; ii++) {
ui.add(getButton(ii));
}
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
TicTacToeButtons o = new TicTacToeButtons();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}