-
error in code ...
The following code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HelloBa {
HelloBa () {
JFrame f = new JFrame("Hello Ba ");
f.setSize(300,300);
f.getContentPane().add(this);
f.setVisible(true);
}
public void paintComponent(Graphics g) {
g.drawString("What up Ba!",125,90);
}
public static void main(String[] args) {
new HelloBa ();
}
}
gives me the error: "cannot resolve symbol method add (HelloBa)". Does anyone have a solution to this problem?
Thanks
-
I think if you leave out this line it will work..
f.getContentPane().add(this);
-
Yes, but it won't do anything.
You need to derive from some class, probably JPanel.
-
sample
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HelloBa extends JPanel{
public HelloBa(){
JFrame f=new JFrame("Hello Ba");
f.setSize(300,300);
f.getContentPane().add(this);
f.setVisible(true);
}
public static void main(String[] args){
new HelloBa();
}
}
-
All of the add() methods in java.awt.Container take Component refs.