通过线程交互更改 Eclipse 中的标签图标

发布于 2024-10-21 02:16:27 字数 3340 浏览 1 评论 0原文

我目前正在开发一个交互式用户界面,它会向您显示是否打开了温室系统的水或照明组件。

当从另一个窗口单击按钮时,我遇到了相当大的挑战,该按钮将其标志值传递给线程类,这反过来又使图标更改

控制器 - > >线程逻辑-> NewSim

这里是我在 UI 窗口中使用的一些示例测试代码 我还是一个初学者,所以我使用 Eclipse 自动生成的代码,

粘贴代码似乎存在一些问题,

这是我用来修改标志值的测试代码片段

ThreadLogic

while(!t.interrupted()){

            sim = new NewSim();
            try {
                sim.setLightStatus(1);
                System.out.println("flag is 1");
                t.sleep(5000);

                sim.setLightStatus(0);
                System.out.println("flag is 0");
                t.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


    }

新模拟

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

//VS4E -- DO NOT REMOVE THIS LINE!
public class NewSim extends JFrame {

    private static final long serialVersionUID = 1L;
    protected JLabel water;
    protected JLabel light;
    Icon waterIcon = null;
    Icon lightIcon = null;  
    private int lightFlag = 0;
    private JPanel iconPane;
    private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";

    public NewSim() {
        initComponents();
    }

    private void initComponents() {
        setLayout(new GroupLayout());
        add(getIconPane(), new Constraints(new Leading(4, 313, 10, 10), new Leading(4, 232, 10, 10)));
        setSize(320, 240);
    }

    private JPanel getIconPane() {
        if (iconPane == null) {
            iconPane = new JPanel();
            iconPane.setLayout(new GroupLayout());
            iconPane.add(getWaterLabel(), new Constraints(new Leading(163, 10, 10), new Leading(79, 12, 12)));
            iconPane.add(getLightLabel(), new Constraints(new Leading(115, 12, 12), new Leading(79, 12, 12)));
        }
        return iconPane;
    }

    private JLabel getLightLabel() {
        if (light == null) {
            light = new JLabel();
            System.out.println(this.lightFlag);
            setLightStatus(0); 

        }
        return light;
    }

    private JLabel getWaterLabel() {
        if (water == null) {
            water = new JLabel();
            water.setIcon(new ImageIcon(getClass().getResource("/images/wateroff.png")));
        }
        return water;
    }
    public void setLightStatus(int lightFlag2) {
        // TODO Auto-generated method stub
        this.lightFlag = lightFlag2;
        switch(this.lightFlag){
            case 0: System.out.println("case 0");
                    light.setIcon(new ImageIcon(getClass().getResource("/images/lightoff.png")));
                    light.revalidate();
                    break;
            case 1: System.out.println("case 0");
                    light.setIcon(new ImageIcon(getClass().getResource("/images/lighton.png")));
                    light.revalidate();
                    break;
        }
    }
}

I'm currently developing an interactive UI which shows you if you turned on a water or lighting component of a greenhouse system.

I am having quite a challenge in making the label icons change when a button is clicked from another window which passes its flag values to a thread class, which, in turn makes the icon change

Controller -> ThreadLogic -> NewSim

here are some sample test codes i used in the UI window
i'm still a beginner so I am using the auto-generated codes from eclipse

there seems to be some problem with pasting the code

this is the test code snippet I use to modify the flag values

ThreadLogic

while(!t.interrupted()){

            sim = new NewSim();
            try {
                sim.setLightStatus(1);
                System.out.println("flag is 1");
                t.sleep(5000);

                sim.setLightStatus(0);
                System.out.println("flag is 0");
                t.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


    }

NewSim

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

//VS4E -- DO NOT REMOVE THIS LINE!
public class NewSim extends JFrame {

    private static final long serialVersionUID = 1L;
    protected JLabel water;
    protected JLabel light;
    Icon waterIcon = null;
    Icon lightIcon = null;  
    private int lightFlag = 0;
    private JPanel iconPane;
    private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";

    public NewSim() {
        initComponents();
    }

    private void initComponents() {
        setLayout(new GroupLayout());
        add(getIconPane(), new Constraints(new Leading(4, 313, 10, 10), new Leading(4, 232, 10, 10)));
        setSize(320, 240);
    }

    private JPanel getIconPane() {
        if (iconPane == null) {
            iconPane = new JPanel();
            iconPane.setLayout(new GroupLayout());
            iconPane.add(getWaterLabel(), new Constraints(new Leading(163, 10, 10), new Leading(79, 12, 12)));
            iconPane.add(getLightLabel(), new Constraints(new Leading(115, 12, 12), new Leading(79, 12, 12)));
        }
        return iconPane;
    }

    private JLabel getLightLabel() {
        if (light == null) {
            light = new JLabel();
            System.out.println(this.lightFlag);
            setLightStatus(0); 

        }
        return light;
    }

    private JLabel getWaterLabel() {
        if (water == null) {
            water = new JLabel();
            water.setIcon(new ImageIcon(getClass().getResource("/images/wateroff.png")));
        }
        return water;
    }
    public void setLightStatus(int lightFlag2) {
        // TODO Auto-generated method stub
        this.lightFlag = lightFlag2;
        switch(this.lightFlag){
            case 0: System.out.println("case 0");
                    light.setIcon(new ImageIcon(getClass().getResource("/images/lightoff.png")));
                    light.revalidate();
                    break;
            case 1: System.out.println("case 0");
                    light.setIcon(new ImageIcon(getClass().getResource("/images/lighton.png")));
                    light.revalidate();
                    break;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒眉千度 2024-10-28 02:16:27

在 Swing 中,您不能在事件调度线程 (EDT) 之外的任何其他线程中创建或修改 swing 组件。

因此,应在 EDT 中执行以下几行:

sim = new NewSim();
sim.setLightStatus(1);
sim.setLightStatus(0);

使用 SwingUtilities.invokeLater() 在 EDT 而不是当前(非 EDT)线程中执行某些代码。并阅读所有 swing 组件的 Javadoc 中存在的警告: http://download.oracle.com/javase/6/docs/api/javax/swing/package-summary.html#threading

In Swing, you can't create or modify a swing component in any othe thread than the Event Dispatch Thread (EDT).

The following lines should thus be executed in the EDT :

sim = new NewSim();
sim.setLightStatus(1);
sim.setLightStatus(0);

Use SwingUtilities.invokeLater() to execute some code in the EDT rather than the current (non-EDT) thread. And read the warning present in the Javadoc of all the swing components : http://download.oracle.com/javase/6/docs/api/javax/swing/package-summary.html#threading

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文