如何使用ImageIcon完全填充JButton的表面? [英] How to fill the surface of the JButton completely with an ImageIcon?

查看:256
本文介绍了如何使用ImageIcon完全填充JButton的表面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

这是我的代码:

    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 ,以便按钮内没有填充。

You need to set the border to EmptyBorder so there is no padding within your button.

添加:

button1.setBorder(BorderFactory.createEmptyBorder());

我的结果:

这篇关于如何使用ImageIcon完全填充JButton的表面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