简体   繁体   English

如何从另一个 class 访问主 class 中的变量

[英]How to access Variable in Main class from another class

I'm using my main class as my UI for a program I'm working on, and in an another class I have a loop running until it loses network connection.我正在使用我的主要 class 作为我正在处理的程序的 UI,而在另一个 class 中,我有一个循环运行,直到它失去网络连接。 said field:说领域:

lastCrashField = new JLabel("",SwingConstants.CENTER);
    lastCrashField.setBounds(80,180,200,30);
    lastCrashField.setBorder(blackLine);

totalCrashField = new JLabel("",SwingConstants.CENTER);
    totalCrashField.setBounds(80,240,200,30);
    totalCrashField.setBorder(blackLine);

How can I update these 2 fields in the UI from a different class, been looking around for some time now and so far I'm drawing a blank thinking maybe I need to re-think the way I handle these fields from their base...如何从不同的 class 更新 UI 中的这两个字段,现在已经环顾了一段时间,到目前为止,我一直在画一个空白的想法,也许我需要重新考虑从它们的基础处理这些字段的方式.. .

You said:你说:

How to access Variable in Main class from another class如何从另一个 class 访问主 class 中的变量

To directly answer your question, choose one of these approaches:要直接回答您的问题,请选择以下方法之一:

  • Pass a reference to each of the fields as arguments to a method on an object of the other class.将对每个字段的引用作为 arguments 传递给另一个 class 的 object 上的方法。
  • Make instance members of the two JLabel widgets.创建两个 JLabel 小部件的实例成员。 Mark them non- private for direct access by an object of the other class.将它们标记为非private ,以供另一个 class 的 object 直接访问。

Code for the first bullet.第一个项目符号的代码。

public class CollisionForm extends … 
{
    // Member fields
    private JLabel lastCrashField, totalCrashField ;

    // In constructor, populate those two field references.
    …

    // Pass references to each of the two fields to your logic method doing some work to produce fresh data.
    void whatever() 
    {
        SomeHelperObject someHelperObject = new SomeHelperObject() ;
        someHelperObject.updateCrashFields( lastCrashField , totalCrashField ) ;
    }
}

public class SomeHelperClass
{
    // Passing two references to JLabel objects.
    void someLogic( JLabel lastCrash , JLabel totalCrash ) 
    {
        lastCrash.setText( … ) ;
        totalCrash.setText( … ) ;
    }
}

Code for the second bullet.第二个项目符号的代码。

public class CollisionForm extends … 
{
    // Member fields
    public JLabel lastCrashField, totalCrashField ;  // Public, not private.

    // In constructor, populate those two field references.
    …

    void whatever() 
    {
        SomeHelperObject someHelperObject = new SomeHelperObject( this ) ;  // Pass instance of Swing form (or controller) to the helper class constructor.
        someHelperObject.updateCrashFields() ;
    }
}

public class SomeHelperClass
{
    // Member fields
    CollisionForm form ;

    // Receiving instance of `CollisionForm` in the constructor of this class.
    SomeHelperClass( CollisionForm collisionForm ) 
    {
        this.form = collisionForm ;
    }

    void someLogic() 
    {
        this.form.lastCrashField.setText( … ) ;
        this.form.totalCrashField.setText( … ) ;
    }
}

But I do not recommend either of these approaches for working with Swing widgets.我不推荐使用这些方法中的任何一种来处理 Swing 小部件。 Read on.继续阅读。

You said:你说:

2 fields in the UI from a different class UI 中的 2 个字段来自不同的 class

Generally you should not have other classes directly manipulating your GUI widgets.通常,您应该让其他类直接操作您的 GUI 小部件。

Better to have the object containing/managing the widgets (a GUI layout object, or a controller object) ask other classes to do work.最好让 object 包含/管理小部件(GUI 布局 object 或 controller 对象)要求其他类做工作。 Any result data should be returned to the layout/controller which then updates the widgets.任何结果数据都应返回到布局/控制器,然后更新小部件。

public class CollisionForm extends … 
{
    // Member fields
    private JLabel lastCrashField, totalCrashField ;  

    // In constructor, populate those two field references.
    …

    void whatever() 
    {
        SomeHelperObject someHelperObject = new SomeHelperObject() ;  // Passing nothing about widgets. This other class remains blissfully ignorant of anything related to Swing.
        List< Integer > freshCrashData = someHelperObject.someLogic() ;
        this.lastCrashField.setText( freshCrashData.get( 0 ) ) ;
        this.totalCrashField.setText( freshCrashData.get( 1 ) ) ;
    }
}

// This version of our `SomeHelperClass` class knows nothing about Swing nor the widgets on our form.
public class SomeHelperClass
{
    List< Integer > someLogic()  // <-- Instead of `void`, return data
    {
        return List.of(
            … ,  // Last crash data.
            …    // Total crash data.
        );       // The calling method on the Swing form uses this data in this returned `List` to update its own widgets. 
    }
}

Tip: In Java 16+ I would use the new record feature to define a class, an instance of which would be returned by this method rather than a clumsy ambiguous List .提示:在 Java 16+ 中,我将使用新的记录功能来定义 class,此方法将返回其实例而不是笨拙的模棱两可的List

You said:你说:

I have a loop running until it loses network connection我有一个循环运行,直到它失去网络连接

This sounds like you are using background threads.这听起来像您正在使用后台线程。

BEWARE: Never access your Swing widgets from another thread.注意:切勿从另一个线程访问您的 Swing 小部件。 Use the "SwingWorker" classes through which the background thread asks Swing's own UI thread to update its widgets.使用“SwingWorker”类,后台线程通过该类要求 Swing 自己的 UI 线程更新其小部件。

Threading with Swing has been covered extensively already on Stack Overflow. Stack Overflow 上已经广泛介绍了使用 Swing 进行线程处理。 So search this site to learn more.因此,搜索此站点以了解更多信息。 And read tutorial on concurrency with Swing by Oracle.并阅读Oracle 的 Swing 并发教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM