我只是学习java但是..
为什么它不改变?如果我将它放在按钮的动作监听器中,我可以使用设置图标更改图标:|
主要课程
package tstando;
public class executor {
public static void main(String[] args) {
Tela inicia = new Tela();
inicia.run();
}
}
GUI的类(tela)
package tstando;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Tela extends JFrame {
public JLabel lblImage = new JLabel("New label");
private static final long serialVersionUID = 1L;
public JPanel contentPane;
public void run() {
try {
Tela frame = new Tela();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public Tela() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
lblImage.setBounds(71, 31, 277, 150);
contentPane.add(lblImage);
lblImage.setIcon(new ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg"));
JButton btnAlteraImagem = new JButton("Altera Imagem");
btnAlteraImagem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
metodos met = new metodos();
//lblImage.setIcon(new ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg"));
}
});
btnAlteraImagem.setBounds(148, 213, 182, 23);
contentPane.add(btnAlteraImagem);
}
}
和方法的类
package tstando;
import java.io.IOException;
import javax.swing.ImageIcon;
public class metodos {
Tela altera = new Tela();
String link = "C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg";
public void alterajlabel() throws IOException {
altera.lblImage.setIcon(new ImageIcon(link));
}
}