0

I'm currently trying to create a very small game in Java, right now I don't know much about Java. Is it possible to open and show and HTML file, like a start up menu?

Example:

  1. Jframe starts

  2. Html file (menu) menu.html

  3. Depends on choice - levels.html - settings.html and exit button

1 Answer 1

1

You can display very simple html pages (nothing too fancy with CSS3, Javascript and stuff) within a JScrollPane.

JTextPane tp = new JTextPane();
JScrollPane js = new JScrollPane();
js.getViewport().add(tp);
JFrame jf = new JFrame();
jf.getContentPane().add(js);
jf.pack();
jf.setSize(800,800);
jf.setVisible(true); 

try {
  URL url = new URL("http://www.google.com");
  tp.setPage(url);
} 
catch (Exception e) {
  e.printStackTrace();
}

You can also use html pages within a jar file and load them like this:

URL url = getClass().getResource("contents.html");
tp.setPage(url);

You can react on links within the ScrollPane using the HyperlinkListener. There is a more sufisticated tutorial on this on http://www.java2s.com/Tutorial/Java/0240__Swing/HyperlinkListenerExample.htm

Happy coding :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.