Hi, it's my first post here..
I have a question
CODE
import javax.swing.*;
import java.awt.*;
public class test extends JFrame {
public static void main(String args[]) {
new test();
}
public test(){
setSize(500, 500);
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension d=tk.getScreenSize();
setLocation(d.width/2-this.getWidth()/2, d.height/2-this.getHeight()/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private Toolkit tk=Toolkit.getDefaultToolkit();
private Image img=tk.getImage("scorpion.jpg");
public void paint(Graphics g){
g.drawImage(img, 50, 50, this);
}
}
Why can't i just put all the contents of public test() into public static void main(String args[])?
here is my attempt
CODE
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame {
public static void main(String args[]) {
JFrame theFrame = new JFrame();
theFrame.setSize(500, 500);
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension d=tk.getScreenSize();
theFrame.setLocation(d.width/2-theFrame.getWidth()/2, d.height/2-theFrame.getHeight()/2);
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theFrame.setVisible(true);
}
private Toolkit tk=Toolkit.getDefaultToolkit();
private Image img=tk.getImage("werd.jpg");
public void paint(Graphics g){
g.drawImage(img, 50, 50, this);
}
}
I ask this because I've been trying to learn to program and I don't understand why the first one works and the second one doesn't.. isn't it basically the same?
This post has been edited by Rose_B: 12 Mar, 2008 - 10:30 PM