gui

GUI 프로그래밍

GUI 컴포넌트

일반적인 GUI 프로그램 구조

AWT와 Swing, JavaFX

AWT

java.awt.Component
-> java.awt.Button
-> java.awt.Canvas
-> java.awt.Checkbox
-> java.awt.Choice
-> java.awt.Container
   --> javaa.awt.Panel
   --> javaa.awt.ScrollPanel
   --> javaa.awt.Window
package gui;
import java.awt.*;

public class AWTEx1 {
    public static void main(String[] args) { 
        // Frame : 독립창 만들기
        Frame f = new Frame("Hello World");
        f.setLayout(new FlowLayout());
        Label label = new Label("Welcome to AWT");
        f.add(label);
        f.setSize(500, 500);
        f.setVisible(true);
    }
}

AWT 창닫기

package gui;

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class AWTEx1 {

    public static void main(String[] args) { 
        // Frame : 독립창 만들기
        Frame f = new Frame("Hello World");
        f.setLayout(new FlowLayout());
        Label label = new Label("Welcome to AWT");
        f.add(label);
        f.setSize(500, 500);
        f.setVisible(true);
        f.addWindowListener(new WindowCloseHandler());
    }
}

class WindowCloseHandler extends WindowAdapter{
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}

Swing

package gui;
import java.awt.*;
import javax.swing.*;

public class SwingEx1 extends JFrame {
    public SwingEx1() {
        super("Hello World");
        getContentPane().setLayout(new FlowLayout());
        JLabel label = new JLabel("Welcome to Swing");
        getContentPane().add(label);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500,500);
        setVisible(true);
    }

    public static void main(String[] args) {
        AWTEx1 app = new SwingEx1();
    } 
}

레이아웃 적용 방법

package gui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class SwingLayout extends JFrame{
    JButton button1 = new JButton("버튼1");
    JButton button2 = new JButton("버튼2");
    JButton button3 = new JButton("버튼3");
    JButton button4 = new JButton("버튼4");
    JButton button5 = new JButton("버튼5");

    Panel p1 = new Panel();
    Panel p2 = new Panel();

    public void flowLayout() {
        p1.setLayout(new FlowLayout());
        p1.add(button1);
        p1.add(button2);
        p1.add(button3);
        p1.add(button4);
    }
    public void gridLayout() {
        p1.setLayout(new GridLayout(2,2));
        p1.add(button1);
        p1.add(button2);
        p1.add(button3);
        p1.add(button4);    
    }
    public void borderLayout() {
        p1.setLayout(new BorderLayout());
        p1.add(button1, BorderLayout.NORTH);
        p1.add(button2, BorderLayout.WEST);
        p1.add(button3, BorderLayout.EAST);
        p1.add(button4, BorderLayout.SOUTH);                
        p1.add(button5, BorderLayout.CENTER);               
    }
    public void cardLayout() {
        final CardLayout card = new CardLayout();
        setLayout(card);
        p1.add(button1);
        p1.add(button2);
        p1.add(button3);
        p2.add(button4);
        p2.add(button5);
        add("p1",p1);
        add("p2",p2);

        button3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                card.show(getContentPane(), "p2");
            }

        });
    }
    public SwingLayout() {
        super("Layout Showcase");
        getContentPane().add(p1);
        //flowLayout();
        //gridLayout();
        borderLayout();
        //cardLayout();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300,200);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingLayout app = new SwingLayout();
    }
}

이벤트 핸들링

이벤트 처리 구현 절차

package ch13;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class SwingEx1 extends JFrame implements ActionListener{
    int index = 0;
    String[] msgs = {"첫 번째 문장","두 번째 문장","세 번째 문장"};
    JButton button1 = new JButton("<<");
    JButton button2 = new JButton(">>");
    JButton button3 = new JButton(msgs[0]);

    public SwingEx1() {
        BorderLayout layout = new BorderLayout();
        setLayout(layout);
        button1.addActionListener(this);
        button2.addActionListener(this);
        button3.setEnabled(false);
        add(button1, BorderLayout.WEST);
        add(button2, BorderLayout.EAST);
        add(button3, BorderLayout.CENTER);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300,100);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if(obj == button1) {
            index--;
        }
        else if(obj == button2) {
            index++;
        }
        if(index > 2) 
            index = 0;
        else if(index < 0) {
            index = 2;
        }
        button3.setText(msgs[index]);
    }   

    public static void main(String[] args) {
        SwingEx1 app = new SwingEx1();
    }
}

익명클래스로 구현

package ch13;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class SwingEx2 extends JFrame{
    int index=0;
    String[] msgs = {"첫번째 문장","두번째 문장","세번째 문장"};
    JButton button1 = new JButton("<<");
    JButton button2 = new JButton(">>");
    JButton button3 = new JButton(msgs[0]);

    public SwingEx2() {
        BorderLayout layout = new BorderLayout();
        setLayout(layout);
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                index--;
                changeText();
            }   
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                index++;
                changeText();
            }
        });
        button3.setEnabled(false);
        add(button1, BorderLayout.WEST);
        add(button2, BorderLayout.EAST);
        add(button3, BorderLayout.CENTER);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300,100);
        setVisible(true);
    }

    public void changeText() {
        if(index > 2) 
            index = 0;
        else if(index < 0) {
            index = 2;
        }
        button3.setText(msgs[index]);
    }

    public static void main(String[] args) {
        SwingEx2 app = new SwingEx2();
    }

}