简体   繁体   English

将对象传递给Java中的类

[英]passing objects to a class in java

I am using webview in JavaFx and in my app I have several panels which I would like to embed a browser window onto. 我在JavaFx中使用webview,在我的应用程序中,我有几个面板要嵌入浏览器窗口。 I am struggling with the object oriented bit. 我正在努力与面向对象的位。 Can you see what I need to do differently. 你能看到我需要做些什么吗?

In my main program I have this for one panel: 在我的主程序中,我有一个面板:

            JPanel jPanel = new JPanel(new BorderLayout());
            browser browser = new browser(jPanel);
            browser.setVisible(true);
            browser.loadURL("http://www.google.com");

I get a red underline on the second line. 我在第二行出现红色下划线。

In my browser class: 在我的浏览器类中:

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView; 
import javax.swing.*;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;


public class browser extends JFrame {

    private final JFXPanel jfxPanel = new JFXPanel();
    private WebEngine engine;
    //private final JPanel panel = new JPanel(new BorderLayout());


    public browser(object panel) {
        super();
        initComponents();
    }


    private void initComponents() {

        createScene();

        panel.add(jfxPanel, BorderLayout.CENTER);

        getContentPane().add(panel);

        setPreferredSize(new Dimension(1024, 600));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();

    }

    private void createScene() {

        Platform.runLater(new Runnable() {
            @Override 
            public void run() {

                WebView view = new WebView();
                engine = view.getEngine();


                jfxPanel.setScene(new Scene(view));
            }
        });
    }

    public void loadURL(final String url) {
        Platform.runLater(new Runnable() {
            @Override 
            public void run() {
                String tmp = toURL(url);

                if (tmp == null) {
                    tmp = toURL("http://" + url);
                }

                engine.load(tmp);
            }
        });
    }

    private static String toURL(String str) {
        try {
            return new URL(str).toExternalForm();
        } catch (MalformedURLException exception) {
                return null;
        }
    }


}

I get a red underline where it says Object and also wherever it says panel. 我在其下标为Object的地方以及在其上标明面板的地方都有红色下划线。

public browser(object panel)

Your variable panel could well be an object, but the class object is defined as Object , not object . 您的可变panel很可能是一个对象,但是类对象被定义为Object ,而不是object Causing the "red line" AKA a compiler error. 导致“红线”又称为编译器错误。

The reason you also get a compiler error on panel, is because they're not visible within your other methods. 您还会在面板上看到编译器错误的原因是,它们在您的其他方法中不可见。 You pass your object panel into the constructor, but it is never set within the class. 您将对象panel传递到构造函数中,但从未在类中设置它。 Eg making a class-wide variable like: 例如,制作一个类范围的变量,例如:

private Object panel;
public browser(Object panel) {
    super();
    initComponents();
    this.panel = panel;
}

That will allow your panel object to be seen in the entire class. 这将使您的面板对象在整个类中都可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM