package ch06;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Mframe extends Frame {
public Mframe() {
setSize(300,300);
setBackground(Color.ORANGE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
}
}
package ch06;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyFrame {
public static void main(String[] args) {
Frame f=new Frame();
f.setSize(300, 300);
f.setVisible(true);
Mframe m=new Mframe();
}
}
package ch06;
class SuperClass4{
//super 라는 키워드 부모를 지칭하는 것.
//super()는 반드시 첫번째 라인에 삽입(생략)
// public SuperClass4() {
// }
public SuperClass4(int i) {
//super();
System.out.println("오류");
}
}
class SubClass4 extends SuperClass4{
public SubClass4() {
super(22);//생략: 반드시 생성자의 첫번째 라인
System.out.println("Sub 생성자");
}
}
public class ConstructorEx4 {
public static void main(String[] args) {
SubClass4 s1=new SubClass4();
}
}
package ch06;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.security.DomainCombiner;
public class MFrame extends Frame {
public MFrame() {
this(300,300,new Color(220,220,220),false);
}
public MFrame(int w, int h) {
this(h,w,new Color(220,220,220),false);
}
public MFrame(Color c) {
this(300,300,c,false);
}
public MFrame(int w, int h, Color c, boolean flag) {
setSize(w,h);
setBackground(c);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setResizable(flag);
setVisible(true);
}
public static void main(String []args) {
//new MFrame();
//new MFrame(Color.cyan);
new MFrame(500,500,new Color(100,200,100),true);
}
}
package ch06;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class MyFrame extends MFrame{
Random r;
public MyFrame() {
super(500, 500, Color.WHITE,false);
r = new Random();
}
public void paint(Graphics g){
if(r==null)
return;
// g.drawString("반갑습니다", 50, 50);
for (int i = 0; i < 10000; i++) {
g.setColor(rColor());//붓에 노란색 물감 셋팅
int x=r.nextInt(500);
int y=r.nextInt(500);
int w=r.nextInt(10)+5;
int h=r.nextInt(10)+5;
// g.drawRect(x,y,w,h);
g.fillRect(x,y,w,h);
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
} //--for
} //--paint
public Color rColor(){
int rr = r.nextInt(256);
int gg = r.nextInt(256);
int bb = r.nextInt(256);
return new Color(rr,gg,bb);
}
public static void main(String[] args) {
new MyFrame();
}
}
this
-> 자신의 생성자
-> 인스턴스 변수(필드)를 지칭
-> 자신의 객체 주소값 or 자신객체
package ch06;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ThisEx2 extends MFrame
implements ActionListener{
Button btn;
public ThisEx2() {
super(500,350,Color.green,true);
btn=new Button("my Button");
add(btn,"South");
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
//System.out.println("보이나요?");
MDialog md=new MDialog(this, "보이나요");
}
class MDialog extends Dialog
implements ActionListener{
Button mbtn;
ThisEx2 f;
public MDialog(ThisEx2 f,String title) {
super(f,title,true);
this.f=f;
setLayout(new FlowLayout());
setSize(150,100);
mbtn=new Button("Click!");
mbtn.addActionListener(this);
add(mbtn);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
f.btn.setBackground(Color.red);
dispose();
}
}
public static void main(String[] args) {
new ThisEx2();
}
}