0

我只是学习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));       
    }
}
4

1 回答 1

2

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

相反,您应该在创建它时传递Telato的引用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您的 修改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();
        }
    }
});

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

您可能需要查看Java 编程语言的代码约定学习 Java 语言以获取更多详细信息

另外,虽然我知道这看起来很混乱,但我强烈建议您花时间学习如何使用布局管理器

于 2013-08-02T01:08:11.480 回答