use*_*542 2 java swing label bufferedimage jbutton
我尝试用ImageIcon完全填充Jbutton的“表面”。到目前为止,我的结果是:

如您所见,“退出”标签的边缘与按钮的边缘之间仍然存在一些空间。您可以在背景上看到带有白色蓝色填充的按钮。我想要的是用标签完全覆盖此按钮。
有没有办法做到这一点?
这是我的代码:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
根据最终要实现的目标,设置contentAreaFilled属性...

button1.setForeground(Color.RED);
button1.setFocusPainted(true);
button1.setContentAreaFilled(false);
button1.setIcon(new ImageIcon(image));
Run Code Online (Sandbox Code Playgroud)
(请注意,框架的内容区域为黄色以突出显示按钮)
和/或调整margins属性...

button1.setForeground(Color.RED);
button1.setFocusPainted(true);
button1.setContentAreaFilled(false);
button1.setMargin(new Insets(0, 0, 0, 0));
button1.setIcon(new ImageIcon(image));
Run Code Online (Sandbox Code Playgroud)
就像其他一些想法一样...