如何在JButton边框内适应ImageIcon

时间:2012-11-03 13:59:19

标签: java swing icons border jbutton

我想给一些JButton上色,这里有一些其他问题告诉我,它会更容易 画一个小图像(用gimp做)并将其设置为JButton的图标。

按钮的数量和大小应该是可变的(它们在网格中),所以我想要一个高分辨率的图像,我可以根据需要进行缩放。

现在的问题是,我不知道如何'切割图标的边缘',因为按钮有圆角。

Pic is not inside the borders of the button

您可以在此处看到图像不在按钮边框内。

这是我在类中扩展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);
}

修改

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

编辑2

以下是setYellow()

中的行的图片
setBorderPainted(false);
setContentAreaFilled(false);

未被注释掉(遗憾的是没有区别)

点击按钮前:

enter image description here

点击按钮后:

enter image description here

更新

我将Borders添加到MyButton构造函数

setBorder(LineBorder.createGrayLineBorder());

现在图标位于按钮边框内。我添加了图片。

enter image description here

enter image description here

但正如您所看到的,我们不再有这些圆形按钮边缘了。

1 个答案:

答案 0 :(得分:2)

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

this answer中所示。

更新

如果不是这样的话,我不会得到你想要达到的目标。

12 Yellow buttons w/ orange rollover icon

我将翻转图标设为橙色,这样我们就可以轻松看到一个按钮的大小,但是我连续放了4个,以确保最小的框架宽度没有在一行按钮之间插入额外的空间。 / p>

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