String mycon="image1.png";
public LottoGUI(String one, String two,String three) {
Container C= getContentPane();
C.setLayout(new FlowLayout());
JLabel MyImage = new JLabel(new ImageIcon(mycon));
JButton labb = new JButton("OK!");
labb.addActionListener(this);
C.add(MyImage);
C.add(labb)
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==labb){}
}
如何在按下按钮时将图片从 image1 更改为新图片?
最佳答案
1) 尝试使用setIcon
方法。如果这不起作用,2) 替换
整个 JLabel
对象和一个新对象(具有新图标)。
如果使用第二种方法,你需要
将 MyImage
转换为类变量。
关于java - 如何更换新的 imageIcon?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21496792/