java传递对象从JFrame到JPanel [英] java pass object from JFrame to JPanel

查看:353
本文介绍了java传递对象从JFrame到JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NetBeans在Java中开发一个独立的应用程序,通过串行端口获取和发送数据。我使用了一个伟大的API用于串行通信,称为java简单串行连接器 http:/ /code.google.com/p/java-simple-serial-connector/



请下载我创建的以下NetBeans项目:
http://netload.in/dateiN9xmwRtn19.htm
即使您没有netbeans你仍然可以查看代码。



上面是一个小例子项目,我解释我想完成的。该示例包含一个包含main方法的JFrame。这个JFrame包含2个面板:一个带导航按钮的面板和一个使用CardLayout显示面板1和面板2的面板。







在JFrame我有一个方法获取端口名称列表使用getPortNames jssc API。我的问题是,我如何传递这些端口名称字符串值从JFrame到我的Panel 1,而不使用以下,因为它不工作:
MyJFrame myjframe = new MyJFrame();
myjframe.getPortNames();



气垫船鳗鱼
再次非常感谢您的解释,它值得金。我现在意识到我的问题是NetBeans而不是Java。我不熟悉它,但我仍然喜欢使用它,由于我的项目的大小和复杂性。我已经上传了2个屏幕截图,显示了我如何尝试将JFrame对象的this引用传递给NetBeans中的JPanel对象。正如你可以在MyJFrame.java屏幕截图中看到的,NetBeans将this引用加下划线作为错误。你能帮我解决这个问题吗?另外,在你的解释的第一句中你提到串口方法应该在一个单独的类,而不是在JFrame,因为他们是非GUI方法。我完全同意你的意见,我宁愿这样做,因为它是正确的面向对象的方法。但是这样做,我再次面临着同样的问题在NetBeans。我现在如何将对象引用从SerialPorts.java类传递给JFrame,JPanel等,以便我不会一直创建一个新的SerialPorts对象,记住我需要连接保持打开所有的时间(例如,使用SerialPorts sp = new SerialPorts();)。非常感谢您的帮助。



在面板1的代码自定义程序中添加this



MyJFrame.java - 编辑器下划线引用this as error

解决方案

我想知道你是否应该拥有一个非GUI模型类并且从它获取的端口名,而不是从JFrame但不管你的问题是一个更具体的一个一般问题的具体例子:你如何将信息从一个GUI对象传递到另一个,并可以进一步推广到:你如何将信息从一个对象传递到另一个。



简单的答案是让一个对象调用另一个方法,这是你尝试过的,这是什么应该工作,但是你的问题尝试是你在错误的对象上调用它。您有一个JFrame对象已经被构造,是可见的,并且包含您需要的信息, 是您应该调用您的方法的对象,而不是一个新的 JFrame对象。当然,新对象是从同一个类构建的,但是要理解,它是一个完全不同的对象,显示的是一个你想要的对象。



现在你的问题归结为这 - 我如何得到一个可视化的JFrame到另一个对象的引用?有很多方法可以做到这一点,但最简单的方法是通过一个构造函数或方法参数传递一个类的引用到另一个。



。 ..



例如,假设你有一个叫FooFrame的类来扩展JFrame,它拥有一个扩展JPanel的FooPanel对象,假设你想要FooPanel对象调用FooFrame的 someMethod()方法:

  public class FooFrame extends JFrame {

public String someMethod(){
return myTextField .getText();
}
}

您可以使用FooPanel创建一个新的FooFrame对象并调用这个方法正在尝试做:

  class FooPanel extends JPanel {

public void otherMethod (){
FooFrame fooFrame = new FooFrame();
fooFrame.someMethod();
}
}

