Why is my JFrame not appearing in a web based Java app?

空扰寡人 提交于 2019-12-11 03:32:43

问题


I have a game application that I will be running in a web browser using the tag. This program has a JFrame, which displays a couple of tabs allowing navigation throughout the program. When I run the program locally it will work fine, displaying the JFrame and working in entirety. However when I upload it to the host and visit it's link the JFrame will not display..

I have searched about 3 hours and simply must not be able to provide the correct keywords for my siutation. All I can get in results is the JFrame not appearing at all, locally or web app.

This is how the client looks when I run it locally from the same .jar:

This is how the client looks in a web browser (tried IE, FF and Chrome):

Applet.java:

public class Client extends RSApplet {

    public static void main(String args[]) {
        try {
            instance = new Client();
            new UserInterface(args);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

}

GUI.java

public class GUI extends Applet implements ActionListener, WindowListener, FocusListener {

    public UserInterface(String args[]) {
        try {
        /* ------------------------------------------------------------------------ */
        /* Metal                                                                    */
        /*  UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");    */
        /* System Look                                                              */
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        /* ------------------------------------------------------------------------ */
            initGUI();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void initGUI() {
        try {
            JFrame.setDefaultLookAndFeelDecorated(true);
            JPopupMenu.setDefaultLightWeightPopupEnabled(false);
            frame = new JFrame(frameTitle);
            frame.setLayout(new BorderLayout());
            Image icon = Toolkit.getDefaultToolkit().getImage(iconDir);
            frame.setIconImage(icon);
            gamePanel = new JPanel();
            gamePanel.setLayout(new BorderLayout());
            gamePanel.add(this);
            gamePanel.setPreferredSize(new Dimension(850, 600));
            frame.getContentPane().add(gamePanel, BorderLayout.CENTER);
            frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    if (loggedIn == false) {
                        System.exit(0);
                    } else {
                        if (showMessage("Are you sure you want to close the game?") == true) {
                            System.exit(0);
                        } else return;
                    }
                }
            });
            initMenuBar();
            frame.pack();
            frame.setVisible(true);
            frame.setResizable(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void initMenuBar() {
        menuBar = new JMenuBar();
        /** File **/
        fileMenu = new JMenu("File");
        String[] fileMenuItems = {
            "Quit"
        };
        menuBar.add(fileMenu);
        /** Quick Links **/
        linksMenu = new JMenu("Quick Links");
        String[] linksMenuItems = {
            "Home", "News", "Donate"
        };
        menuBar.add(linksMenu);
        frame.getContentPane().add(menuBar, BorderLayout.NORTH);
    }

}

回答1:


Are you trying to put the application on a web page, i mean to run as a web application. If yes then you want an Applet or JApplet and not a frame. A frame works normally but for you to add it to a website then you need to make it an applet. Make use of the links below..

JApplet class for se6- http://download.oracle.com/javase/6/docs/api/javax/swing/JApplet.html JApplet/Applet tutorial -- http://download.oracle.com/javase/tutorial/uiswing/components/applet.html

Good Luck



来源:https://stackoverflow.com/questions/6010925/why-is-my-jframe-not-appearing-in-a-web-based-java-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!