Assistance needed on this code, please
807607Nov 9 2006 — edited Nov 9 2006The code below is an example program:
/* package demo.swing; */
import javax.swing.JFrame;
/**
* Hello World application
* @author MAbernethy
*/
public class HelloWorld extends JFrame
{
private javax.swing.JLabel jLabel = null;
private javax.swing.JTextField jTextField = null;
private javax.swing.JButton jButton = null;
public static void main(String[] args)
{
HelloWorld w = new HelloWorld();
w.setVisible(true);
}
public HelloWorld()
{
super();
this.setSize(300, 200);
this.getContentPane().setLayout(null);
this.add(getJLabel(), null);
this.add(getJTextField(), null);
this.add(getJButton(), null);
this.setTitle("Hello World");
}
private javax.swing.JLabel getJLabel() {
if(jLabel == null) {
jLabel = new javax.swing.JLabel();
jLabel.setBounds(34, 49, 53, 18);
jLabel.setText("Name:");
}
return jLabel;
}
private javax.swing.JTextField getJTextField() {
if(jTextField == null) {
jTextField = new javax.swing.JTextField();
jTextField.setBounds(96, 49, 160, 20);
}
return jTextField;
}
private javax.swing.JButton getJButton() {
if(jButton == null) {
jButton = new javax.swing.JButton();
jButton.setBounds(103, 110, 71, 27);
jButton.setText("OK");
}
return jButton;
}
}
The code compiles without any probems,but when run the following error messsages appear:
Exception in thread "main" java.lang.Error: Donot use HelloWorld.add() use HelloWorld.getContentPane().add() instead
at javax.swing.Jframe.creatRootPaneException(Jframe.java:465)
at javax.swing.Jframe.addImpl(Jframe.java:491)
at java.awt.Container.add(Container.java:518)
at HelloWorld.<init>(HelloWorld.java:26)
at HelloWorld.main(HelloWorld.java:17)