但是你遇到同样的问题跑进上面。这里创建的FooFrame对象不是正在显示的对象,因此JTextField的内容将是空的并且不是很有帮助。我建议的解决方案是,你通过一个构造函数参数传递一个引用从原始的JFrame到JPanel如:

  class FooPanel extends JPanel {
private FooFrame fooFrame;

//构造函数接受对FooFrame对象的引用
public FooPanel(FooFrame fooFrame){
this.fooFrame = fooFrame; //并设置其字段
}

public void otherMethod(){
//不再需要!
// FooFrame fooFrame = new FooFrame();


//现在的方法在正确的对象上调用!
fooFrame.someMethod();然后你可以在FooFrame中创建你的FooPanel对象,并传递给你的FooPanel对象,然后通过传递给你的FooPanel对象。 FooFrame对象( this )。

  public class FooFrame extends JFrame {

private JTextField myTextField = new JTextField(10);
private FooPanel fooPanel;

public FooFrame(){
//当前FooFrame对象是这个!
fooPanel = new FooPanel(this);
add(fooPanel);
}

public String someMethod(){
return myTextField .getText();
}
}


I am developing a standalone application, in Java using NetBeans, that gets and sends data via Serial Port. I am using a great API for Serial communication called java simple serial connector http://code.google.com/p/java-simple-serial-connector/

Please download the following NetBeans project that I created: http://netload.in/dateiN9xmwRtn19.htm Even if you do not have netbeans you can still view the code.

The above is a small example project that I made to explain what I wish to accomplish. The example contains a JFrame that contains the main method. This JFrame contains 2 panels: a Panel with navigation buttons, and a Panel that displays Panel 1 and Panel 2 using CardLayout.

In the JFrame I have a method that gets port names list using the getPortNames() method of the jssc API. My question is, how do I pass these port names String values from JFrame to my Panel 1, without using the following since it will not work: MyJFrame myjframe = new MyJFrame(); myjframe.getPortNames();

Hovercraft Full Of Eels Once again thank you very much for your explanation, it is worth gold. I now realize that my problem is NetBeans rather than Java. I am not familiar with it enough but I would still prefer to use it due to the size and complexity of my project. I have uploaded 2 more screenshots below, that show how I tried to pass the "this" reference of the JFrame object to my JPanel object in NetBeans. As you can see in the MyJFrame.java screenshot, NetBeans has underlined "this" reference as an error. Would you please be able to help me with this problem? Also, in the first sentence of your explanation you mention that the serial port methods should be in a separate class, not in JFrame, since they are non-GUI methods. I fully agree with you and I would prefer to do it that way since it is the correct object-oriented approach. But doing it that way, I am once again facing the same problem in NetBeans. How do I now pass the object reference from SerialPorts.java class to JFrame, JPanel etc in a way so that I am not creating a new SerialPorts object all the time, remember I need the connection to stay open all the time (ex. without using SerialPorts sp = new SerialPorts();). Thank you so so so much for your help.

Adding "this" in code customizer for Panel 1

MyJFrame.java - editor underlines reference "this" as error

解决方案

I wonder if you should have the port names held by a non-GUI model class and obtained from it rather than from the "JFrame", but regardless your question is a specific example of a more of a general question: how do you pass information from one GUI object to another, and that can be generalized further to: how do you pass information from one object to another.

The simple answer is to have one object call a method on the other, which is what you've tried, and which is what should work, but the problem with your attempt is that you're calling it on the wrong object. You have a JFrame object that has already been constructed, that is visible, and that holds the information you need, and that is the object which you should be calling your method on, not a new JFrame object. Sure the new object is built from the same class, but understand that it is a completely different object from the one that's displayed which is the one you want.

So now your problem boils down to this -- how do I get a reference to the visualized JFrame into another object? There are many ways to do this, but the easiest way to do this is to probably pass a reference from one class to the other via a constructor or method parameter.

...

So for instance, say you've got a class called FooFrame that extends JFrame, and it holds a FooPanel object that extends JPanel, and suppose you want the FooPanel object to call the someMethod() method of FooFrame:

public class FooFrame extends JFrame {

   public String someMethod() {
      return myTextField .getText();
   }
}

You could have FooPanel create a new FooFrame object and call this method as you're trying to do:

class FooPanel extends JPanel {

   public void otherMethod() {
      FooFrame fooFrame = new FooFrame();
      fooFrame.someMethod();
   }
}

But then you run into the same problem you're running into above. The FooFrame object created here is not the one being displayed, and so the contents of the JTextField will be empty and not very helpful. The solution I'm suggesting is that you pass a reference from the original JFrame into the JPanel via a constructor parameter like so:

class FooPanel extends JPanel {
   private FooFrame fooFrame;

   // constructor accepts a reference to a FooFrame object
   public FooPanel(FooFrame fooFrame) {
      this.fooFrame = fooFrame; // and sets its field with it
   }

   public void otherMethod() {
      // no longer needed!
      // FooFrame fooFrame = new FooFrame(); 


      // now method is called on the right object!
      fooFrame.someMethod(); 
   }
}

Then you can create your FooPanel object in FooFrame and pass the FooFrame object (this) into it.

public class FooFrame extends JFrame {

   private JTextField myTextField = new JTextField(10);
   private FooPanel fooPanel;

   public FooFrame() {
      // the current FooFrame object is this!
      fooPanel = new FooPanel(this);
      add(fooPanel);
   }

   public String someMethod() {
      return myTextField .getText();
   }
}

这篇关于java传递对象从JFrame到JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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