Java 拉伸图标以适合按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13810213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Stretch Icon to fit button
提问by Brandon
I'm trying to resize my icon so that it covers the whole button and sits in the center of the button. When I try, it stretches my button and messes up everything else. How can I do it? Currently, my code is:
我正在尝试调整我的图标大小,使其覆盖整个按钮并位于按钮的中心。当我尝试时,它会拉长我的按钮并弄乱其他所有内容。我该怎么做?目前,我的代码是:
In my constructor of a class..
在我的一个类的构造函数中..
javax.swing.JButton Console = new javax.swing.JButton;
ScaleButtonImage(Console, ConsoleEnabledImage);
Within that class..
在那个班级..
private void ScaleButtonImage(javax.swing.JButton Button, java.awt.Image ButtonIcon) {
double Width = ButtonIcon.getWidth(Button);
double Height = ButtonIcon.getHeight(Button);
double xScale = 28/Width;//Button.getWidth() / Width;
double yScale = 28/Height;//Button.getHeight() / Height;
double Scale = Math.min(xScale, yScale); //ToFit
//double Scale = Math.max(xScale, yScale); //ToFill
java.awt.Image scaled = ButtonIcon.getScaledInstance((int)(Scale * Width), (int)(Scale * Height), java.awt.Image.SCALE_SMOOTH);
Button.setIcon(new javax.swing.ImageIcon(scaled));
}
LAYOUT:
布局:
.addGroup(layout.createSequentialGroup()
.addComponent(Enable, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Graphics, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Debug, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Console, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
Then I link all of them horizontally and vertically so they're all the same size.
然后我将它们水平和垂直链接起来,使它们的大小都相同。
Instead, it ends up looking like the below. Also, if I change the icon of the first button, all the buttons change size because of my constrains. How do I make the icons fit the buttons?
相反,它最终看起来像下面这样。此外,如果我更改第一个按钮的图标,由于我的限制,所有按钮都会更改大小。如何使图标适合按钮?
回答by Branislav Lazic
Try something like this (If I didn't make any mistake with brackets):
尝试这样的事情(如果我没有用括号弄错的话):
JButton button = new JButton(new ImageIcon(((new ImageIcon(
"path-to-image").getImage()
.getScaledInstance(64, 64,
java.awt.Image.SCALE_SMOOTH)))));
This way your image will be resized (in my case to size 64x64) and added to button
, like in this example here:
这样你的图像将被调整大小(在我的例子中为 64x64)并添加到button
,就像在这个例子中一样:
EDIT:
编辑:
This is the way to resize your image and to keep image ratio:
这是调整图像大小并保持图像比例的方法:
ImageIcon ii = new ImageIcon("path-to-image");
int scale = 2; // 2 times smaller
int width = ii.getIconWidth();
int newWidth = width / scale;
yourComponent.setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
java.awt.Image.SCALE_SMOOTH)));
回答by VGR
This worked for me:
这对我有用:
public class ImageButton
extends JButton {
private static final long serialVersionUID = 1;
/** @serial */
private Image image;
/** @serial */
private final Rectangle innerArea = new Rectangle();
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
SwingUtilities.calculateInnerArea(this, innerArea);
g.drawImage(image,
innerArea.x, innerArea.y, innerArea.width, innerArea.height,
this);
}
}
}