Ive tried to fill the "surface" of the Jbutton completely with an ImageIcon. My result so far is:
As you can see there is still some space between the edge of the "Exit"-Label and the edge of the button. You can see the button with the white-blue filling at the background. What I want is to cover this button COMPLETELY with the label.
Is there a way to do that?
Here is my code:
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;
}
}
Depending on what you ultimately want to achieve you could, set the contentAreaFilled
property...
button1.setForeground(Color.RED);
button1.setFocusPainted(true);
button1.setContentAreaFilled(false);
button1.setIcon(new ImageIcon(image));
(note, the frame's content area is yellow to highlight the button)
And/or adjust the margins
properties...
button1.setForeground(Color.RED);
button1.setFocusPainted(true);
button1.setContentAreaFilled(false);
button1.setMargin(new Insets(0, 0, 0, 0));
button1.setIcon(new ImageIcon(image));
Just as some additional ideas...
You need to set the border to EmptyBorder
so there is no padding within your button.
add this:
button1.setBorder(BorderFactory.createEmptyBorder());
my result:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.