Java 如何使用JButton更改JLabel中的图标

Java 如何使用JButton更改JLabel中的图标,java,swing,actionlistener,jlabel,imageicon,Java,Swing,Actionlistener,Jlabel,Imageicon,我正在制作一个简单的转换工具,将美元转换成欧元,反之亦然 整个目的只是为了实验和学习这个很酷的工具,java 我在顶部有一个JLabel,上面有一个欧元图标,表示起始货币。我有一个按钮在下面,我想用它来改变图标到一美元一 目前,我正在与一个ActionListener进行交流,并尝试setIcon/setIconImage的不同变体(我能想到的每一篇文章都表明,迄今为止,一切都不起作用) 方法,并将所有“我的组件”添加到网格布局,并将ActionLister添加到“我的转换”按钮 e、 g 在

我正在制作一个简单的转换工具,将美元转换成欧元,反之亦然

整个目的只是为了实验和学习这个很酷的工具,java

我在顶部有一个JLabel,上面有一个欧元图标,表示起始货币。我有一个按钮在下面,我想用它来改变图标到一美元一

目前,我正在与一个ActionListener进行交流,并尝试setIcon/setIconImage的不同变体(我能想到的每一篇文章都表明,迄今为止,一切都不起作用)


方法,并将所有“我的组件”添加到网格布局,并将ActionLister添加到“我的转换”按钮

e、 g

在完成常规代码(setVisible和出于您的考虑我将省略的类似代码之后,因为我看不到它会干扰这一点,请让我知道是否应该包含所有代码)


此部分已更改多次,这是本文的主要原因,如何更改JLabel中的此图标我还将在这里设置转换率,这取决于他们选择从美元还是欧元开始。(费率不是实际费率。)

首先,创建并存储一个新的
图像图标

ImageIcon image = new ImageIcon(getClass().getResource("/nameOfImage.jpg"));
然后将其放入您的
操作侦听器中

label.setIcon(image);
label.setText("");
您必须确保为项目设置了资源文件夹。您可以在或中阅读如何做到这一点

您还声明
actionPerformed()
错误。我建议你应该这样做

@Override
public void actionPerformed(ActionEvent e)
{

}
按照惯例,在java中,方法名以小写字母开头,类以大写字母开头

整个目的只是为了实验和学习这个很酷的工具,java

然后我将向您展示如何通过多种方式改进此程序

//Don't make your Swing class implement Actionlistener and add it as a
//listener to itself in the constructor before it's fully initialized
public class MoneyConverter extends JFrame {
    //These don't have to be loaded at runtime, so make them into constants
    //Java variables and methods follow thisNamingConvention
    private static final Icon euroIcon = new ImageIcon("C:\\Users\\Russel\\Desktop\\1euro.gif");
    private static final Icon dollarIcon = new ImageIcon("C:\\Users\\Russel\\Desktop\\1dollar.gif");
    //These you probably want to use later so save them as private class variables
    //Make them final so you can access them in the ActionListeners below
    private final JLabel currencyLabel;
    private final JButton euroButton;
    private final JButton dollarButton;

    public MoneyConverter() {
        //These are declared final and are are therefore usually set first in constructor
        this.currencyLabel = new JLabel("<--- Starting Curency", euroIcon, SwingConstants.CENTER);
        this.euroButton = new JButton("Swap to Euro");
        this.dollarButton = new JButton("Swap to Dollar");

        //Write your own separate ActionListener for each button
        euroButton.addActionListener(new ActionListener() {
            @Override
            public void run() {
                currencyLabel.setIcon(euroIcon);
                //These two are required for you to see the effect
                //This should also be the solution to your initial problem
                currencyLabel.revalidate();
                currencyLabel.repaint();
            }
        });
        dollarButton.addActionListener(new ActionListener() {
            @Override
            public void run() {
                currencyLabel.setIcon(dollarIcon);
                currencyLabel.revalidate();
                currencyLabel.repaint();
            }
        });

        //Add your components here using whatever layout manager you want.
    }

