我只是在学习java但是..

为什么它没有改变?如果将其放在按钮的动作侦听器中,则可以使用设置的图标来更改图标:|

主班

package tstando;

public class executor {
    public static void main(String[] args) {
        Tela inicia = new Tela();
        inicia.run();
    }
}


GUI的类别(电话)

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,其次,您在Tela类的alterajlabel中的metodos实例与屏幕上的实例不同。您只需简单地创建它的另一个副本并对其进行修改。

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

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

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


然后,您需要将btnAlteraImagemTela修改为类似于...

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 LanguageLearning the Java Language以获得更多详细信息

另外,尽管我知道这似乎有些混乱,但我强烈建议您花时间学习如何use layout managers

09-05 09:17