java左上角图标太小,调整图标大小以适应Java中的JButton?

Whenever I set an icon for my JButton it is always not sized correctly. How can I resize the icon to fit the button fully?

final JButton btnSanic = new JButton();

Image img = icon.getImage();

Image newimg = img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);

icon = new ImageIcon(newimg);

btnSanic.setIcon(icon);

解决方案

There are any number of issues. To start with, all Swing components DON'T auto scale images. Sure, might be a nice idea, but given the amount of time and processing required to do it efficiently, I understand why they don't, so you need to do all the work...

You should also remember, that the size of a component is not determined until it is laid out, and while you can provide all the sizing hints you might like, the layout manager is well within its rights to ignore one or more of these hints.

Instead of "hoping" you know the size of the button, you should make use of the ComponentListener API to receive notifications of when the component is actually resized...

6XbXW.gif

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.Image;

import java.awt.Insets;

import java.awt.event.ComponentAdapter;

import java.awt.event.ComponentEvent;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

public class TestButton {

public static void main(String[] args) {

new TestButton();

}

private BufferedImage master;

public TestButton() {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {

ex.printStackTrace();

}

try {

master = ImageIO.read(new File("C:\\svg\\Revert 256x256.png"));

JButton btn = new JButton() {

@Override

public Dimension getPreferredSize() {

return new Dimension(90, 50);

}

};

btn.addComponentListener(new ComponentAdapter() {

@Override

public void componentResized(ComponentEvent e) {

JButton btn = (JButton) e.getComponent();

Dimension size = btn.getSize();

Insets insets = btn.getInsets();

size.width -= insets.left + insets.right;

size.height -= insets.top + insets.bottom;

if (size.width > size.height) {

size.width = -1;

} else {

size.height = -1;

}

Image scaled = master.getScaledInstance(size.width, size.height, java.awt.Image.SCALE_SMOOTH);

btn.setIcon(new ImageIcon(scaled));

}

});

JFrame frame = new JFrame("Testing");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(btn);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

} catch (IOException exp) {

exp.printStackTrace();

}

}

});

}

}

Note: This example is far from optimised, but simply provides a broad concept of a possible solution...

Now, a word of warning. Image#getScaledInstance is neither the fastest or greatest of scaling algorithms...

Take a look at...

for more details...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值