gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 09:22:54 27 4
gpt4 key购买 nike

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

enter image description here

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

有办法吗?

这是我的代码:

    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;
}

}

最佳答案

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

Example

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

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

和/或调整边距属性...

Example

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

就像一些额外的想法......

关于java - 如何用 ImageIcon 完全填充 JButton 的表面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24397792/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com