另一个类中的调用方法来设置标签文本(不要使用netbeans默认值) [英] Call method in another class to set label text (don't use netbeans' default stuff)

查看:68
本文介绍了另一个类中的调用方法来设置标签文本(不要使用netbeans默认值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用netbeans,并且想要编辑标签中的文本.我想从主驱动程序类中的另一个类编辑此标签.我大约有7或8个JDialog页面,并且说标签在那些页面之一上.

I'm using netbeans and want to edit the text in a label. I want to edit this label from another class in the main driver class. I have about 7 or 8 JDialog pages and let's say the label is on one of those pages.

当我尝试从那些JDialogs之一调用该方法以便可以对其进行编辑时,它会不断询问java.awt.Frame.我在哪里可以找到该框架名称?或者,是否有一种更简便的方法来编辑另一个类中的标签?

When I try to call the method from one of those JDialogs so that I can edit it, it keeps asking for a java.awt.Frame. Where would I find that Frame name? Or, is there an easier way of editing labels from another class?

Netbeans默认将标签设置为私有,所以我在网上看了看,人们说,使用setter方法将是最简单的.

Netbeans makes labels private by default so I looked online and people have said making a setter method would be easiest.

QuickScreen是一个.java文件,例如...

QuickScreen is a .java file for instance...

public static void resetNumbers(){
    QuickScreen qs = new QuickScreen(some frame);
    qs.editLabel("Hello");
}

请参考我对bmoran解决方案的最后评论.

Please refer to my last comment on bmoran's solution.

推荐答案

如果更改了要设置为默认或受保护级别访问权限的标签,则只要两个类都位于对话框中,就可以从对话框中进行设置.相同的软件包(默认情况下),或者您的对话框扩展了标签所在的类(受保护).

If you change the label you want to set to either default or protected level access, then you can set it from your dialog as long as both classes are in the same package (for default) or your dialog box extends the class that the label is in (protected).

    public class FrameClass extends JFrame {
        JLabel label1;// package access
        MyDialog dialog;

        //constructor *** Netbeans may have an init() method ***
        public FrameClass(){
        label1=new JLabel("Hello!");
        ...
        }

        ...
    }

public class MyDialog{
    public void changeLabel(){
        FrameClass.label1.setText("Good Bye!");
    }
}

这篇关于另一个类中的调用方法来设置标签文本(不要使用netbeans默认值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