본문 바로가기

[JAVA]

[JAVA]10월 12일 GUI, 그래픽 사용자 인터페이스

*기본 컴포넌트
·JButton, JLabel, JCheckbox, JChoice, JList, JMenu, JTextField, JScrollbar, JTextArea, JCanvas 등이 여기에 속한다.
*컨테이너 컴포넌트
·다른 컴포넌트를 안에 포함할 수 있는 컴포넌트로서 JFrame, JDialog, JApplet, JPanel, JScrollPane 등이 여기에 속한다.

 * GUI (Graphic User Interface)

GUI 작성 절차

최상위 컨테이너를 생성한다.

컴포넌트를 추가한다.

ex) 프레임 생성

public class Test {

public static void main(String[] args) {

JFrame f = new JFrame();

f.setTitle("Hello");

f.setSize(1000, 900);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

}

프레임 생성의 두번째 예로는  

class MyFrame extends JFrame

{

public MyFrame(){

this.setTitle("상속 받아서 만듬");

this.setSize(500, 200);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true); } }

public class Test {

public static void main(String[] args) {

MyFrame mf = new MyFrame();

}

}

MYFrame 클래스에 JFrame 클래스를 상속받아 MyFrame을 정의한후에

메인함수에서 구현.


*컴포넌트 생성과 추가

위에 MYFrame 기본생성자 안에 추가 하면 버튼이라는 컴포넌트가 생성된다.


JButton button = new JButton("버튼");

this.add(button);

이렇게 생성하면 화면에 버튼이라는 창 하나만 꽉차게 나온다.

그래서 패널 이라는곳에 컴포넌트들을 여러개 추가해서 사용해보자.


*패널(JPanel) - 컴포넌트들을 가질 수 있는 컨테이너.

*레이블(JPanel) - 레이블은 편집이 불가능한 텍스트를 표시.

패널과 레이블 사용예로는 

JPanel panel = new JPanel();                    //패널 생성

JLabel label = new JLabel("안녕하세요");  //레이블 생성

JButton button = new JButton("버튼");     //버튼 생성

panel.add(label);                           //패널에 레이블을 추가

panel.add(button);                         //패널에 버튼을 추가

this.add(panel);                            //패널을 프레임에 추가