简体   繁体   中英

How to fit ImageIcon within JButton borders

I want to color some JButtons, some other questions here showed me, that it would be easier to paint a little image (did it with gimp) and set it as icon for the JButton.

The number and size of the buttons should be variable (they're in a grid), so I want a high res image that I can scale how i need it.

The problem now is, I don't know how to 'cut the edges' of the icon, because the buttons have rounded edges.

Pic不在按钮的边框内

Here you can see that the image is not inside of the button border.

And here is my method in the class that extends JButton.

public void setYellow() {
    URL u = getClass().getResource("/img/yellow.png");
    ImageIcon i = new javax.swing.ImageIcon(u);
    //Image img = i.getImage();
    //img = img.getScaledInstance(size, size, java.awt.Image.SCALE_SMOOTH); 
    //i = new ImageIcon(img);
    setIcon(i);
}

EDIT

package test;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import control.Control;
import view.Field;
import view.View;

public class HelloWorldSwing {

  /**
     * @param args
     */
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                TestView.initialize();
            }
        });
    }    

}

class TestView {
private static TestView view = new TestView();
public static TestView getView() {
    return view;
}

private TestView() {
    JFrame frame = new JFrame("HelloWorldSwing");
    frame.setLayout(new GridLayout(0,3));
    int buttonSize = 40;

    frame.getContentPane().add(new MyButton(buttonSize));
    frame.getContentPane().add(new MyButton(buttonSize));
    frame.getContentPane().add(new MyButton(buttonSize));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

public static void initialize() {
}
}

class MyButton extends JButton {

int size;
public MyButton(int size) {
    this.size = size;
    setPreferredSize(new Dimension(size, size));
    this.addActionListener(new ButtonHandler());
    setBorder(LineBorder.createGrayLineBorder());
    setOpaque(true);
}

public void setYellow() {
    //URL u = getClass().getResource("/img/test.png"); // 64x64 png pic
        URL u1 = null;
    try {
        u1 = new URL("http://assets1.qypecdn.net/uploads/users/0195/7210" 
                       + "/calvin_yellow_original_thumb.jpg");         
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ImageIcon i = new javax.swing.ImageIcon(u1);
//      Image img = i.getImage();
//      img = img.getScaledInstance(size, size, java.awt.Image.SCALE_SMOOTH); 
//      i = new ImageIcon(img);
    setIcon(i);
//      setBorderPainted(false);
//      setContentAreaFilled(false); did not help
}
}

class ButtonHandler implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
    MyButton mb = (MyButton) e.getSource();
    mb.setYellow();
}
}

EDIT 2

Here are the pictures where the lines in setYellow()

setBorderPainted(false);
setContentAreaFilled(false);

are not commented out (unfortunately there is no difference)

Before button is clicked:

在此输入图像描述

After button is clicked:

在此输入图像描述

UPDATE

I added Borders to the MyButton constructor

setBorder(LineBorder.createGrayLineBorder());

and now the icons are inside the button borders. I added pictures.

在此输入图像描述

在此输入图像描述

But as you can see, we don't have these rounded button edges anymore.

button.setBorderPainted(false);
button.setContentAreaFilled(false);

As seen in this answer .

Update

I do not quite get what you are trying to achieve if not this.

12个黄色按钮带橙色翻转图标

I made the rollover icon to be orange so that we could easily see the size of one button, but otherwise I put 4 in a row to ensure the minimum frame width did not insert extra space between the buttons in a row.

import java.awt.*;
import java.awt.Image;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class HelloWorldSwing {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                TestView.getView();
            }
        });
    }    
}

class TestView {

    private static TestView view = new TestView();

    public static TestView getView() {
        return view;
    }

    private TestView() {
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setLayout(new GridLayout(3,4));
        int buttonSize = 40;

        for (int i=0; i<12; i++) {
            frame.getContentPane().add(new MyButton(buttonSize));
        }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void initialize() {
    }
}

class MyButton extends JButton {

    int size;

    public MyButton(int size) {
        this.size = size;
        setPreferredSize(new Dimension(size, size));
        this.addActionListener(new ButtonHandler());
        setOpaque(true);
        setYellow();
    }

    public Image getImage(int sz, Color color) {
        BufferedImage bi = new BufferedImage(sz,sz,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.setColor(color);
        g.fillRect(0, 0, sz, sz);

        g.dispose();
        return bi; 
    }

    public void setYellow() {
        Image img = getImage(64, Color.YELLOW).getScaledInstance(size, size, java.awt.Image.SCALE_SMOOTH); 
        setIcon(new ImageIcon(img));
        Image rollover = getImage(64, Color.ORANGE).getScaledInstance(size, size, java.awt.Image.SCALE_SMOOTH);
        setRolloverIcon(new ImageIcon(rollover));
        setBorderPainted(false);
        setContentAreaFilled(false); 
    }
}

class ButtonHandler implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        MyButton mb = (MyButton) e.getSource();
        mb.setYellow();
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM