gpt4 book ai didi

java - JButton setIcon 更新错误

转载 作者:行者123 更新时间:2023-12-01 07:36:42 26 4
gpt4 key购买 nike

目前我正在使用 netbeans 制作一个基于更改按钮中的图像的 java 程序....

实际上我的要求是当我单击另一个按钮(说A)时更改按钮的图像图标......

我想出了以下程序......

       // Following function is included inside the button's (Here A) ActionListener........
public void change_image()
{
if(sex==0)
{
ic=new ImageIcon("E:\\java_images\\female_profile.jpg");
sex=1;
}
else if(sex==1)
{
ic = new ImageIcon("E:\\java_images\\male_profile.png");
sex=0;
}

// To resize the image into the size of the button...
labelicon.setImage(ic.getImage().getScaledInstance(image_btn.getWidth(),image_btn.getHeight(), Image.SCALE_DEFAULT));

img_btn.setIcon(labelicon);

}

我包含的变量是

           private int sex;    // 0  - female, 1 - male

private ImageIcon ic,labelicon; // variables meant for storing ImageIcons.....
private JButton img_btn; // the button at which the image is to be displayed....

现在我观察到的奇怪行为是......

仅当我单击最小化按钮时,图像才会在单击按钮时显示。即当我单击按钮 A 时,ActionListener 中指定的代码将被执行。但只有当我最小化窗口并再次使其出现在屏幕上时,图像更改的效果才会出现....任何人都可以告诉我为什么会发生这种情况以及如何消除问题吗?

我想要的只是在单击 A 按钮时更改图像......嗯..我没有包含创建按钮的代码,因为它们可以通过 netbeans swing GUI 构建器轻松完成......

最佳答案

  1. 加载Icon/ImageIcon作为局部变量一次,没有理由从 ActionListener 重新加载图像

  2. API中的描述是Image#ScaledInstance非常异步

  3. 否则你必须打电话

.

labelicon.getImage().flush();
img_btn.setIcon(labelicon);

编辑

@akp 写道,但是..您将如何调整图标图像的大小..??

enter image description here enter image description here enter image description here

还有两种或三种其他方法可以放置 Icon/ImageIcon 并可随其父级调整大小,JLabel 可能是最简单的方法

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.swing.*;

public class JButtonAndIcon {

private static JLabel label = new JLabel();
private static Random random = new Random();
private static ImageIcon image1; // returns null don't worry about
private static ImageIcon image2; // returns null don't worry about
private static Timer backTtimer;
private static int HEIGHT = 300, WEIGHT = 200;

public static void main(String[] args) throws IOException {
label.setPreferredSize(new Dimension(HEIGHT, WEIGHT));
final JButton button = new JButton("Push");
button.setBorderPainted(false);
button.setBorder(null);
button.setFocusable(false);
button.setMargin(new Insets(0, 0, 0, 0));
button.setContentAreaFilled(false);
button.setLayout(new BorderLayout());
button.add(label);
button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
if (button.getIcon() == image1) {
label.setIcon(image2);
} else {
label.setIcon(image1);
}
}
});
JFrame frame = new JFrame("Test");
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
startBackground();
frame.setVisible(true);
}

private static void startBackground() {
backTtimer = new javax.swing.Timer(750, updateBackground());
backTtimer.start();
backTtimer.setRepeats(true);
}

private static Action updateBackground() {
return new AbstractAction("Background action") {

private static final long serialVersionUID = 1L;

@Override
public void actionPerformed(ActionEvent e) {
label.setIcon(new ImageIcon(getImage()));
}
};
}

public static BufferedImage getImage() {
int w = label.getWidth();
int h = label.getHeight();
GradientPaint gp = new GradientPaint(0f, 0f, new Color(
127 + random.nextInt(128),
127 + random.nextInt(128),
127 + random.nextInt(128)),
w, w,
new Color(random.nextInt(128), random.nextInt(128), random.nextInt(128)));
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
g2d.setPaint(gp);
g2d.fillRect(0, 0, w, h);
g2d.setColor(Color.BLACK);
return bi;
}
}

关于java - JButton setIcon 更新错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11095142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com