Contents l l GUI Button GUI Textfield 2


Contents l l GUI – Button GUI - Textfield 2

GUI – Button l Vote. Counter. java와 Vote. Counter. Panel. java를 수 정해보자. l 후보자 1명을 추가한다. – for Sam l Sam을 위한 변수 추가 - vote counter, button, label l Sam을 위한 내부 class, Sam. Button. Listener 추가 - l Sam button을 눌렀을 때 반응하는 listener. Action. Listener를 추가할 때, class instance 생성. panel에 Sam의 button과 label을 추가 참고] 4. 10, 4. 11 – Push. Counter. java, Push. Counter. Panel. java 3

GUI – Textfield l BMI. java , BMIPanel. java // ******************************* // BMI. java // // BMI(body mass index)를 계산하기 위한 GUI를 구성한다. // ******************************* import javax. swing. JFrame; public class BMI { // ----------------------------// BMI GUI를 생성하고 보여준다. // ----------------------------public static void main(String[] args) { JFrame frame = new JFrame ("BMI"); frame. set. Default. Close. Operation (JFrame. EXIT_ON_CLOSE); BMIPanel panel = new BMIPanel; frame. get. Content. Panel(). add(panel); frame. pack(); frame. set. Visible (true); } } 4

// ******************************* // BMIPanel. java // // GUI에서 BMI를 계산한다. // ******************************* import java. awt. *; import java. awt. event. *; import javax. swing. *; public class BMIPanel extends JPanel { private int WIDTH = 300; private int HEIGHT = 120; private JPanel height. Label, weight. Label, BMILabel, result. Label; private JText. Fied height, weight; private JButton calculate; // ----------------------------// GUI를 구성한다. // ----------------------------public BMI() { // textfield (height와 weight)의 label 생성 height. Label = new JLabel ("Your height in meters : "); weight. Label = new JLabel ("Your weight in kilograms : "); 5

// "this is your BMI" label 생성 // BMI결과를 나타내는 label 생성 // height을 입력받을 JText. Field 생성 // weight을 입력받을 JText. Field 생성 // BMI를 계산할 button 생성 // button을 눌렀을 때 대응하는 BMIListener 생성 // // // panel에 panel에 height label과 height textfield를 추가 weight label과 weight textfield를 추가 button 추가 BMI label 추가 BMI결과를 나타내는 label 추가 // panel의 크기 지정 // panel의 색 지정 } 6

// ***************************** // 계산 button의 action listener를 나타낸다. // ***************************** private class BMIListener implements Action. Listener { // --------------------------// button을 눌렀을 때 BMI 계산 // --------------------------public void action. Performed (Action. Event event) { String height. Text, weight. Text; int height. Val, weight. Val; double bmi; // textfield(height, weight)에서 text를 가지고 온다. // Integer. parse. Int를 이용하여 text를 int로 형변환한다. // BMI 계산 : 체중 / 신장 or 체중 / 신장 2 // 결과 label에 BMI 결과를 나타낸다. Double. to. String을 사용하여 string으로 형변환한다. } } } 7

- Slides: 8