gpt4 book ai didi

java - 为什么可以编译,但不会改变 seticon

转载 作者:行者123 更新时间:2023-12-02 06:51:14 25 4
gpt4 key购买 nike

我正在学习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));
}
}

最佳答案

首先,您永远不会调用 alterajlabel,其次,您的 metodos 的 alterajlabel 中的 Tela 实例 类与屏幕上的实例不同。您只需创建它的另一个副本并对其进行修改即可。

相反,您应该在创建 Tela 时将其引用传递给 metodos,这样 metodos 就可以处理在屏幕上

您需要更改您的 methodos 类,使其看起来更像...

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));
}
}

然后,您需要将 btnAlteraImagemActionListener 修改为更像...

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();
}
}
});

这意味着您不会不必要地暴露 Tela

您可能想查看Code Conventions for the Java Programming LanguageLearning the Java Language了解更多详情

此外,虽然我知道这可能看起来很困惑,但我强烈建议您花时间学习如何 use layout managers

关于java - 为什么可以编译,但不会改变 seticon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18007138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com