class에 extends JFrame
JFrame 안될 경우 module 있는지 확인하고 삭제
생성자에 다 만들어줘야함
메서드를 따로 만들어줘서 생성자에 호출해줘야함
this.setBounds(500, 100, 500, 500); //this. 는 JFrame이라고 생각
->시작위치, 너비, 높이
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
->종료
this.setBackground(new Color(190,242,200));
==this.setBackground(Color.MAGENTA);
->this는 frame을 가르키는건데 frame 위에 막이 하나 더있어서 색변경x
그래서 컨테이너를 가져와야함
컨테이너 가져오는 법->this.getContentPane().setBackground(Color.cyan);
frame 보이는 법
-this.setVisible(true);
메인에서는 new 생성자메서드명(String값); 생성자 호출만
//class에 extends JFrame
//JFrame 안될 경우 module 있는지 확인하고 삭제
public class SwingGibon_07 extends JFrame{
//생성자에 다 만들어줘야함
//메서드를 따로 만들어줘도 생성자에 호출해줘야함
public SwingGibon_07(String title) {
super(title);
//시작위치, 너비, 높이
this.setBounds(500, 100, 500, 500); //this. 는 JFrame이라고 생각
//종료
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//배경색
//==this.setBackground(Color.MAGENTA);
//this.setBackground(new Color(190,242,200));
//this는 frame을 가르키는건데 frame 위에 막이 하나 더있어서 색변경x
//그래서 컨테이너 가져와야함
this.getContentPane().setBackground(Color.cyan);
//frame 보이게
this.setVisible(true);
}
public static void main(String[] args) {
new SwingGibon_07("스윙기본");//생성자 호출만
}
}
결과화면
전역변수에 선언
Container cp;-> 컨테이너 선언
JButton btn1;-> 버튼 생성/J붙은건 거의 다 JFrame
컨테이너 전역변수선언해서 색표현 가능
cp=this.getContentPane();
cp.setBackground(new Color(255,255,200)); -> ()rgb색
버튼생성
변수명=new JButton("버튼이름");
btn1=new JButton("버튼1");
frame에 버튼 추가
frame 기본 레이아웃이 BorderLayout(동서남북)
BorderLayout추가시 위치 지정
this.add(btn1, BorderLayout.NORTH);//위쪽
this.add(btn1, BorderLayout.SOUTH);//아래쪽
this.add("North", btn1); //첫글자 대문자로
생성과 동시에 프레임 추가 위치지정
this.add("South",new JButton("아래쪽"));
this.add("West",new JButton("왼쪽"));
this.add("East",new JButton("오른쪽"));
this.add("Center",new JButton("가운데"));
버튼1에 속성
btn1.setBackground(Color.cyan); //색깔넣기
btn1.setForeground(Color.white); //글자색
디자인... 나중에 많아지면 따로 만들어서 오버로딩이나 extends
액션 .. visible위에
public class SwingLayout_08 extends JFrame {
//전역변수
Container cp;
JButton btn1; //버튼 생성
//J붙은건 거의 다 JFrame
public SwingLayout_08(String title) {
super(title);
this.setBounds(200, 100, 300, 500);
//컨테이너 전역변수선언해서 색표현 가능
cp=this.getContentPane();
cp.setBackground(new Color(255,255,200));
//버튼생성
btn1=new JButton("버튼1");
//frame에 버튼 추가
//frame 기본 레이아웃이 BorderLayout(동서남북)
//BorderLayout추가시 위치 지정
//this.add(btn1, BorderLayout.NORTH);//위쪽
//this.add(btn1, BorderLayout.SOUTH);//아래쪽
this.add("North", btn1); //첫글자 대문자로
//생성과 동시에 프레임 추가 위치지정
this.add("South",new JButton("아래쪽"));
this.add("West",new JButton("왼쪽"));
this.add("East",new JButton("오른쪽"));
this.add("Center",new JButton("가운데"));
//버튼1에 속성
btn1.setBackground(Color.cyan); //색깔넣기
btn1.setForeground(Color.white); //글자색
this.setVisible(true);
}
//디자인... 나중에 많아지면 따로 만들어서 오버로딩이나 extends
//액션 .. visible위에
public static void main(String[] args) {
// TODO Auto-generated method stub
new SwingLayout_08("스윙 레이아웃 연습");
}
}
결과화면