572
No
class Calculator
package java8;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame{//프레임 상속
JPanel panel;
JTextField tField;
JButton[] buttons; //버튼 배열
String[] labels={
"Backspace", "","", "CE", "C",
"7", "8","9","/", "sqrt",
"4","5","6", "x","%",
"1","2","3","-","1/x",
"0","+/-", ".","+", "="
};
public Calculator() {
tField=new JTextField(35);//텍스트입력상자(글자수)
panel=new JPanel();
tField.setText("0.");//텍스트값 설정
//getText() 텍스트 읽기, setText("텍스트") 텍스트 쓰기
tField.setEditable(false);//입력 불가능 상탱
//0행, 5열, x축, y축 간격
panel.setLayout(new GridLayout(0, 5, 3, 3));
buttons=new JButton[25];
int index=0;
for(int rows=0; rows<5; rows++){
for(int cols=0; cols<5; cols++){
buttons[index]=new JButton(labels[index]); //버튼 생성
if(cols>=3)
buttons[index].setForeground(Color.red);//전경색
else
buttons[index].setForeground(Color.blue);
buttons[index].setBackground(Color.yellow);
panel.add(buttons[index]);
index++;
}
}
add(tField, BorderLayout.NORTH );
add(panel, BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
new Calculator();
}
}
댓글 ( 4)
댓글 남기기