JApplet Structure Problem

D

d.schulz81

Hi,

I can't run this Japplet in a web browser. But the eclipse applet
viewer works...
I guess it's beacause we havn't implemented the methods init, start,
stop and resize.
Can you post some standard example methods?
Should we move the code from the main method in the init method?

Any help will be appreciated...

Thanks alot

Dennis


package mn;
import java.awt.BorderLayout;

import java.awt.Dimension;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;

public class NewJApplet extends javax.swing.JApplet {
private JPanel jPanel2;
private JPanel jPanel1;
private JPanel jPanel3;
private JPanel jPanel7;
private JLabel jLabel1;
private JLabel jLabel4;
private JPanel jPanel5;
private JLabel jLabel5;
private JTextField jTextField2;
private JSlider jSlider2;
private JPanel jPanel10;
private JLabel jLabel3;
private JPanel jPanel9;
private JCheckBox jCheckBox1;
private JPanel jPanel6;
private JPanel jPanel4;
private JSlider jSlider1;
private JLabel jLabel2;
private JButton OK;
private JTextField jTextField1;
private JButton jb1;
private JButton jb2;

{
//Set Look & Feel
try
{
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch(Exception e) {
e.printStackTrace();
}
}

/**
* Auto-generated main method to display this
* JApplet inside a new JFrame.
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
NewJApplet inst = new NewJApplet();
// inst.setSize(500,500);
frame.getContentPane().add(inst);
//((JComponent)frame.getContentPane()).setPreferredSize(new
java.awt.Dimension(
// 500,
// 500));
// frame.setSize(500,500);
frame.pack();
// ((JComponent)frame.getContentPane()).setPreferredSize(new
java.awt.Dimension(
// 500,
// 500));
// frame.setSize(500,500);
// inst.setSize(500,500);
frame.setVisible(true);
}

public NewJApplet() {
super();
initGUI();
}

private void initGUI() {
try {
BorderLayout thisLayout = new BorderLayout();
getContentPane().setLayout(thisLayout);
//this.setSize(576, 300);
{
jPanel2 = new JPanel();
getContentPane().add(jPanel2, BorderLayout.NORTH);
//jPanel2.setPreferredSize(new java.awt.Dimension(576, 78));
}
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.SOUTH);
BorderLayout jPanel1Layout = new BorderLayout();
jPanel1.setLayout(jPanel1Layout);
//jPanel1.setPreferredSize(new java.awt.Dimension(576, 103));
{
jPanel3 = new JPanel();

BorderLayout jPanel3Layout = new BorderLayout();
jPanel3.setLayout(jPanel3Layout);
//jPanel3.setPreferredSize(new java.awt.Dimension(361, 113));
{
jPanel7 = new JPanel();

jPanel7.setPreferredSize(new java.awt.Dimension(600, 35));
{
jLabel2 = new JLabel();
jPanel7.add(jLabel2);
jLabel2.setText("Signal");
jLabel2.setPreferredSize(new java.awt.Dimension(
58,
17));
}
{
jSlider1 = new JSlider();
jPanel7.add(jSlider1);
}
{
jTextField1 = new JTextField();
jPanel7.add(jTextField1);
jTextField1.setText("1000");
}
{
jLabel1 = new JLabel();
jPanel7.add(jLabel1);
jLabel1.setText("Hz");
}
{
jb1 = new JButton();
jPanel7.add(jb1);
jb1.setText("OK");

}
jPanel3.add(jPanel7, BorderLayout.NORTH);
}

{
jPanel10 = new JPanel();

//jPanel10.setPreferredSize(new java.awt.Dimension(
// 339,
// 35));
{
jLabel4 = new JLabel();
jPanel10.add(jLabel4);
jLabel4.setText("Abtastung");
jLabel4.setPreferredSize(new java.awt.Dimension(
58,
17));
}
{
jSlider2 = new JSlider();
jPanel10.add(jSlider2);
}
{
jTextField2 = new JTextField();
jPanel10.add(jTextField2);
jTextField2.setText("1000");
}
{
jLabel5 = new JLabel();
jPanel10.add(jLabel5);
jLabel5.setText("Hz");
}
{
jb1 = new JButton();
jPanel10.add(jb1);
jb1.setText("OK");

}
jPanel3.add(jPanel10, BorderLayout.SOUTH);
}
jPanel1.add(jPanel3, BorderLayout.WEST);
}
{
jPanel4 = new JPanel();

BorderLayout jPanel4Layout = new BorderLayout();
jPanel4.setLayout(jPanel4Layout);
//jPanel4.setPreferredSize(new java.awt.Dimension(186, 68));
{
jPanel6 = new JPanel();
jPanel4.add(jPanel6, BorderLayout.NORTH);
{
jCheckBox1 = new JCheckBox();
jPanel6.add(jCheckBox1);
jCheckBox1.setText("Abtastungspunkte");
}
}
{
jPanel9 = new JPanel();
jPanel4.add(jPanel9, BorderLayout.CENTER);
//jPanel9
// .setPreferredSize(new java.awt.Dimension(202, 53));
{
jLabel3 = new JLabel();
jPanel9.add(jLabel3);
jLabel3.setText("Abtasttheorem ist erfüllt");
}
{
OK = new JButton();
jPanel9.add(OK);
OK.setText("Reset");
}
}
{
jPanel5 = new JPanel();
jPanel4.add(jPanel5, BorderLayout.WEST);
}
jPanel1.add(jPanel4, BorderLayout.EAST);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

}
 
Z

zero

(e-mail address removed) wrote in
Hi,

I can't run this Japplet in a web browser. But the eclipse applet
viewer works...
I guess it's beacause we havn't implemented the methods init, start,
stop and resize.
Can you post some standard example methods?
Should we move the code from the main method in the init method?

Any help will be appreciated...

Thanks alot

Dennis

I guess this demonstrates my point that you shouldn't use an IDE until
you know how to work without one.

Applets typically don't have a main method - you would only add one if
you want it to run as application as well. This is also (usually) the
reason to create a JFrame in the applet's main method: to provide a place
for the applet to live in.

In this case, you can have an init method like this:

public void init()
{
initGUI();
}

That should be all there is to it.

Then remove the main method if you don't want it to run as application.
If you still want that main method, have a look at
http://www.mindprod.com/jgloss/applet.html#SWITCHHITTER
for a more robust implementation.

Remember when making an applet/application hybrid to always test it as
applet first, because applets are much more restricted - what works in an
application may not necessarily work in an applet.
 
A

Andrew Thompson

zero said:
I guess this demonstrates my point that you shouldn't use an IDE until
you know how to work without one.

[ fully agree.. ]
Remember when making an applet/application hybrid to always test it as
applet first,

Fully disagree. *
..because applets are much more restricted -

Yes they are, but in very predictable and specific ways.
(Roedy has compiled a list at MindProd)
... what works in an
application may not necessarily work in an applet.

If you launch an application using JWS it loses all the
'automatic privileges' that it had as an application
launched from the command line.

Or to put that another way, a signed applet will be able
to do anything an application can do.

* When developing a hybrid applet/application, it is
fastest to test using the application's class files,
the applet generally does not get a separate test till
quite late in the development (possibly once the class
files have been jar'd and signed - though that is most
the case for a hybrid that needs extended priviliges).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top