Java Beginners

Moderators: zibadian
Number of threads: 1233
Number of posts: 2675

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How to add a JApplet into a JFrame Posted by ColacX on 23 May 2008 at 4:16 AM
Been looking at some examples on the internet but cant make them work.
I tried this. SSF is an JApplet. Anything I'm missing?
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);
    }

Report
Re: How to add a JApplet into a JFrame Posted by zibadian on 23 May 2008 at 7:12 AM
: Been looking at some examples on the internet but cant make them
: work.
: I tried this. SSF is an JApplet. Anything I'm missing?
:
: 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);
:     }
:
:
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)
Report
Re: How to add a JApplet into a JFrame Posted by ColacX on 23 May 2008 at 8:53 AM
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.
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"));
    }
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.
Report
Re: How to add a JApplet into a JFrame Posted by zibadian on 23 May 2008 at 9:40 AM
: 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.
:
: 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"));
:     }
: 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:
  f.add(ssf, BorderLayout.CENTER);

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.
Report
Re: How to add a JApplet into a JFrame Posted by zibadian on 23 May 2008 at 10:12 AM
: : 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.
: :
: : 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"));
: :     }
: : 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:
:
: 
:   f.add(ssf, BorderLayout.CENTER);
: 
:
: 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:
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"));
	}

}
Report
Re: How to add a JApplet into a JFrame Posted by ColacX on 23 May 2008 at 11:02 AM
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.
Report
Re: How to add a JApplet into a JFrame Posted by zibadian on 23 May 2008 at 11:48 AM
: 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).
Report
Re: How to add a JApplet into a JFrame Posted by ColacX on 23 May 2008 at 4:28 PM
: 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.

Report
Re: How to add a JApplet into a JFrame Posted by zibadian on 23 May 2008 at 7:09 PM
: : 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.
 
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;
	}

}

There's also an Integer object, but that's immutable, so you cannot use that for encapsulation.
Report
This post has been deleted. Posted by ColacX on 24 May 2008 at 6:00 AM
This post has been deleted.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.