(예제2)
public class SwingBasic_01 extends JFrame{
public SwingBasic_01(String title) {
super(title);
//시작위치는 무조건 this.
this.setBounds(50, 100, 400, 500); //시작지점,너비,높이
//종료시 처리할 코드가 있으면 이벤트 핸들러를 만든다
//핸들러란 이벤트 발생시 호출되는 메서드
//익명내부클래스로 해보기(방법은 여러가지): 가독성이 좋고 구현이 편하나 코드가 긴 단점.
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
System.exit(0);
}
});
//배경색 변경
this.getContentPane().setBackground(new Color(191,240,200)); //숫자는 ~256
//프레임을 보이게
this.setVisible(true);
}
public static void main(String[] args) {
new SwingBasic_01("스윙기본연습");
}
}
(예제2)
public class SwingBasic_02 extends JFrame{
Container cp; //컨테이너라는 클래스 생성
public SwingBasic_02(String title) {
super(title);
cp=this.getContentPane();
//내부클래스 호출
// this.addWindowListener(new winclose());
//메인프레임 종료될 때 모든 프로그램도 종료
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//익명클래스 만들고 내부클래스 만들어서 할 필요 없음
this.setBounds(200,100,400,500); //사이즈
cp.setBackground(new Color(255,255,200)); //백그라운드 색
this.setVisible(true); //보이게 하기
}
//내부클래스 이용해서 윈도우 클래스 구현
//class winclose extends WindowAdapter{
//
// @Override
// public void windowClosing(WindowEvent e) {
// // TODO Auto-generated method stub
// super.windowClosing(e);
// System.exit(0);
// }
// }
public static void main(String[] args) {
new SwingBasic_02("스윙기본연습2");
}
}
'JAVA' 카테고리의 다른 글
210823_swing(버튼) (0) | 2021.08.23 |
---|---|
210823_swing+layout(버튼 생성) (0) | 2021.08.23 |
210823_Array+List (0) | 2021.08.23 |
210810_Vector (0) | 2021.08.20 |
210820_Map (0) | 2021.08.20 |