更改不同类中 JTextField 的文本

发布于 2024-11-19 10:01:31 字数 946 浏览 3 评论 0原文

我如何从其他类更改 jTextField 的文本

假设我有 A 类,如果我选择一个项目并单击创建帐户。我向 jTabbedPane 添加了一个具有相同名称的选项卡。该选项卡为 B 类。 其代码是:

onclick“创建帐户”此函数addclass(mainCB.getSelectedIndex())已被调用

 public void addclass(int a) {
    String s=(String) mainCB.getItemAt(a); //mainCB is variable name of combobox
    JComponent subpanel2=new B(); //added the class
    jTabbedPane1.add(s,subpanel2); //added new tab which is the new class
    B ob=new B(); //object of new class B
    ob.heading(s); //heading is the function in Class B
}

现在我如何更改A类中的jTextField1文本。B

类中的heading()函数如下:

public void heading(String s){
    head.setText(s); //head is the variable name of jTextField1 of class B
}

我有贴出了A班和B班的照片。

这是A班 class A


jTabbedPane 中添加的新面板是 B 类。这在 A 类中被调用。

B 类

How can i change the text of a jTextField from other class

Suppose i have Class A in which if i select an item and click create account. I added a tab with same name to my jTabbedPane. This tab is class B.
the code for this is:

onclick on "Create Account" this function addclass(mainCB.getSelectedIndex()) has been called

 public void addclass(int a) {
    String s=(String) mainCB.getItemAt(a); //mainCB is variable name of combobox
    JComponent subpanel2=new B(); //added the class
    jTabbedPane1.add(s,subpanel2); //added new tab which is the new class
    B ob=new B(); //object of new class B
    ob.heading(s); //heading is the function in Class B
}

Now how can i Change the jTextField1 text from class A.

heading() function in class B is as follows:

public void heading(String s){
    head.setText(s); //head is the variable name of jTextField1 of class B
}

I have posted the image of both the classes A and B.

This is Class A
class A


The new panel which is added in jTabbedPane is class B. This is being called in class A.

Class B

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

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

发布评论

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

评论(1

命比纸薄 2024-11-26 10:01:31

您在 addClass 方法中创建了类 B 的两个实例。我认为在 subpanel2(类型为 B)上调用 heading 可以解决您的问题。这会是这样的:

public void addclass(int a) {
    String s=(String) mainCB.getItemAt(a); //mainCB is variable name of combobox
    B subpanel2=new B(); //added the class
    jTabbedPane1.add(s,subpanel2); //added new tab which is the new class
    subpanel2.heading(s); //heading is the function in Class B
}

这是你想要的吗?

You create two instances of the class B in your addClass method. I think it would solve your problem to call heading on subpanel2, which is of type B. This would go something like:

public void addclass(int a) {
    String s=(String) mainCB.getItemAt(a); //mainCB is variable name of combobox
    B subpanel2=new B(); //added the class
    jTabbedPane1.add(s,subpanel2); //added new tab which is the new class
    subpanel2.heading(s); //heading is the function in Class B
}

Is this what you wanted?

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