| | |
help!! My applet can't run... Urgent!!
![]() |
•
•
Join Date: Apr 2003
Posts: 2
Reputation:
Solved Threads: 0
I can't seem to get my applet running...Pls help me look at my source code:
import java.awt.*;
import javax.swing.*;
import java.applet.*;
public class ChineseHoroApplet extends JApplet {
protected final static String names[] = { "Year of Birth",
"Month of Birth", "Day of Birth" };
protected JLabel labels[];
protected JTextField fields[];
protected JButton doTask1;
protected JPanel innerPanelCenter, innerPanelSouth;
protected int size;
public static final int YEAR= 0, MONTH = 1,
DAY = 2;
public ChineseHoroApplet( int mySize )
{
size = mySize;
labels = new JLabel[ size ];
fields = new JTextField[ size ];
// create labels
for ( int count = 0; count < labels.length; count++ )
labels[ count ] = new JLabel( names[ count ] );
// create text fields
for ( int count = 0; count < fields.length; count++ )
fields[ count ] = new JTextField();
// create panel to lay out labels and fields
innerPanelCenter = new JPanel();
innerPanelCenter.setLayout( new GridLayout( size, 2 ) );
// attach labels and fields to innerPanelCenter
for ( int count = 0; count < size; count++ ) {
innerPanelCenter.add( labels[ count ] );
innerPanelCenter.add( fields[ count ] );
}
// create generic buttons; no labels or event handlers
doTask1 = new JButton();
// create panel to lay out buttons and attach buttons
innerPanelSouth = new JPanel();
innerPanelSouth.add( doTask1 );
// set layout of this container and attach panels to it
setLayout( new BorderLayout() );
add( innerPanelCenter, BorderLayout.CENTER );
add( innerPanelSouth, BorderLayout.SOUTH );
// validate layout
validate();
} // end constructor
// return reference to generic task button doTask1
public JButton getDoTask1Button()
{
return doTask1;
}
// return reference to fields array of JTextFields
public JTextField[] getFields()
{
return fields;
}
// clear content of text fields
public void clearFields()
{
for ( int count = 0; count < size; count++ )
fields[ count ].setText( "" );
}
// set text field values; throw IllegalArgumentException if
// incorrect number of Strings in argument
public void setFieldValues( String strings[] )
throws IllegalArgumentException
{
if ( strings.length != size )
throw new IllegalArgumentException( "There must be " +
size + " Strings in the array" );
for ( int count = 0; count < size; count++ )
fields[ count ].setText( strings[ count ] );
}
// get array of Strings with current text field contents
public String[] getFieldValues()
{
String values[] = new String[ size ];
for ( int count = 0; count < size; count++ )
values[ count ] = fields[ count ].getText();
return values;
}
} // end class ChineseHoroApplet
HTMl CODE:
<HTML><APPLET CODE = "ChineseHoroApplet.class" WIDTH=200 HEIGHT=300>
</APPLET></HTML>
import java.awt.*;
import javax.swing.*;
import java.applet.*;
public class ChineseHoroApplet extends JApplet {
protected final static String names[] = { "Year of Birth",
"Month of Birth", "Day of Birth" };
protected JLabel labels[];
protected JTextField fields[];
protected JButton doTask1;
protected JPanel innerPanelCenter, innerPanelSouth;
protected int size;
public static final int YEAR= 0, MONTH = 1,
DAY = 2;
public ChineseHoroApplet( int mySize )
{
size = mySize;
labels = new JLabel[ size ];
fields = new JTextField[ size ];
// create labels
for ( int count = 0; count < labels.length; count++ )
labels[ count ] = new JLabel( names[ count ] );
// create text fields
for ( int count = 0; count < fields.length; count++ )
fields[ count ] = new JTextField();
// create panel to lay out labels and fields
innerPanelCenter = new JPanel();
innerPanelCenter.setLayout( new GridLayout( size, 2 ) );
// attach labels and fields to innerPanelCenter
for ( int count = 0; count < size; count++ ) {
innerPanelCenter.add( labels[ count ] );
innerPanelCenter.add( fields[ count ] );
}
// create generic buttons; no labels or event handlers
doTask1 = new JButton();
// create panel to lay out buttons and attach buttons
innerPanelSouth = new JPanel();
innerPanelSouth.add( doTask1 );
// set layout of this container and attach panels to it
setLayout( new BorderLayout() );
add( innerPanelCenter, BorderLayout.CENTER );
add( innerPanelSouth, BorderLayout.SOUTH );
// validate layout
validate();
} // end constructor
// return reference to generic task button doTask1
public JButton getDoTask1Button()
{
return doTask1;
}
// return reference to fields array of JTextFields
public JTextField[] getFields()
{
return fields;
}
// clear content of text fields
public void clearFields()
{
for ( int count = 0; count < size; count++ )
fields[ count ].setText( "" );
}
// set text field values; throw IllegalArgumentException if
// incorrect number of Strings in argument
public void setFieldValues( String strings[] )
throws IllegalArgumentException
{
if ( strings.length != size )
throw new IllegalArgumentException( "There must be " +
size + " Strings in the array" );
for ( int count = 0; count < size; count++ )
fields[ count ].setText( strings[ count ] );
}
// get array of Strings with current text field contents
public String[] getFieldValues()
{
String values[] = new String[ size ];
for ( int count = 0; count < size; count++ )
values[ count ] = fields[ count ].getText();
return values;
}
} // end class ChineseHoroApplet
HTMl CODE:
<HTML><APPLET CODE = "ChineseHoroApplet.class" WIDTH=200 HEIGHT=300>
</APPLET></HTML>
•
•
Join Date: Jan 2004
Posts: 15
Reputation:
Solved Threads: 0
ok first thing is that any code that is run must go through the public void
init() method needs to be overridden inorder for your changes to take affect so
the GUI code must go in the public void init() method that u define, which u must.
then check your array indexes ok. too cause i got an array index out of bounds exception when u run it ok.
init() method needs to be overridden inorder for your changes to take affect so
the GUI code must go in the public void init() method that u define, which u must.
then check your array indexes ok. too cause i got an array index out of bounds exception when u run it ok.
there seems to be some bad code although I only caught one thing that really stood out.
public JTextField[] getFields()
{
return fields;
}
your comment said you're returning to the text field but your return type is an array. It doesn't make sense. that should be done with a loop. although I may have missed something and be way off base. what is this applet supposed to do?
public JTextField[] getFields()
{
return fields;
}
your comment said you're returning to the text field but your return type is an array. It doesn't make sense. that should be done with a loop. although I may have missed something and be way off base. what is this applet supposed to do?
•
•
Join Date: Jan 2004
Posts: 15
Reputation:
Solved Threads: 0
import java.awt.*;
import javax.swing.*;
//import java.applet.*;
public class ChineseHoroApplet extends JApplet {
protected final static String names[] = { "Year of Birth",
"Month of Birth", "Day of Birth" };
protected JLabel labels[];
protected JTextField fields[];
protected JButton doTask1;
protected JPanel innerPanelCenter, innerPanelSouth;
protected int size;
public static final int YEAR= 0, MONTH = 1,
DAY = 2;
public void init()
{
size = 3;
labels = new JLabel[ size ];
fields = new JTextField[ size ];
// create labels
for ( int count = 0; count < labels.length; count++ )
labels[ count ] = new JLabel( names[ count ] );
// create text fields
for ( int count = 0; count < fields.length; count++ )
fields[ count ] = new JTextField();
// create panel to lay out labels and fields
innerPanelCenter = new JPanel();
innerPanelCenter.setLayout( new GridLayout( size, 2 ) );
// attach labels and fields to innerPanelCenter
for ( int count = 0; count < size; count++ ) {
innerPanelCenter.add( labels[ count ] );
innerPanelCenter.add( fields[ count ] );
}
// create generic buttons; no labels or event handlers
doTask1 = new JButton();
// create panel to lay out buttons and attach buttons
innerPanelSouth = new JPanel();
innerPanelSouth.add( doTask1 );
// set layout of this container and attach panels to it
//setLayout( new BorderLayout() );
super.getContentPane().add( innerPanelCenter);
super.getContentPane().add( innerPanelSouth);
// validate layout
validate();
} // end constructor
// return reference to generic task button doTask1
public JButton getDoTask1Button()
{
return doTask1;
}
// return reference to fields array of JTextFields
public JTextField[] getFields()
{
return fields;
}
// clear content of text fields
public void clearFields()
{
for ( int count = 0; count < size; count++ )
fields[ count ].setText( "" );
}
// set text field values; throw IllegalArgumentException if
// incorrect number of Strings in argument
public void setFieldValues( String strings[] )
throws IllegalArgumentException
{
if ( strings.length != size )
throw new IllegalArgumentException( "There must be " +
size + " Strings in the array" );
for ( int count = 0; count < size; count++ )
fields[ count ].setText( strings[ count ] );
}
// get array of Strings with current text field contents
public String[] getFieldValues()
{
String values[] = new String[ size ];
for ( int count = 0; count < size; count++ )
values[ count ] = fields[ count ].getText();
return values;
}
} // end class ChineseHoroApplet
now applets come defaulted to border layout so there is no need for that, and
u must get the content pane first before u can add components to an applet
and u can't instantiate an applet u was missing the init() method which runs
the applet for u.
now i got the applet running but now its ur turn to update it to what u want it to do ok
lots of luck.
import javax.swing.*;
//import java.applet.*;
public class ChineseHoroApplet extends JApplet {
protected final static String names[] = { "Year of Birth",
"Month of Birth", "Day of Birth" };
protected JLabel labels[];
protected JTextField fields[];
protected JButton doTask1;
protected JPanel innerPanelCenter, innerPanelSouth;
protected int size;
public static final int YEAR= 0, MONTH = 1,
DAY = 2;
public void init()
{
size = 3;
labels = new JLabel[ size ];
fields = new JTextField[ size ];
// create labels
for ( int count = 0; count < labels.length; count++ )
labels[ count ] = new JLabel( names[ count ] );
// create text fields
for ( int count = 0; count < fields.length; count++ )
fields[ count ] = new JTextField();
// create panel to lay out labels and fields
innerPanelCenter = new JPanel();
innerPanelCenter.setLayout( new GridLayout( size, 2 ) );
// attach labels and fields to innerPanelCenter
for ( int count = 0; count < size; count++ ) {
innerPanelCenter.add( labels[ count ] );
innerPanelCenter.add( fields[ count ] );
}
// create generic buttons; no labels or event handlers
doTask1 = new JButton();
// create panel to lay out buttons and attach buttons
innerPanelSouth = new JPanel();
innerPanelSouth.add( doTask1 );
// set layout of this container and attach panels to it
//setLayout( new BorderLayout() );
super.getContentPane().add( innerPanelCenter);
super.getContentPane().add( innerPanelSouth);
// validate layout
validate();
} // end constructor
// return reference to generic task button doTask1
public JButton getDoTask1Button()
{
return doTask1;
}
// return reference to fields array of JTextFields
public JTextField[] getFields()
{
return fields;
}
// clear content of text fields
public void clearFields()
{
for ( int count = 0; count < size; count++ )
fields[ count ].setText( "" );
}
// set text field values; throw IllegalArgumentException if
// incorrect number of Strings in argument
public void setFieldValues( String strings[] )
throws IllegalArgumentException
{
if ( strings.length != size )
throw new IllegalArgumentException( "There must be " +
size + " Strings in the array" );
for ( int count = 0; count < size; count++ )
fields[ count ].setText( strings[ count ] );
}
// get array of Strings with current text field contents
public String[] getFieldValues()
{
String values[] = new String[ size ];
for ( int count = 0; count < size; count++ )
values[ count ] = fields[ count ].getText();
return values;
}
} // end class ChineseHoroApplet
now applets come defaulted to border layout so there is no need for that, and
u must get the content pane first before u can add components to an applet
and u can't instantiate an applet u was missing the init() method which runs
the applet for u.
now i got the applet running but now its ur turn to update it to what u want it to do ok
lots of luck.
![]() |
Similar Threads
- simple java applet won't run on eclipse (Java)
- Urgent help on Applet (Java)
- Do I have to sign my applet to run it with fullscreen? (Java)
Other Threads in the Java Forum
- Previous Thread: Arrays???
- Next Thread: Need help with looping some sounds
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking bidirectional binary binarytree birt bluetooth chatprogramusingobjects class client code columns component database derby design designadrawingapplicationusingjavajslider eclipse encryption error errors expand fractal game givemetehcodez graphics gui guidancer homework html ide if_statement image inheritance integer intellij j2me java javadesktopapplications javaprojects jlabel jme jni jpanel jtextfield julia linux list map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source problem program project property recursion reference ria scanner search server set sms smsspam sort sourcelabs splash sql sqlite static stop string subclass support swing testautomation threads tree ui unicode validation windows





