简体   繁体   中英

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. 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.

public browser(object panel)

Your variable panel could well be an object, but the class object is defined as Object , not 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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