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

时间:2016-03-23 19:15:07

标签: java swing actionlistener jlabel imageicon

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

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

我在顶部有一个JLabel,带有一个欧元图标,表示起始货币。我有一个按钮,我想用它将图标更改为1美元。

我目前正在使用ActionListener并尝试使用setIcon / setIconImage的不同变体(每次尝试我都能想到看到到目前为止没有任何效果)。

public class MoneyConverter extends JFrame implements ActionListener{
     //add label and icon showing base conversion currency
     JLabel startcur = new JLabel("<--- Starting Curency", new ImageIcon("C:\\Users\\Russel\\Desktop\\1euro.gif"), SwingConstants.CENTER);
     JButton euro = new JButton("Swap to Euro");
     JButton dollar = new JButton("Swap to Dollar");

然后我设置了

public MoneyConverter(){} 

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

e.g。

    dollar.addActionListener(this);
    euro.addActionListener(this);

在通常的代码之后(setVisible以及为了您而省略的喜欢,因为我没有看到它干扰了这个,请告诉我是否应该包含所有内容)

public void ActionPerformed (ActionEvent e){
    Object source = e.getSource();
    if (source.equals(euro)){
         startcur.setIcon(new ImageIcon("C:\\Users\\Russel\\Desktop\\1.gif"));
    }
}

此部分已多次更改并且是此帖的主要原因,如何在JLabel中更改此图标? - 我也将在这里设置转换率,具体取决于他们选择以美元还是欧元开头。 (费率不会是实际费率。)

2 个答案:

答案 0 :(得分:1)

首先,创建并存储新的ImageIcon

ImageIcon image = new ImageIcon(getClass().getResource("/nameOfImage.jpg"));

然后将其放入Action Listener

label.setIcon(image);
label.setText("");

您必须确保为项目设置了资源文件夹。您可以在IntelliJEclipse

中了解如何执行此操作

您还宣布actionPerformed()错误。我建议你阅读this你应该这样做。

@Override
public void actionPerformed(ActionEvent e)
{

}

传统上,在java中,方法名称以小写字母开头,而Classes以大写字母开头。

答案 1 :(得分:0)

  

整个目的只是为了试验和学习这个很酷的工具,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();
            }
        });
    }
}