1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | import java.awt.*; import java.awt.event.*; public class Calculator extends Frame { Panel buttons = new Panel(); Panel display = new Panel(); String[] buttonStr = {"MC", "MR", "MS", "M+", "M-", "←", "CE", "C", "±", "√", "7", "8", "9", "/", "%", "4", "5", "6", "*", "1/x", "1", "2", "3", "-", "=", "0", ",000", ".", "+", " "}; Button[] buttonArr = new Button[buttonStr.length]; Label label = new Label("0"); MenuBar menu = new MenuBar(); Menu view = new Menu("보기"); Menu edit = new Menu("편집"); Menu help = new Menu("도움말"); MenuItem v_general = new MenuItem("일반용"); MenuItem v_engineer = new MenuItem("공학용"); MenuItem v_programmer = new MenuItem("프로그래머용"); MenuItem v_stats = new MenuItem("통계용"); CheckboxMenuItem v_record = new CheckboxMenuItem("기록"); CheckboxMenuItem v_position = new CheckboxMenuItem("자릿수 구분 단위"); MenuItem v_normal = new MenuItem("기본"); MenuItem v_measure = new MenuItem("단위 변환"); MenuItem v_day = new MenuItem("날짜 계산"); Menu v_worksheet = new Menu("워크시트"); MenuItem vw_m = new MenuItem("주택담보대출"); MenuItem vw_v = new MenuItem("자동차 임대"); MenuItem vw_f = new MenuItem("연비 계산(mpg)"); MenuItem vw_u = new MenuItem("연비 계산(L/100km)"); MenuItem e_copy = new MenuItem("복사"); MenuItem e_paste = new MenuItem("붙여넣기"); Menu e_record = new Menu("기록"); MenuItem er_copy = new MenuItem("기록 복사"); MenuItem er_edit = new MenuItem("편집"); MenuItem er_cancelEdit = new MenuItem("편집 취소"); MenuItem er_delete = new MenuItem("삭제"); MenuItem h_Help = new MenuItem("도움말 보기"); MenuItem h_Info = new MenuItem("계산기 정보"); public Calculator() { super("계산기"); Toolkit toolkit = Toolkit.getDefaultToolkit(); Image icon = toolkit.getImage("cal.png"); setIconImage(icon); setSize(230, 320); setLocation(100, 100); setMenuBar(menu); setResizable(false); label.setFont(new Font("돋움체", Font.BOLD, 20)); display.setLayout(new FlowLayout(FlowLayout.RIGHT)); display.setBackground(Color.LIGHT_GRAY); display.add(label); buttons.setBackground(Color.gray); buttons.setLayout(new GridLayout(6, 5, 5, 5)); for (int i = 0; i < buttonStr.length; i++) { buttonArr[i] = new Button(buttonStr[i]); buttons.add(buttonArr[i]); } add(display, BorderLayout.NORTH); add(buttons, BorderLayout.CENTER); menu.add(view); menu.add(edit); menu.add(help); view.add(v_general); view.add(v_engineer); view.add(v_programmer); view.add(v_stats); view.addSeparator(); view.add(v_record); view.add(v_position); view.addSeparator(); view.add(v_normal); view.add(v_measure); view.add(v_day); view.add(v_worksheet); v_worksheet.add(vw_m); v_worksheet.add(vw_v); v_worksheet.add(vw_f); v_worksheet.add(vw_u); edit.add(e_copy); edit.add(e_paste); edit.add(e_record); e_record.add(er_copy); e_record.add(er_edit); e_record.add(er_cancelEdit); e_record.add(er_delete); help.add(h_Help); help.add(h_Info); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); setVisible(true); } public static void main(String[] args) { Calculator cal = new Calculator(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator_2 extends Frame { private Panel buttons = new Panel(); private Panel display = new Panel(); private String[] buttonStr = {"MC", "MR", "MS", "M+", "M-", "←", "CE", "C", "±", "√", "7", "8", "9", "/", "%", "4", "5", "6", "*", "1/x", "1", "2", "3", "-", "=", "0", ".", "+"}; private Button[] buttonArr = new Button[buttonStr.length]; private Label label = new Label("0"); private MenuBar menu = new MenuBar(); private Menu view = new Menu("보기"); private Menu edit = new Menu("편집"); private Menu help = new Menu("도움말"); private MenuItem v_general = new MenuItem("일반용"); private MenuItem v_engineer = new MenuItem("공학용"); private MenuItem v_programmer = new MenuItem("프로그래머용"); private MenuItem v_stats = new MenuItem("통계용"); private CheckboxMenuItem v_record = new CheckboxMenuItem("기록"); private CheckboxMenuItem v_position = new CheckboxMenuItem("자릿수 구분 단위"); private MenuItem v_normal = new MenuItem("기본"); private MenuItem v_measure = new MenuItem("단위 변환"); private MenuItem v_day = new MenuItem("날짜 계산"); private Menu v_worksheet = new Menu("워크시트"); private MenuItem vw_m = new MenuItem("주택담보대출"); private MenuItem vw_v = new MenuItem("자동차 임대"); private MenuItem vw_f = new MenuItem("연비 계산(mpg)"); private MenuItem vw_u = new MenuItem("연비 계산(L/100km)"); private MenuItem e_copy = new MenuItem("복사"); private MenuItem e_paste = new MenuItem("붙여넣기"); private Menu e_record = new Menu("기록"); private MenuItem er_copy = new MenuItem("기록 복사"); private MenuItem er_edit = new MenuItem("편집"); private MenuItem er_cancelEdit = new MenuItem("편집 취소"); private MenuItem er_delete = new MenuItem("삭제"); private MenuItem h_Help = new MenuItem("도움말 보기"); private MenuItem h_Info = new MenuItem("계산기 정보"); public Calculator_2() { super("계산기"); Toolkit toolkit = Toolkit.getDefaultToolkit(); Image icon = toolkit.getImage("cal.png"); setIconImage(icon); setSize(230, 320); setLocation(100, 100); setMenuBar(menu); setResizable(false); label.setFont(new Font("돋움체", Font.BOLD, 20)); display.setLayout(new FlowLayout(FlowLayout.RIGHT)); display.setBackground(Color.LIGHT_GRAY); display.add(label); buttons.setBackground(Color.PINK); // buttons.setLayout(new GridLayout(6, 5, 5, 5)); buttons.setLayout(new GridBagLayout()); for (int i = 0; i < buttonStr.length; i++) { buttonArr[i] = new Button(buttonStr[i]); buttonArr[i].setBackground(Color.WHITE); // buttons.add(buttonArr[i]); } int k = 0; for (int i = 0; i < 6; i++) { for(int j = 0; j < 5; j++) { if(k >= buttonArr.length) break; if(buttonArr[k].getLabel().equals("0")) { arrange(buttonArr[k], j, i, 2, 1); j++; } else if(buttonArr[k].getLabel().equals("=")) { arrange(buttonArr[k], j, i, 1, 2); } else { arrange(buttonArr[k], j, i, 1, 1); } k++; } } add(display, BorderLayout.NORTH); add(buttons, BorderLayout.CENTER); menu.add(view); menu.add(edit); menu.add(help); view.add(v_general); view.add(v_engineer); view.add(v_programmer); view.add(v_stats); view.addSeparator(); view.add(v_record); view.add(v_position); view.addSeparator(); view.add(v_normal); view.add(v_measure); view.add(v_day); view.add(v_worksheet); v_worksheet.add(vw_m); v_worksheet.add(vw_v); v_worksheet.add(vw_f); v_worksheet.add(vw_u); edit.add(e_copy); edit.add(e_paste); edit.add(e_record); e_record.add(er_copy); e_record.add(er_edit); e_record.add(er_cancelEdit); e_record.add(er_delete); help.add(h_Help); help.add(h_Info); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); setVisible(true); } public void arrange(Component c, int x, int y, int w, int h) { // GridBagLayout은 GridBagConstraints 클래스에 저장된 제약에 따라 컴포넌트를 배치한다. GridBagConstraints gbc = new GridBagConstraints(); // 컴포넌트를 확장하는 방식 (NONE, HORIZONTAL, VERTICAL, BOTH) gbc.fill = GridBagConstraints.BOTH; gbc.gridx = x; // x 좌표 gbc.gridy = y; // y 좌표 gbc.gridwidth = w; // 컴포넌트가 차지할 넓이 gbc.gridheight = h; // 컴포넌트가 차지할 높이 gbc.weightx = 1; // 여백을 분배하는 좌표, 0 일경우 가운데로 수렴한다. 보통 1로 사용 gbc.weighty = 1; gbc.insets = new Insets(2,2,2,2); buttons.add(c, gbc); } public static void main(String[] args) { Calculator_2 cal = new Calculator_2(); } } | cs |
'미래(2015-2016) > 자습' 카테고리의 다른 글
DefaultListModel (0) | 2015.11.02 |
---|---|
GridBagLayout (0) | 2015.10.29 |
Dimension (0) | 2015.10.27 |
Toolkit (0) | 2015.10.26 |
소코반(준완성), 테트리스 (1) | 2015.10.08 |