본문 바로가기

자바(JAVA)
[자바(Java)] GUI

// GUI : 그래픽 환경

- AWT : 자바 초창기 버전에서 사용

- SWING

- 기본적으로 JFrame ==> 빈 화면 ==> 버튼, 텍스트, 체크, 라디오 ...

기본 창 설정

package ex04.sample04;

import javax.swing.*;

public class Exam1013_3 {
	static class MyGUI extends JFrame {
		MyGUI() {
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 프로그램을 종료하게 한다.
			setTitle("연 습");
			
			setSize(500,500); // 픽셀 단위
			setVisible(true);
		}
	}
	
	public static void main(String[] args) {
		new MyGUI();
	}

}

버튼 만들기

package ex04.sample04;
import java.awt.*; //레이아웃 사용
import javax.swing.*;

public class Exam1013_3 {
	static class MyGUI extends JFrame {
		MyGUI() {
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
			setTitle("연 습");
			this.setLayout(new FlowLayout()); // 레이아웃 설정
			
			JButton btn1 = new JButton("버튼1"); //버튼 만들기
			this.add(btn1); // 버튼 올리기
			
			JButton btn2 = new JButton("버튼2"); 
			this.add(btn2);
			
			JButton btn3 = new JButton("버튼3"); 
			this.add(btn3);
			
			JButton btn4 = new JButton("버튼4");
			this.add(btn4);
			
			setSize(250,250); 
			setVisible(true);
		}
	}
	
	public static void main(String[] args) {
		new MyGUI();
	}
}


**FlowLayout 은 화면 사이즈에 맞춰서 버튼의 배열을 자동으로 조정해준다.


버튼 설정(내가 직접 그린 내 사인을 활용'_')

package ex04.sample04;
import java.awt.*; //레이아웃 사용
import javax.swing.*;

public class Exam1013_3 {
	static class MyGUI extends JFrame {
		MyGUI() {
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
			setTitle("연 습");
			this.setLayout(new FlowLayout());
			
			ImageIcon img1 = new ImageIcon("c://image//sign1.png"); //이미지 불러오기
			ImageIcon img2 = new ImageIcon("c://image//sign2.png");
			
			JButton btn1 = new JButton("Sign1", img1);  // 버튼 이미지 설정
			this.add(btn1); // 버튼에 이미지 올리기
			
			JButton btn2 = new JButton("Sign2", img2); 
			this.add(btn2); 
			
			JLabel lbl1 = new JLabel("레이블 1");
			JLabel lbl2 = new JLabel("img2");
			this.add(lbl2);
			this.add(lbl2);
						
			JCheckBox chk1 = new JCheckBox("Java");
			JCheckBox chk2 = new JCheckBox("C");
			JCheckBox chk3 = new JCheckBox("Python");
			this.add(chk1);
			this.add(chk2);
			this.add(chk3);
			
			JRadioButton rdo1 = new JRadioButton("Java");
			JRadioButton rdo2 = new JRadioButton("C");
			JRadioButton rdo3 = new JRadioButton("Python");
			this.add(rdo1);
			this.add(rdo2);
			this.add(rdo3);
			
			ButtonGroup grp = new ButtonGroup();
			grp.add(rdo1);
			grp.add(rdo2);
			grp.add(rdo3);
			
			
			
			setSize(1000,250); 
			setVisible(true);
		}
	}
	
	public static void main(String[] args) {
		new MyGUI();
	}
}

버튼 설정2(목록)

package ex04.sample04;
import java.awt.*; 
import javax.swing.*;

public class Exam1013_3 {
	static class MyGUI extends JFrame {
		MyGUI() {
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
			setTitle("연 습");
			this.setLayout(new FlowLayout());
			
			String[] language = {"C", "JavaScript", "Java", "Python", "HTML", "JSP", "Oracle"};
			JList list = new JList(language);
			this.add(list);
			
			JComboBox combo = new JComboBox(language);
			this.add(combo);
						
			setSize(250,250); 
			setVisible(true);
		}
	}
	
	public static void main(String[] args) {
		new MyGUI();
	}
}

이벤트 - 버튼 색 바꾸기

package ex04.sample04;

import java.awt.*; 
import javax.swing.*;
import java.awt.event.*; //자바의 이벤트 처리

public class Exam1013_3 {
	static class MyGUI extends JFrame {
		MyGUI() {
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
			setTitle("연 습");
			this.setLayout(new FlowLayout());
			
			JButton btn1 = new JButton("Click");
			btn1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					btn1.setBackground(Color.BLUE);
				}
			});
			this.add(btn1);
									
			setSize(250,250); 
			setVisible(true);
		}
	}
	
	public static void main(String[] args) {
		new MyGUI();
	}
}