我只是在研究java但是..
为什么不改变?如果我把它放在按钮的动作监听器中,我可以使用设置图标更改图标:|
MAIN CLASS
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));
}
}
答案 0 :(得分:2)
首先,您永远不会致电alterajlabel
,其次,Tela
类alterajlabel
中metodos
的实例不是同一个实例在屏幕上。您只需创建另一个副本并进行修改即可。
相反,您应该在创建时将Tela
的引用传递给metodos
,这样metodos
可以处理屏幕上的实例
您需要将metodos
课程更改为更像......
public class metodos {
Tela tela;
String link = "C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg";
public metods(Tela tela) {
this.tela = tela;
}
public void alterajlabel() throws IOException {
tela.lblImage.setIcon(new ImageIcon(link));
}
}
然后,您需要修改ActionListener
的{{1}}更多...
btnAlteraImagem
你也可以......
btnAlteraImagem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
metodos met = new metodos(Tela.this);
try {
met.alterajlabel();
} catch (IOException exp) {
exp.printStackTrace();
}
}
});
并称之为......
public class metodos {
String link = "C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg";
public void alterajlabel(Tela tela) throws IOException {
tela.lblImage.setIcon(new ImageIcon(link));
}
}
这可能是一种稍好的方法。
然而,在我看来,最好做一些像......
btnAlteraImagem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
metodos met = new metodos();
try {
met.alterajlabel(Tela.this);
} catch (IOException exp) {
exp.printStackTrace();
}
}
});
你会称之为......
public class metodos {
String link = "C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg";
public void alterajlabel(JLabel label) throws IOException {
label.setIcon(new ImageIcon(link));
}
}
这意味着您不会不必要地展示btnAlteraImagem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
metodos met = new metodos();
try {
met.alterajlabel(lblImage);
} catch (IOException exp) {
exp.printStackTrace();
}
}
});
您可能需要查看Code Conventions for the Java Programming Language和Learning the Java Language了解更多详情
另外,虽然我知道这看起来很混乱,但我强烈建议您花点时间学习如何use layout managers