본문 바로가기

PROGRAM/JAVA-SWING

#01 - 시작하기(JFrame)

JFrame을 상속받은 경우

public class JavaExam {
    public static void main(String[] args) {
        new P01_Frame();
    }
}
 
class P01_Frame extends JFrame{
    public P01_Frame() {
        run();
    }
    private void run() {
        setTitle("예제1");
        setBounds(100, 100, 300, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

JFrame을 상속받지 않고 사용한 경우

public class JavaExam {
    public static void main(String[] args) {
        new P01_Frame();
    }
}
 
class P01_Frame{
    public P01_Frame() {
        run();
    }
    private void run() {
    	JFrame frame = new JFrame();
    	frame.setTitle("예제1");
    	frame.setBounds(100, 100, 300, 200);
    	frame.setVisible(true);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

 

Frame의 크기 및 위치 지정
- frame.setLocation(int x, int y) : 화면 좌측 상단 기준으로 프레임 창이 뜨는 위치 지정
- frame.setSize(int width, int height) : 프레임 창의 크기 지정
- frame.setBounds(int x, int y, int width, int height) : 창의 위치와 크기 지정
- frame.setPreferredSize(new Dimension(int width, ind height)) :  layout 관리자에서는 setPreferredSize를 써줘야함 
  다른 크기지정 메소드는 잘려나가거나 나오지 않을 수 있음
- frame.pack() : 프레임 안의 컴포넌트의 크기에 맞게 프레임의 사이즈를 줄임(맞춤)

 

나머지
- frame.setVisible(boolean b) : 프레임이 보이거나 보이지 않도록 지정하는 메소드

- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) : 윈도우 창 종료시 선택 옵션 

  • DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.
  • HIDE_ON_CLOSE (defined in WindowConstants): 등록된 WindowListener 객체를 호출한 후 자동으로 프레임을 숨깁니다.
  • DISPOSE_ON_CLOSE (defined in WindowConstants): 등록된 WindowListener 개체를 호출한 후 프레임을 자동으로 숨기고 삭제합니다.
  • EXIT_ON_CLOSE (defined in WindowConstants): Exit the application using the System exit method. Use this only in applications.

 

https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/JFrame.html#setDefaultCloseOperation(int) 

 

JFrame (Java SE 11 & JDK 11 )

An extended version of java.awt.Frame that adds support for the JFC/Swing component architecture. You can find task-oriented documentation about using JFrame in The Java Tutorial, in the section How to Make Frames. The JFrame class is slightly incompatible

docs.oracle.com

 

'PROGRAM > JAVA-SWING' 카테고리의 다른 글

KeyEvent  (0) 2022.09.20
Thread 2  (0) 2022.09.19
Thread  (0) 2022.09.15
#02 - 버튼배열 생성(계산기 프로그램)  (0) 2022.09.07
#02 - 버튼 생성하기(JPanel / JButton / JLabel)  (0) 2022.09.07