Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to add a JApplet into a JFrame

ColacXColacX Posts: 141Member
Been looking at some examples on the internet but cant make them work.
I tried this. SSF is an JApplet. Anything I'm missing?
[code]public static void main(String arg[]){
JFrame f=new JFrame();
f.setSize(1200,700);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);

SSF ssf=new SSF();
ssf.setSize(1200,700);

f.add(ssf);
}[/code]

Comments

  • zibadianzibadian Posts: 6,349Member
    : Been looking at some examples on the internet but cant make them
    : work.
    : I tried this. SSF is an JApplet. Anything I'm missing?
    : [code]: public static void main(String arg[]){
    : JFrame f=new JFrame();
    : f.setSize(1200,700);
    : f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    : f.setVisible(true);
    :
    : SSF ssf=new SSF();
    : ssf.setSize(1200,700);
    :
    : f.add(ssf);
    : }[/code]:
    :
    Try to specify where you want the JApplet to be placed on the frame. By default JFrames use the BorderLayout.
    For more information about the BorderLayout see:
    http://java.sun.com/javase/6/docs/api/java/awt/BorderLayout.html
    For more information on the options of JFrame.add() see:
    http://java.sun.com/javase/6/docs/api/java/awt/Container.html#add(java.awt.Component,%20java.lang.Object)
  • ColacXColacX Posts: 141Member
    Changed the code to this i only see a button now.
    Is there something special i have to write in the JApplet? Note that the source code for the JApplet is rather long but I'll post it if its necessary.
    [code]public static void main(String arg[]){
    JFrame f=new JFrame();
    f.setSize(1200,700);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    f.setLayout(new GridLayout(2,2));
    SSF ssf=new SSF();
    ssf.setSize(1200,700);

    f.add(ssf);
    f.add(new JButton("Yoho"));
    }[/code]A side question how to make a pointer in java?
    I have a int shipx;
    that i want to give to x;
    and i want x to point to shipx and this change it value whenever shipx does. Thanks for the help.
  • zibadianzibadian Posts: 6,349Member
    : Changed the code to this i only see a button now.
    : Is there something special i have to write in the JApplet? Note that
    : the source code for the JApplet is rather long but I'll post it if
    : its necessary.
    : [code]: public static void main(String arg[]){
    : JFrame f=new JFrame();
    : f.setSize(1200,700);
    : f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    : f.setVisible(true);
    : f.setLayout(new GridLayout(2,2));
    : SSF ssf=new SSF();
    : ssf.setSize(1200,700);
    :
    : f.add(ssf);
    : f.add(new JButton("Yoho"));
    : }[/code]: A side question how to make a pointer in java?
    : I have a int shipx;
    : that i want to give to x;
    : and i want x to point to shipx and this change it value whenever
    : shipx does. Thanks for the help.

    Change the add() code to include the constraints of where the component needs to be placed. Thus:
    [code]
    f.add(ssf, BorderLayout.CENTER);
    [/code]
    To have the applet fill the frame. You don't need any additional code changes in the applet.

    There are no pointers in Java. It is possible to encapsule the int value into an Integer object. Then you can reference that object, because objects are accessed by reference not by value.
  • zibadianzibadian Posts: 6,349Member
    : : Changed the code to this i only see a button now.
    : : Is there something special i have to write in the JApplet? Note that
    : : the source code for the JApplet is rather long but I'll post it if
    : : its necessary.
    : : [code]: : public static void main(String arg[]){
    : : JFrame f=new JFrame();
    : : f.setSize(1200,700);
    : : f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    : : f.setVisible(true);
    : : f.setLayout(new GridLayout(2,2));
    : : SSF ssf=new SSF();
    : : ssf.setSize(1200,700);
    : :
    : : f.add(ssf);
    : : f.add(new JButton("Yoho"));
    : : }[/code]: : A side question how to make a pointer in java?
    : : I have a int shipx;
    : : that i want to give to x;
    : : and i want x to point to shipx and this change it value whenever
    : : shipx does. Thanks for the help.
    :
    : Change the add() code to include the constraints of where the
    : component needs to be placed. Thus:
    : [code]:
    : f.add(ssf, BorderLayout.CENTER);
    : [/code]:
    : To have the applet fill the frame. You don't need any additional
    : code changes in the applet.
    :
    : There are no pointers in Java. It is possible to encapsule the int
    : value into an Integer object. Then you can reference that object,
    : because objects are accessed by reference not by value.
    :
    Here's a working example of a JApplet in a JFrame:
    [code]
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.HeadlessException;

    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JFrame;

    public class FrameTest extends JFrame {

    public FrameTest() throws HeadlessException {
    super();
    Test t = new Test();
    t.init();
    add(t, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
    FrameTest ft = new FrameTest();
    ft.setSize(800, 600);
    ft.setVisible(true);
    }

    }

    class Test extends JApplet {

    @Override
    public void init() {
    super.init();
    setLayout(new FlowLayout());
    add(new JButton("Hello World"));
    }

    }
    [/code]
  • ColacXColacX Posts: 141Member
    Uh it does seem to work however somethings like get documentbase() or createImage() wont work in a JFrame this means i have to change my code. Thanks anyway.

    : There are no pointers in Java. It is possible to encapsule the int
    : value into an Integer object. Then you can reference that object,
    : because objects are accessed by reference not by value.
    Please give me an example that. Thanks.
  • zibadianzibadian Posts: 6,349Member
    : Uh it does seem to work however somethings like get documentbase()
    : or createImage() wont work in a JFrame this means i have to change
    : my code. Thanks anyway.
    :
    : : There are no pointers in Java. It is possible to encapsule the int
    : : value into an Integer object. Then you can reference that object,
    : : because objects are accessed by reference not by value.
    : Please give me an example that. Thanks.

    Just a tip for your next project:
    I've had similar problems before. I've solved them by making my applet/application code in a JPanel. Then write a JFrame and a JApplet around it. That way the application code doesn't need to change. The application JPanel should defer all possibly restricted operations to its container. For example: File-I/O should be part of the JFrame and JApplet. This way the JFrame version can perform the file-I/O and the JApplet can give a custom message (unless it is allowed to perform file-I/O).
  • ColacXColacX Posts: 141Member
    : Just a tip for your next project:
    : I've had similar problems before. I've solved them by making my
    : applet/application code in a JPanel. Then write a JFrame and a
    : JApplet around it. That way the application code doesn't need to
    : change. The application JPanel should defer all possibly restricted
    : operations to its container. For example: File-I/O should be part of
    : the JFrame and JApplet. This way the JFrame version can perform the
    : file-I/O and the JApplet can give a custom message (unless it is
    : allowed to perform file-I/O).
    Sounds like a plan :D

    : : There are no pointers in Java. It is possible to encapsule the int
    : : value into an Integer object. Then you can reference that object,
    : : because objects are accessed by reference not by value.
    : Please give me an example that. Thanks.
    Still waiting for that example thanks.

  • zibadianzibadian Posts: 6,349Member
    : : Just a tip for your next project:
    : : I've had similar problems before. I've solved them by making my
    : : applet/application code in a JPanel. Then write a JFrame and a
    : : JApplet around it. That way the application code doesn't need to
    : : change. The application JPanel should defer all possibly restricted
    : : operations to its container. For example: File-I/O should be part of
    : : the JFrame and JApplet. This way the JFrame version can perform the
    : : file-I/O and the JApplet can give a custom message (unless it is
    : : allowed to perform file-I/O).
    : Sounds like a plan :D
    :
    : : : There are no pointers in Java. It is possible to encapsule the int
    : : : value into an Integer object. Then you can reference that object,
    : : : because objects are accessed by reference not by value.
    : : Please give me an example that. Thanks.
    : Still waiting for that example thanks.
    :
    :
    Here's an example of integer encapsulation.
    [code]
    public class ObjectEncapsExample {

    public static void main(String[] args) {
    int i = 2;
    change1(i); // pass by value
    System.out.println(i); // unchanged
    MyInteger j = new MyInteger(2);
    change2(j); // pass by reference
    System.out.println(j); // changed
    }

    private static void change2(MyInteger aJ) {
    aJ.setValue(100);

    }

    private static void change1(int aI) {
    aI = 100;
    }
    }

    class MyInteger {
    private int value;

    public MyInteger(int aValue) {
    super();
    value = aValue;
    }

    public final int getValue() {
    return value;
    }

    public final void setValue(int aValue) {
    value = aValue;
    }

    @Override
    public String toString() {
    return "" + value;
    }

    }
    [/code]
    There's also an Integer object, but that's immutable, so you cannot use that for encapsulation.
  • ColacXColacX Posts: 141Member
    This post has been deleted.
Sign In or Register to comment.