Java 从另一个类访问 GUI 组件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22833328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access GUI components from another class
提问by horHAY
I'm new to Java and I've hit a brick wall. I want to access GUI components (that have been created in one class) from another class. I am creating a new GUI class from one class, like so;
我是 Java 新手,遇到了麻烦。我想从另一个类访问 GUI 组件(已在一个类中创建)。我正在从一个类创建一个新的 GUI 类,就像这样;
GUI gui = new GUI();
and I can access the components in that class, but when I go to a different class I cant. I really just need to access the JTextAreas
to update their content. Could someone point me in the right direction please, any help is greatly appreciated.
我可以访问该类中的组件,但是当我转到另一个类时我不能。我真的只需要访问JTextAreas
来更新他们的内容。有人能指出我正确的方向吗,非常感谢任何帮助。
GUI
Class:
GUI
班级:
public class GUI {
JFrame frame = new JFrame("Server");
...
JTextArea textAreaClients = new JTextArea(20, 1);
JTextArea textAreaEvents = new JTextArea(8, 1);
public GUI()
{
frame.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 3));
...
frame.setVisible(true);
}
}
采纳答案by Paul Samsotha
First respect encapsulation rules. Make your fields private
. Next you want to have getters
for the fields you need to access.
首先尊重封装规则。让你的领域private
。接下来,getters
您需要访问需要访问的字段。
public class GUI {
private JTextField field = new JTextField();
public GUI() {
// pass this instance of GUI to other class
SomeListener listener = new SomeListener(GUI.this);
}
public JTextField getTextField() {
return field;
}
}
Then you'll want to pass your GUI to whatever class needs to access the text field. Say an ActionListener
class. Use constructor injection (or "pass reference") for the passing of the GUI
class. When you do this, the GUI
being referenced in the SomeListener
is the same one, and you don't ever create a new one (which will not reference the same instance you need).
然后,您需要将 GUI 传递给访问文本字段所需的任何类。说一ActionListener
堂课。使用构造函数注入(或“传递引用”)来传递GUI
类。当你这样做时,GUI
被引用的对象SomeListener
是同一个,你永远不会创建一个新的(它不会引用你需要的同一个实例)。
public class SomeListener implements ActionListener {
private GUI gui;
private JTextField field;
public SomeListener(GUI gui) {
this.gui = gui;
this.field = gui.getTextField();
}
}
Though the above maywork, it may be unnecesary. First think about what exactly it is you want to do with the text field. If some some action that can be performed in the GUI class, but you just need to access something in the class to perform it, you could just implement an interface
with a method that needs to perform something. Something like this
虽然上述可能有效,但可能是不必要的。首先考虑一下您想要对文本字段做什么。如果某些操作可以在 GUI 类中执行,但您只需要访问类中的某些内容来执行它,您可以只interface
使用需要执行某些操作的方法来实现。像这样的东西
public interface Performable {
public void someMethod();
}
public class GUI implements Performable {
private JTextField field = ..
public GUI() {
SomeListener listener = new SomeListener(GUI.this);
}
@Override
public void someMethod() {
field.setText("Hello");
}
}
public class SomeListener implements ActionListener {
private Performable perf;
public SomeListener(Performable perf) {
this.perf = perf;
}
@Override
public void actionPerformed(ActionEvent e) {
perf.someMethod();
}
}
回答by Tony_craft
The best option to access that text areas is creating a get method for them. Something like this:
访问该文本区域的最佳选择是为它们创建一个 get 方法。像这样的东西:
public JTextArea getTextAreaClients(){
return this.textAreaClients;
}
And the same for the other one.So to access it from another class:
另一个也一样。所以从另一个类访问它:
GUI gui = new GUI();
gui.getTextAreaClients();
Anyway you will need a reference for the gui object at any class in which you want to use it, or a reference of an object from the class in which you create it.
无论如何,您将需要在要使用它的任何类中对 gui 对象的引用,或者从创建它的类中引用对象。
EDIT ---------------------------------------
编辑 - - - - - - - - - - - - - - - - - - - -
To get the text area from GUI to Server you could do something like this inside of Create-Server.
要将文本区域从 GUI 获取到服务器,您可以在 Create-Server 中执行类似操作。
GUI gui = new GUI();
Server server = new Server();
server.setTextAreaClients(gui.getTextAreaClients());
For this you should include a JTextArea field inside of Server and the setTextAreaClients method that will look like this:
为此,您应该在 Server 中包含一个 JTextArea 字段和如下所示的 setTextAreaClients 方法:
JTextArea clients;
public void setTextAreaClients(JTextArea clients){
this.clients = clients;
}
So in this way you will have a reference to the JTextArea from gui.
因此,通过这种方式,您将从 gui 获得对 JTextArea 的引用。
回答by trashgod
Several approaches are possible:
几种方法是可能的:
The identifier
gui
is a reference to yourGUI
instance. You can passgui
to whatever class needs it, as long as you respect the event dispatch thread. Addpublic
accessor methods toGUI
as required.Declarations such as
JTextArea textAreaClients
have package-privateaccessibility. They can be referenced form other classes in the same package.Arrange for your text areas to receive events from another class using a
PropertyChangeListener
, as shown here.
回答by Niranjan Dharmarajan
here i add a simple solution hope it works good,
在这里,我添加了一个简单的解决方案,希望效果很好,
Form A
表格A
controls
控制
Textfield : txtusername
文本字段:txt用户名
FormB fb = new FormB();
fb.loginreset(txtusername); //only textfield name no other attributes
Form B
to access FormA's control
Form B
访问 FormA 的控件
public void ResetTextbox(JTextField jf)
{
jf.setText(null); // or you can set or get any text
}
回答by rainer
There is actually no need to use a class that implements ActionListener.
实际上不需要使用实现 ActionListener 的类。
It works without, what might be easier to implement:
它没有工作,可能更容易实现:
public class SomeActionListener {
private Gui gui;
private JButton button1;
public SomeActionListener(Gui gui){
this.gui = gui;
this.button1 = gui.getButton();
this.button1.addActionListener(l -> System.out.println("one"));
}
}
and then, like others have elaborated before me in this topic:
然后,就像其他人在我之前在这个主题中详细阐述的那样:
public class GUI {
private JButton button = new JButton();
public GUI() {
// pass this instance of GUI to other class
SomeActionListener listener = new SomeActionListener(GUI.this);
}
public JButton getButton() {
return button;
}
}