自动调整大小的JButton图标 [英] Auto-resizing JButton Icon
问题描述
所以我有向其中添加图标的JButton.图标最初太大,因此我需要预先调整它们的大小,并且效果很好.除了在调整窗口大小时,JButton会更改大小,而图标不会更改,这是有问题的.
so I have this JButtons to which I add Icons. The Icons are too large initially, so I resize them beforehand, and it works fine. Except that when I resize the window, the JButtons change size, but not the Icons, which is problematic.
是否有一种方法可以让Icon仅仅填充它所连接的JButton?一点代码使其更加清晰:
Is there a way to have an Icon just fill the JButton it is attached to? Bit of code to make it clearer:
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是一个个人函数,它接受一个Icon,一个width参数和一个height参数,并返回一个经过调整大小的Icon(显然). 我尝试更改布局,但没有任何改变.我尝试获取JButton的宽度/高度,但是由于添加Icon时它们尚不存在,因此不起作用.
resizeIcon being a personal function, that takes an Icon, a width parameter and a height parameter, and return a resized Icon (obviously). I tried changing the Layout, but it doesn't change anything. I tried getting the width/height of the JButton, but since they don't exist yet when the Icon is added, it doesn't work.
你们想知道如何解决这个问题吗?只要我的JButton充满了我给它的图像,它就不必是图标,那真是棒极了:)
Would you guys have any idea how to get through this? It doesn't have to be an icon, as long as my JButton is filled with the image I give it, it's awesome :)
谢谢!
推荐答案
在Swing中,您可以将任何JComponent
添加到另一个JComponent
中,因为Image
是JLabel
最好的JComponent
,那么为什么不呢?将JLabel#setIcon()
放入JButton
in Swing you can add any JComponent
to the another JComponent
, for Image
is JLabel
the best JComponent
, then why not put the JLabel#setIcon()
to the JButton
import java.awt.*;
import javax.swing.*;
public class ResizeIconInButton extends JFrame {
private static final long serialVersionUID = 1L;
public ResizeIconInButton() {
JButton myButton = new JButton();
myButton.setLayout(new BorderLayout());
myButton.add(new CustomComponents0());
add(myButton, BorderLayout.CENTER);
setPreferredSize(getPreferredSize());
setTitle("Resize Icon In Button");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
ResizeIconInButton main = new ResizeIconInButton();
}
};
javax.swing.SwingUtilities.invokeLater(r);
}
}
class CustomComponents0 extends JLabel {
private static final long serialVersionUID = 1L;
@Override
public Dimension getMinimumSize() {
return new Dimension(200, 100);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}
@Override
public void paintComponent(Graphics g) {
int margin = 10;
Dimension dim = getSize();
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
}
}
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();
}
});
}
}
这篇关于自动调整大小的JButton图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!