J
Jenny
Hi,
Create new class vs extends a class - Could you tell me which way
should I use?
Here are two pieces of code. They do the same thing.
1. The code is from the link
http://www.cadenhead.org/book/java24hours/source/chapter13/ClockFrame.java
import java.awt.*;
import javax.swing.*;
public class ClockFrame extends JFrame {
public ClockFrame() {
super("Clock");
setSize(225, 125);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
FlowLayout flo = new FlowLayout();
pane.setLayout(flo);
ClockPanel time = new ClockPanel();
pane.add(time);
setContentPane(time);
setVisible(true);
}
public static void main(String[] arguments) {
ClockFrame sal = new ClockFrame();
}
}
2. I wrote this code to compare.
import java.awt.*;
import javax.swing.*;
public class Windows {
public static void main(String[] args) {
JFrame f = new JFrame("The Frame");
f.setSize(300, 300);
f.setLocation(100, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
Container pane = f.getContentPane();
FlowLayout flo = new FlowLayout();
pane.setLayout(flo);
ClockPanel time = new ClockPanel();
pane.add(time);
f.setContentPane(time);
f.setVisible(true);
}
}
I think the first one is better. But I am new to Java. I'd like to
get your thoughts.
Thank you a lot.
Jenny
Create new class vs extends a class - Could you tell me which way
should I use?
Here are two pieces of code. They do the same thing.
1. The code is from the link
http://www.cadenhead.org/book/java24hours/source/chapter13/ClockFrame.java
import java.awt.*;
import javax.swing.*;
public class ClockFrame extends JFrame {
public ClockFrame() {
super("Clock");
setSize(225, 125);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
FlowLayout flo = new FlowLayout();
pane.setLayout(flo);
ClockPanel time = new ClockPanel();
pane.add(time);
setContentPane(time);
setVisible(true);
}
public static void main(String[] arguments) {
ClockFrame sal = new ClockFrame();
}
}
2. I wrote this code to compare.
import java.awt.*;
import javax.swing.*;
public class Windows {
public static void main(String[] args) {
JFrame f = new JFrame("The Frame");
f.setSize(300, 300);
f.setLocation(100, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
Container pane = f.getContentPane();
FlowLayout flo = new FlowLayout();
pane.setLayout(flo);
ClockPanel time = new ClockPanel();
pane.add(time);
f.setContentPane(time);
f.setVisible(true);
}
}
I think the first one is better. But I am new to Java. I'd like to
get your thoughts.
Thank you a lot.
Jenny