Java与JFrame和类的交互 [英] Java with JFrame and class interaction

查看:84
本文介绍了Java与JFrame和类的交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一Package中有两个类.该程序包称为main. Main包含两个类:Main类和Display类.创建显示类以显示带有文本框和按钮的GUI.我为按钮提供了一个监听器,当用户单击其中一个按钮时,该监听器将触发.在主类中,有一个对象向量,我正在存储该对象,并且需要在显示类的文本框中一次显示一个对象.

I have two classes in the same Package. The package is called main. Main contains two classes a Main class and a Display class. The display class was created to display a GUI with text boxes and buttons. I gave the buttons a listener that fires when the user clicks one of the button. In the main class is a vector of object that i am storing and need to display one a time in the display class' text boxes.

我的问题是:鼠标单击的动作是否可以在主类中调用一种方法,以收集所需的信息,然后将其传递回显示类中的方法以修改那些文本框?>是否需要将我的两个类合并为一?我在课堂上会怎么做?

My Question is: can the mouse clicked action call a method in the main class that gathers the needed info an passes that back to a method in the display class to modify those text boxes> Do i need to combine my two classes into one? How would i do within the classes?

通过测试,我使主类扩展了显示类.我可以从这里启动display类,但是当我尝试从main中调用display方法时,它什么也没做.如果我尝试从显示类调用主方法,它似乎也无济于事.

From testing i have made the main class extend the display class. I am able to start the display class from here but when i try to call a method in display from main it does nothing. If i try to call a main method from the display class it also seems to do nothing.

推荐答案

如果您想让机械师修理汽车(通过启动,诊断问题,打开引擎盖等),请把汽车交给机械师,不是吗?

If you want your mechanic to fix your car (by starting it, diagnosing the problem, opening the hood, etc.), you give your car to the mechanic, don't you?

在Java中是相同的.如果要让显示对象(机械师)访问主要对象(汽车)中的可用信息,则需要将主要对象提供给显示器:

It's the same in Java. If you want the Display object (the mechanic) to access to information available in the Main object (the car), you need to give the Main object to the Display:

Main main = new Main(); // main contains data
Display display = new Display(main) // Display is constructed, and is given the main object

在显示中:

public void someButtonClicked() {
    String someInformation = this.main.getSomeInformation();
    this.someTextField.setText(someInformation);
}

这篇关于Java与JFrame和类的交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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