GUI 기초)연습문제

Dec 29, 2023
GUI  기초)연습문제

1) GUI 기초
다음과 같은 프로그램의 화면만 작성하여 보자. 적절한 배치 관리자를
선택하여 사용한다. 아직 이벤트 처리를 학습하지 않았으므로 기능을
제공할 필요는 없지만 기능을 제공해도 좋다.
package ex09.example; import ex09.MyFrame10; import javax.swing.*; import java.awt.*; public class example01 extends JFrame { public example01() { setSize(400, 150); setTitle("My Frame"); JLabel label = new JLabel("자바는 재미있나요?"); setLayout(new FlowLayout()); JButton button1 = new JButton("Yes"); JButton button2 = new JButton("No"); add(label); add(button1); add(button2); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { example01 f = new example01(); } }
notion image

기능 추가


 

 
 
2) GUI 기초
다음과 같은 프로그램의 화면만 만들어보자. 적절한 배치 관리자를
선택하여 사용한다. 아직 이벤트 처리를 학습하지 않았으므로 기능을
제공할 필요는 없지만 기능을 제공해도 좋다.
package ex09.example; import javax.swing.*; import java.awt.*; public class example02 extends JFrame { public example02() { setTitle("My Frame"); setSize(400, 150); setLayout(new FlowLayout()); JLabel jLabel = new JLabel("카운터 값"); JTextField jTextField = new JTextField(8); JButton jButton = new JButton("증가"); add(jLabel); add(jTextField); add(jButton); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { example02 f = new example02(); } }
notion image

기능 추가


 

 
 
3) GUI 기초
다음과 같은 프로그램의 화면만 만들어보자. 적절한 배치 관리자를
선택하여 사용한다. 패널을 추가로 사용하여도 좋다.
package ex09.example; import javax.swing.*; import java.awt.*; public class example03 extends JFrame { public example03() { setSize(350, 150); setTitle("My Frame"); setLayout(new FlowLayout()); JLabel jLabel = new JLabel("자바 호텔에 오신 것을 환영합니다. 숙박일수를 입력하세요."); JButton btn1 = new JButton("1일"); JButton btn2 = new JButton("2일"); JButton btn3 = new JButton("3일"); JButton btn4 = new JButton("4일"); JButton btn5 = new JButton("5일"); add(jLabel); add(btn1); add(btn2); add(btn3); add(btn4); add(btn5); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { example03 f = new example03(); } }
notion image
 
4) GUI 기초
다음과 같은 프로그램의 화면만 만들어보자.
배경색이 다른 패널을 이용한다.
이클립스의 “windowBuilder”를 이용하여도 좋다.
package ex09.example; import javax.swing.*; import java.awt.*; public class example04 extends JFrame { public example04() { setSize(320, 150); setTitle("JLabel Test"); setLayout(new FlowLayout()); setLayout(new FlowLayout(FlowLayout.LEFT, 25, 10)); // FlowLayout 생성자 수정 JLabel label1 = new JLabel(" Red"); JLabel label2 = new JLabel(" Yellow"); JLabel label3 = new JLabel(" Green"); JLabel label4 = new JLabel(" Blue"); JPanel jPanel1 = new JPanel(); jPanel1.setPreferredSize(new Dimension(40, 40)); jPanel1.setBackground(Color.red); JPanel jPanel2 = new JPanel(); jPanel2.setPreferredSize(new Dimension(40, 40)); jPanel2.setBackground(Color.yellow); JPanel jPanel3 = new JPanel(); jPanel3.setPreferredSize(new Dimension(40, 40)); jPanel3.setBackground(Color.green); JPanel jPanel4 = new JPanel(); jPanel4.setPreferredSize(new Dimension(40, 40)); jPanel4.setBackground(Color.blue); add(label1); add(label2); add(label3); add(label4); add(jPanel1); add(jPanel2); add(jPanel3); add(jPanel4); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { example04 f = new example04(); } }
notion image
 
5) GUI 기초
체중과 키를 받아서 BMI를 계산하는 다음과 같은 화면을 만들어보자.
이클립스의 “windowBuilder”를 이용하여도 좋다.
package ex09.example; import javax.swing.*; import java.awt.*; public class example05 extends JFrame { public example05() { setSize(350, 200); setLayout(new FlowLayout()); setLayout(new FlowLayout(FlowLayout.LEFT, 30, 10)); // GridLayout 생성자 수정 JLabel jLabel1 = new JLabel("나의 BMI 지수는 얼마나 될까? "); JLabel jLabel2 = new JLabel("나의 체중(kg):"); JLabel jLabel3 = new JLabel("나의 키(m): "); JTextField jTextField1 = new JTextField(10); JTextField jTextField2 = new JTextField(10); JButton jButton = new JButton("BMI 확인하기"); add(jLabel1); add(jLabel2); add(jTextField1); add(jLabel3); add(jTextField2); add(jButton); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { example05 f = new example05(); } }
notion image
 
6) GUI 기초
회원의 정보를 받아서 데이터베이스에 저장하는 프로그램의 화면을
만들어보자. 이클립스의 “windowBuilder”를 이용하여도 좋다.
package ex09.example; import javax.swing.*; import java.awt.*; public class Example06 extends JFrame { public Example06() { setSize(350, 260); setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 3; gbc.insets = new Insets(5, 5, 5, 5); JLabel j1 = new JLabel("회원 등록하기"); j1.setLocation(100, 10); j1.setFont(new Font("Dotum", Font.PLAIN, 20)); JLabel j2 = new JLabel("이름 "); JLabel j3 = new JLabel("패스워드 "); JLabel j4 = new JLabel("이메일 주소 "); JLabel j5 = new JLabel("전화번호 "); JTextField f1 = new JTextField(10); JTextField f2 = new JTextField(10); JTextField f3 = new JTextField(10); JTextField f4 = new JTextField(10); JButton b1 = new JButton("등록하기"); JButton b2 = new JButton("취소"); b1.setPreferredSize(new Dimension(100, 25)); b2.setPreferredSize(new Dimension(100, 25)); add(j1, gbc); gbc.gridy = 1; gbc.gridwidth = 1; gbc.anchor = GridBagConstraints.EAST; add(j2, gbc); gbc.gridx = 1; gbc.anchor = GridBagConstraints.WEST; add(f1, gbc); gbc.gridy = 2; gbc.gridx = 0; gbc.anchor = GridBagConstraints.EAST; add(j3, gbc); gbc.gridx = 1; gbc.anchor = GridBagConstraints.WEST; add(f2, gbc); gbc.gridy = 3; gbc.gridx = 0; gbc.anchor = GridBagConstraints.EAST; add(j4, gbc); gbc.gridx = 1; gbc.anchor = GridBagConstraints.WEST; add(f3, gbc); gbc.gridy = 4; gbc.gridx = 0; gbc.anchor = GridBagConstraints.EAST; add(j5, gbc); gbc.gridx = 1; gbc.anchor = GridBagConstraints.WEST; add(f4, gbc); gbc.gridy = 5; gbc.gridx = 0; gbc.gridwidth = 2; gbc.anchor = GridBagConstraints.CENTER; // 버튼을 중앙 정렬 // 간격을 작게 조절 gbc.insets = new Insets(2, -100, 2, 2); add(b1, gbc); gbc.gridx = 2; add(b2, gbc); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { Example06 f = new Example06(); } }
notion image
 
맑은 고딕 (Malgun Gothic):
Windows 운영 체제에 기본으로 포함되어 있는 한글 폰트입니다.
돋움 (Dotum):
Windows 운영 체제에 기본으로 포함되어 있는 폰트 중 하나입니다.
나눔 폰트 (Nanum Font):
나눔고딕, 나눔명조 등 다양한 스타일의 한글 폰트를 제공하는 나눔 폰트는 Linux 및 Windows에서도 사용 가능합니다.
한글 고딕 (Hanna):
한글 폰트로서 깔끔한 디자인을 가지고 있습니다.
 
7) 배치 관리자
프레임 안에 20개의 버튼을 다음과 같이 배치하는 프로그램을
작성하라. GridLayout을 사용한다.
버튼의 배경색은 랜덤한 색상으로 한다.
import javax.swing.*; import java.awt.*; import java.util.HashSet; import java.util.Set; public class Example07 extends JFrame { private JPanel panel; private JTextField tField; private JButton[] buttons; String[] labels = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19" }; public Example07() { tField = new JTextField(20); panel = new JPanel(); tField.setEnabled(false); setTitle("My Frame"); panel.setLayout(new GridLayout(0, 5, 0, 0)); buttons = new JButton[20]; int index = 0; // Use a Set to keep track of used colors Set<Color> usedColors = new HashSet<>(); for (int row = 0; row < 5; row++) { for (int cols = 0; cols < 5; cols++) { if (index < labels.length) { buttons[index] = new JButton(labels[index]); // Generate a unique color Color color; do { int R = (int) (Math.random() * 256); int G = (int) (Math.random() * 256); int B = (int) (Math.random() * 256); color = new Color(R, G, B); } while (usedColors.contains(color)); usedColors.add(color); buttons[index].setForeground(Color.BLACK); buttons[index].setBackground(color); panel.add(buttons[index]); index++; } } } setLayout(new BorderLayout()); add(tField, BorderLayout.NORTH); add(panel, BorderLayout.CENTER); tField.setBorder(BorderFactory.createEmptyBorder()); pack(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { Example07 f = new Example07(); } }
notion image
 
8) 배치 관리자
다음과 같은 간단한 전화번호 입력기의 화면을 만들어보자.
여러 개의 패널과 배치 관리자를 혼용하여 완성해본다.
package ex09.example; import javax.swing.*; import java.awt.*; public class Example08 extends JFrame { private JPanel panel; private JTextField tField; private JButton[] buttons; private String[] labels = { "1", "2", "3", "4", "5", "5", "7", "8", "9", "*", "0", "#", "send", "", "end" }; public Example08() { tField = new JTextField(35); panel = new JPanel(); tField.setText(""); tField.setEnabled(false); panel.setLayout(new GridLayout(0, 3, 0, 0)); buttons = new JButton[15]; int index = 0; for (int row = 0; row < 3; row++) { for (int cols = 0; cols < 5; cols++) { buttons[index] = new JButton(labels[index]); panel.add(buttons[index]); index++; } add(tField, BorderLayout.NORTH); add(panel, BorderLayout.CENTER); } JButton buttons = new JButton("clear"); add(buttons,BorderLayout.EAST); setVisible(true); pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { Example08 f = new Example08(); } }
notion image
 
9) 배치 관리자
다음과 같이 난수를 발생하여서 레이블을 불규칙하게 배치하여 보자.
어떤 배치 관리자를 어떻게 사용하여야 하는가?
import javax.swing.*; import java.awt.*; import java.util.HashSet; import java.util.Set; public class Example09 extends JFrame { private int[] labels; // 배열 초기화 private JPanel panel; public Example09() { setSize(400, 200); setLayout(null); // 배열 크기 지정 int num = 20; labels = new int[num]; // 랜덤 위치에 배치 Set<Point> usedLocations = new HashSet<>(); for (int index = 0; index < num; index++) { // 올바른 난수 범위로 수정 labels[index] = (int) (Math.random() * num); // 랜덤 위치에 JLabel 추가 (가정) JLabel label = new JLabel(String.valueOf(labels[index])); Point location; do { int x = (int) (Math.random() * 350); int y = (int) (Math.random() * 80); location = new Point(x, y); } while (usedLocations.contains(location)); usedLocations.add(location); label.setBounds(location.x, location.y, 400, 80); add(label); } setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { Example09 f = new Example09(); } }
notion image
 
Share article
RSSPowered by inblog