레퍼런스를 두고 클론코딩을 하되,
import java.awt.*;
import javax.swing.*;
public class Practice_calc extends JFrame{
public Practice_calc(){
setTitle("계산기");
setSize(350,450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p_all = new JPanel();
JPanel p_cal = new JPanel();
JPanel p_cont = new JPanel();
Font bold = new Font("맑은 고딕", Font.BOLD, 40);
Font button_bold = new Font("맑은 고딕", Font.BOLD, 30);
Color gray = new Color(204, 204, 204);
Color dark_gray = new Color(154, 154, 154);
Color white = new Color(255, 255, 255);
Color light_blue = new Color(153, 153, 255);
/* p_all 만들기 */
p_all.setLayout(new FlowLayout());
setContentPane(p_all);
p_all.add(p_cal);
p_all.add(p_cont);
/* p_cal 만들기 */
p_cal.setLayout(new GridLayout(2, 1));
p_cal.setPreferredSize(new Dimension(290,80)); //사이즈 지정
JLabel input = new JLabel("2+3=", JLabel.RIGHT); //입력 수식
JLabel result = new JLabel("5", JLabel.RIGHT); //결과값
// input.setAlignmentX(RIGHT_ALIGNMENT);
// result.setAlignmentX(RIGHT_ALIGNMENT);
input.setForeground(dark_gray); //색상
result.setFont(bold); //폰트
p_cal.add(input);
p_cal.add(result);
/* p_cont 만들기 */
p_cont.setLayout(new GridLayout(4, 4));
p_cont.setPreferredSize(new Dimension(290, 300));
JButton[][] arr_button = new JButton[4][4];
String[][] arr_content = {{"7", "8", "9", "+"}, {"4", "5", "6", "-"}, {"1", "2", "3", "*"}, {"C", "0", "=", "/"}};
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
arr_button[i][j] = new JButton(arr_content[i][j]);
p_cont.add(arr_button[i][j]);
char temp = arr_content[i][j].charAt(0);
if(temp == '=')
arr_button[i][j].setBackground(light_blue);
else if(temp >= '0' && temp <= '9') {
arr_button[i][j].setBackground(white);
arr_button[i][j].setFont(button_bold);
}
else
arr_button[i][j].setBackground(gray);
}
}
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Practice_calc();
}
}
최종 목표는 '1시간마다 모니터에서 눈을 떼고 눈 스트레칭을 하라고 알림을 주는 윈도우 프로그램'을 만드는 것이다. 이유는 다들 예상하다시피...코딩에 시간 가는 줄 모르고 집중하다 보니 안구건조증이 생기고 눈이 아파졌기 때문이다. (그리고 이 프로그램을 만들면서 또 눈이 아플 예정이다.)
찾아보니 어떤 언어로 구현하든, 내가 원하는 대로 알림창을 띄우려면 GUI가 필요한 것 같았다. 마침 최근에 학교 수업에서 JAVA swing을 배운 적이 있었기 때문에 JAVA로 만들기로 결정했다. 구글링했을 때 현업에서 JAVA swing을 잘 쓰지 않는다고 해서 찝찝했지만, 다른 걸 새로 배우는 시간 없이 빠르게 만들어보고 싶었다.
최종 목표인 프로그램을 만들기에 앞서, 좀 더 간단하고 레퍼런스가 많은 프로그램을 연습용으로 만들어보고 싶었다.