1

I would like to convert my Simple Java program to an Applet Program. I've looked for different tutorials but all were in general most of it didn't talk about GUI to Applet.

This is my simple program if you could instruct me to do it or comment on the changed lines I would be so much appreciated.

Here is the code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test_Exam_091 {

    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame implements MouseListener {

    public MyFrame() {
        setTitle("Playing With The Mouse!");
        setSize(400, 400);
        setResizable(false);
        setVisible(true);
        addMouseListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        show();
    }

    public void mouseEntered(MouseEvent me) {
        System.out.println("Mouse entered at: ("
            + me.getX() + ", " + me.getY() + ")");
    }

    public void mouseExited(MouseEvent me) {
        System.out.println("Mouse exited at: ("
            + me.getX() + ", " + me.getY() + ")");
    }

    public void mouseClicked(MouseEvent me) {
        System.out.println("Mouse clicked at: ("
            + me.getX() + ", " + me.getY() + ")");
    }

    public void mousePressed(MouseEvent me) {
        System.out.println("Mouse pressed at: ("
            + me.getX() + ", " + me.getY() + ")");
    }

    public void mouseReleased(MouseEvent me) {
        System.out.println("Mouse released at: ("
            + me.getX() + ", " + me.getY() + ")");
    }
} // End of MyFrame class
3
  • 1
    "I would like to convert my Simple Java program to an Applet Program." Don't. Instead launch the program as-is using Java Web Start. Commented Nov 28, 2011 at 20:59
  • We actually have to learn how to edit our GUI programs and convert it to an Applet . I've learned how to do it thanks so some tutorials, but I've learned does always applies to every program. Commented Nov 28, 2011 at 21:26
  • See also this hybrid application/applet. Commented Nov 29, 2011 at 3:02

2 Answers 2

2

JApplet is a descendant of java.awt.Panel that also acts as a Swing container so it can be used almost interchangeably with other containers like JFrame and JPanel.

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

Comments

1

Some tips: Most of the lines of the constructor are irrelevant to applets.

setTitle("Playing With The Mouse!"); // An applet has no title 
    // (Though it might have an ID/name in HTML)
setSize(400, 400);  // Set in HTML
setResizable(false); // An applet is usually fixed size
setVisible(true); // Not relevant to an applet (visible by default)
addMouseListener(this); 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Not allowed in an applet
show(); // Deprecated for components around 1.2, for TLCs around 1.5, ..
    // ..and redundant

In the listener methods..

System.out.println("Mouse pressed at: (" + me.getX() + ", " + me.getY() +")");

Will cause problems, since that output will end up in the Java Console which is generally invisible to the end user (and most developers). It was not particularly appropriate to the JFrame form either. It will be necessary to add a JLabel or similar to the GUI, in which to display the results.

3 Comments

Exactly! I've tried to remove all the JFrame properties but I still have problems.
Oh yeah, I forgot about the System.out.println I think this what was causing the issue, let me try it and will get back to you! thanks!
I've chosen @AndrewThompson as the correct answer. Adding to his answer we have to change JFrame to JApplet and remove the Main Method.

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.