    public static void main(String []args){
        //Start new Swing applications like this to prevent it from
        //clogging the rest of the program
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MoneyConverter();
            }
        });
    }
}
//不要让Swing类实现Actionlistener并将其添加为
//侦听器在完全初始化之前在构造函数中对其自身进行初始化
公共类MoneyConverter扩展JFrame{
//这些不必在运行时加载,所以将它们转换为常量
//Java变量和方法遵循这个命名约定
私有静态最终图标euroIcon=新图像图标(“C:\\Users\\Russel\\Desktop\\1euro.gif”);
私有静态最终图标dollarIcon=新图像图标(“C:\\Users\\Russel\\Desktop\\1dollar.gif”);
//您可能希望稍后使用这些变量,因此将它们保存为私有类变量
//将它们设置为最终版本,以便您可以在下面的ActionListeners中访问它们
私人最终JLabel currencyLabel;
私人最终JButton euroButton;
私人最终JButton dollarButton;
公帑转换公司(){
//它们被声明为final,因此通常在构造函数中首先设置

this.currencyLabel=新的JLabel("这可能会对您有所帮助:您可以在
actionPerformed
方法和
条件中添加
print
语句,以查看是否调用了该方法以及检查是否正确。此外,该方法是否真的调用了
actionPerformed
,大写字母a?我将很快研究并发性和线程,以便我可以理解这个相关的问题,这是一个附带项目,用于应用我在盲目前进之前所做的事情,我想我一定忽略了一些简单的事情。感谢你的回答,希望在我开始使用线程后能有所帮助…你不应该在actionPreformed()中创建新的ImageIcon方法。您的构造函数应该创建每个图标一次并存储它。然后在actionPerformed()方法中,您只需调用
startcur.setIcon(TheCorrecicon)
“请告诉我是否应该包含所有图标”No.1)为了更快地获得更好的帮助,请发布一个或。2)获取图像的一种方法例如,热链接到中看到的图像。
ImageIcon image = new ImageIcon(getClass().getResource("/nameOfImage.jpg"));
label.setIcon(image);
label.setText("");
@Override
public void actionPerformed(ActionEvent e)
{

}
//Don't make your Swing class implement Actionlistener and add it as a
//listener to itself in the constructor before it's fully initialized
public class MoneyConverter extends JFrame {
    //These don't have to be loaded at runtime, so make them into constants
    //Java variables and methods follow thisNamingConvention
    private static final Icon euroIcon = new ImageIcon("C:\\Users\\Russel\\Desktop\\1euro.gif");
    private static final Icon dollarIcon = new ImageIcon("C:\\Users\\Russel\\Desktop\\1dollar.gif");
    //These you probably want to use later so save them as private class variables
    //Make them final so you can access them in the ActionListeners below
    private final JLabel currencyLabel;
    private final JButton euroButton;
    private final JButton dollarButton;

    public MoneyConverter() {
        //These are declared final and are are therefore usually set first in constructor
        this.currencyLabel = new JLabel("<--- Starting Curency", euroIcon, SwingConstants.CENTER);
        this.euroButton = new JButton("Swap to Euro");
        this.dollarButton = new JButton("Swap to Dollar");

        //Write your own separate ActionListener for each button
        euroButton.addActionListener(new ActionListener() {
            @Override
            public void run() {
                currencyLabel.setIcon(euroIcon);
                //These two are required for you to see the effect
                //This should also be the solution to your initial problem
                currencyLabel.revalidate();
                currencyLabel.repaint();
            }
        });
        dollarButton.addActionListener(new ActionListener() {
            @Override
            public void run() {
                currencyLabel.setIcon(dollarIcon);
                currencyLabel.revalidate();
                currencyLabel.repaint();
            }
        });

        //Add your components here using whatever layout manager you want.
    }

    public static void main(String []args){
        //Start new Swing applications like this to prevent it from
        //clogging the rest of the program
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MoneyConverter();
            }
        });
    }
}