Java 自动调整JButton图标的大小
所以我有一个我添加图标的按钮。图标最初太大了,所以我事先调整了它们的大小,效果很好。除了当我调整窗口大小时,JButtons会改变大小,但图标不会改变,这是有问题的 有没有办法让图标只填充它所连接的JButton?一段代码,让它更清晰:Java 自动调整JButton图标的大小,java,swing,resize,jbutton,imageicon,Java,Swing,Resize,Jbutton,Imageicon,所以我有一个我添加图标的按钮。图标最初太大了,所以我事先调整了它们的大小,效果很好。除了当我调整窗口大小时,JButtons会改变大小,但图标不会改变,这是有问题的 有没有办法让图标只填充它所连接的JButton?一段代码,让它更清晰: public JewelClass(){ setBackground (new Color (30,30,30)); addActionListener(this); setLayout(new GridLayout());
public JewelClass(){
setBackground (new Color (30,30,30));
addActionListener(this);
setLayout(new GridLayout());
ImageIcon icon = new ImageIcon(src/carre.jpg);
setIcon (resizeIcon(icon,60,60));
}
resizeIcon是一个个人函数,它接受一个图标、一个宽度参数和一个高度参数,并返回一个调整大小的图标(显然)。
我试着改变布局,但没有改变任何东西。我尝试获取JButton的宽度/高度,但由于在添加图标时它们还不存在,因此无法工作
你们知道怎么度过这个难关吗?它不一定是一个图标,只要我的JButton中充满了我给它的图像,它就棒极了:)
谢谢 在Swing中,您可以将任何
JComponent
添加到另一个JComponent
,因为Image
是JLabel
最好的JComponent
,那么为什么不将JLabel\setIcon()
放在JButton
编辑:
您可以向该按钮添加一个组件侦听器,该按钮在调整图像大小时会调整其中的图像大小
yourButton.addComponentListener(new ComponentListener() {
@Override
public void componentShown(ComponentEvent e) {
// ignore
}
@Override
public void componentResized(ComponentEvent e) {
resizeIcon(icon, yourButton.getWidth(), yourButton.getHeight());
}
@Override
public void componentMoved(ComponentEvent e) {
// ignore
}
@Override
public void componentHidden(ComponentEvent e) {
// ignore
}
});
希望有帮助
paintComponent
图形
对象在绘制图像时,提供维度参数
getWidth()
和getHeight()
。通过这样做,将自动调整大小。此外,在调整大小时,您需要进行一些抗锯齿处理,以便图像不会太像素化 对于更简单的方法,更改为int margin=0;维度dim=this.getParent().getSize()无需重写getMinimumSize()
和getPreferredSize()
,因为父组件的边距已经设置好了!所以它可以绘制一个形状,但唯一的问题是,一旦我替换g.setColor(Color.red);g、 fillRect(边距、边距、尺寸宽度-边距*2、尺寸高度-边距*2)
byIcon-Icon=newimageicon(“src/triangle.jpg”);icon.paintIcon(这个,g,60,60)代码>,它将停止显示任何内容。你知道这是为什么吗?仍然没有调整图像大小(这是OP的问题,因为我理解它)基本上,添加一个组件到一个组件是一个。。。实现目标的非常迂回的方式,特别是当它没有实现或者我错过了什么?事实上,这个答案并没有达到预期的效果。@TomášZato可能是你的另一个错误
import java.awt.*;
import javax.swing.*;
public class ResizeIconInButton extends JFrame {
private static final long serialVersionUID = 1L;
private static final String IMAGE_PATH = "http://duke.kenai.com/misc/Bullfight.jpg";
private JButton myButton = new JButton();
private JLabel myLabel = new JLabel();
public ResizeIconInButton() {
Icon myIcon = new ImageIcon(IMAGE_PATH);
myLabel.setIcon(myIcon);
myButton.setLayout(new BorderLayout());
myButton.add(myLabel);
add(myButton, BorderLayout.CENTER);
setPreferredSize(new Dimension(200, 100));
setTitle("Resize Icon In Button");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ResizeIconInButton main = new ResizeIconInButton();
}
});
}
}
yourButton.addComponentListener(new ComponentListener() {
@Override
public void componentShown(ComponentEvent e) {
// ignore
}
@Override
public void componentResized(ComponentEvent e) {
resizeIcon(icon, yourButton.getWidth(), yourButton.getHeight());
}
@Override
public void componentMoved(ComponentEvent e) {
// ignore
}
@Override
public void componentHidden(ComponentEvent e) {
// ignore
}
});