如何使用ImageIcon完全填充JButton的表面?

我试图用ImageIcon完全填充Jbutton的“表面”。 到目前为止我的结果是:

在此处输入图像描述

正如您所看到的,“Exit”-Label的边缘和按钮的边缘之间仍然存在一些空间。 您可以在背景中看到带有白蓝色填充的按钮。 我想要的是完全覆盖这个按钮与标签。

有没有办法做到这一点?

这是我的代码:

package footballQuestioner; import java.awt.AlphaComposite; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Graphics2D; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Insets; import java.awt.RenderingHints; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Beiexamples { public static void main(String[] args) { JFrame frame = new MyFrame(); } } class MyFrame extends JFrame { BufferedImage image; JLabel label; public MyFrame() { setLayout(new GridBagLayout()); JPanel panel=new JPanel(new BorderLayout()); Font font = new Font("Rockwell Extra Bold", Font.PLAIN, 25); JButton button1=new JButton(); image=getBufferedImage("footballQuestioner/ExitButtonLabel.png"); image=getScaledImage(250, 100, image); label=new JLabel(new ImageIcon(image)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(750,300); button1.setForeground(Color.YELLOW); // button1.setBackground(new Color(0,100,0)); button1.setFocusPainted(true); button1.setIcon(new ImageIcon(image)); button1.setBorder(BorderFactory.createEmptyBorder()); add(button1); pack(); centeringWindow(); setVisible(true); } public void centeringWindow() { Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); int x; int y; x = (int) (dimension.getWidth() - getWidth()) / 2; y = (int) (dimension.getHeight() - getHeight()) / 2; setLocation(x, y); } public BufferedImage getBufferedImage(String pathName) { try { image = ImageIO.read(Beispielfenster.class.getClassLoader() .getResourceAsStream(pathName)); } catch (IOException e) { e.printStackTrace(); } return image; } public BufferedImage getScaledImage(int width, int height, BufferedImage orginalImage) { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = image.createGraphics(); // 2. Use the Graphic object to draw a new image to the image in the // buffer g.setComposite(AlphaComposite.Clear); g.fillRect(0, 0, width, height); g.setComposite(AlphaComposite.SrcOver); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.drawImage(orginalImage, 0, 0, width, height, null); g.dispose(); return image; // Image image=orginalImage.getImage(); // Image newImage=image.getScaledInstance(width,height, // Image.SCALE_SMOOTH); // ImageIcon imageIcon=new ImageIcon(newImage); // // return imageIcon; } } 

您需要将边框设置为EmptyBorder以便按钮内没有填充。

添加这个:

 button1.setBorder(BorderFactory.createEmptyBorder()); 

我的结果:

在此处输入图像描述

根据您最终想要实现的目标,设置contentAreaFilled属性…

例

 button1.setForeground(Color.RED); button1.setFocusPainted(true); button1.setContentAreaFilled(false); button1.setIcon(new ImageIcon(image)); 

(注意,框架的内容区域为黄色以突出显示按钮)

和/或调整margins属性……

例

 button1.setForeground(Color.RED); button1.setFocusPainted(true); button1.setContentAreaFilled(false); button1.setMargin(new Insets(0, 0, 0, 0)); button1.setIcon(new ImageIcon(image)); 

正如一些额外的想法……