계산기 만들기 질문입니다.
지지않는
숫자 버튼을 눌렀을때 이벤트가 발생하지 않습니다.왜 그런건지..ㅠ0ㅠ
고수님들의 답변 부탁드립니다!!!
import java.awt.*;
import java.awt.event.*;
public class EngCalculatorUI extends Frame implements ItemListener,ActionListener{
MenuBar mBar;
Menu editMenu,viewMenu;
Menu helpMenu;
GridBagConstraints gbc;
TextField resultScreen;
Button btnBackSpace,btnCE,btnC;
CheckboxGroup cbg1;
Checkbox cbHex,cbDec,cbOct,cbBin;
CheckboxGroup cbg2;
Checkbox cbDegrees,cbRadians,cbGrads;
Checkbox cbInv,cbHyp;
Button btnSta,btnFE,btnOpen,btnClose,btnMC,btnAve;
Button btndms,btnExp,btnln,btnMR,btnSum,btnsin,btnxbyy;
Button btnlog,btnMS,btns,btncos,btnxby3,btnfac,btnMPlus;
Button btnDat,btntan,btnxby2,btnDevideX,btnpi;
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0;
Button btnPorM,btnPoint,btndevide,btnMod,btnAnd,btnMulti;
Button btnOr,btnXor,btnMinus,btnLsh,btnNot,btnPlus,btnResult;
Button btnInt,btnAA,btnBB,btnCC,btnDD,btnEE,btnFF;
Dialog dlgInfo;
Label ver_Info,name_Info,ID_Info,result;
String str;
public static void main(String[] args) {
EngCalculatorUI ecalcUI = new EngCalculatorUI();
}
public EngCalculatorUI() {
//배경 색상 정의
final Color ECACL_BG_COLOR = new Color(222,222,222);
// 메뉴바 생성
editMenu = new Menu(편집);
editMenu.add(복사);
editMenu.add(붙여넣기);
viewMenu = new Menu(보기);
mBar = new MenuBar();
mBar.add(editMenu);
mBar.add(viewMenu);
helpMenu = new Menu(도움말);
helpMenu.add(도움말 항목);
helpMenu.addSeparator();
MenuItem cal_Info = new MenuItem(계산기 정보);
helpMenu.add(cal_Info);
cal_Info.addActionListener(this); mBar.setHelpMenu(helpMenu);
setMenuBar(mBar);
//panel 생성
//결과표시 panel
Panel resultPanel = new Panel();
//보기 선택 panel
Panel viewSelectionPanel = new Panel();
//기능 panel
Panel buttonsPanel = new Panel();
//보디 패널 생성
Panel bodyPanel = new Panel();
bodyPanel.setBackground(ECACL_BG_COLOR);
add(bodyPanel);
//보디 패널에 Layout 관리자로 GridBagLayout 설정
GridBagLayout gbl = new GridBagLayout();
bodyPanel.setLayout(gbl);
Insets i = new Insets(0, 0, 0, 0);
//각 패널에 GridBagConstraints 설정
GridBagConstraints gbc;
gbc = new GridBagConstraints
(0,0,1,1,1.0,1.0,GridBagConstraints.WEST,GridBagConstraints.BOTH,i,0,0);
gbl.setConstraints(resultPanel, gbc);
gbc = new GridBagConstraints
(0,3,1,1,1.0,1.0,GridBagConstraints.WEST,GridBagConstraints.BOTH,i,0,0);
gbl.setConstraints(viewSelectionPanel, gbc);
gbc = new GridBagConstraints
(0,6,1,1,1.0,8.0,GridBagConstraints.WEST,GridBagConstraints.BOTH,i,0,0);
gbl.setConstraints(buttonsPanel, gbc);
//서브 패널 생성
Panel subPanel1 = new Panel();
Panel subPanel2 = new Panel();
Panel subPanel3 = new Panel();
Panel subPanel4 = new Panel();
Panel buttonsPanel1 = new Panel();
buttonsPanel1.setBackground(ECACL_BG_COLOR);
GridBagLayout bgbl = new GridBagLayout();
buttonsPanel.setLayout(bgbl);
Insets bi = new Insets(0, 0, 0, 0);
// 각 서브 패널에 GridBagConstraints 설정
GridBagConstraints bgbc;
bgbc = new GridBagConstraints(0,0,1,1,0.5,1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, bi,0,0);
bgbl.setConstraints(subPanel1, bgbc);
bgbc = new GridBagConstraints(1,0,1,1,0.5,1.0,
GridBagConstraints.WEST,GridBagConstraints.BOTH,bi,0,0);
bgbl.setConstraints(subPanel2, bgbc);
bgbc = new GridBagConstraints(0,1,1,1,0.5,5.0,
GridBagConstraints.WEST,GridBagConstraints.BOTH,bi,0,0);
bgbl.setConstraints(subPanel3, bgbc);
bgbc = new GridBagConstraints(1,1,1,1,0.5,5.0,
GridBagConstraints.WEST,GridBagConstraints.BOTH, bi,0,0);
bgbl.setConstraints(subPanel4, bgbc);
buttonsPanel.add(subPanel1); buttonsPanel.add(subPanel2);
buttonsPanel.add(subPanel3); buttonsPanel.add(subPanel4);
//보디 패널에 각 패널 추가
bodyPanel.add(resultPanel);
bodyPanel.add(viewSelectionPanel);
bodyPanel.add(buttonsPanel);
add(bodyPanel);
resultScreen = new TextField(0.,49);
resultScreen.setEditable(false);
resultScreen.setBackground(new Color(255,255,255));
//resultScreen.setHorizontalAlignment(JTextField.RIGHT);
resultPanel.add(resultScreen);
//viewSelectionPanel 설정
viewSelectionPanel.setLayout(new GridLayout(1,2));
//checkbox
cbg1 = new CheckboxGroup();
cbHex = new Checkbox(Hex,cbg1,false);
cbDec = new Checkbox(Dec,cbg1,truebg1,true);
cbOct = new Checkbox(Oct,cbg1,false);
cbBin = new Checkbox(Bin,cbg1,false);
viewSelectionPanel.add(cbHex);viewSelectionPanel.add(cbDec);
viewSelectionPanel.add(cbOct);viewSelectionPanel.add(cbBin);
cbg2 = new CheckboxGroup();
cbDegrees = new Checkbox(Degress,cbg2,true);
cbRadians = new Checkbox(Radians,cbg2,false);
cbGrads = new Checkbox(Grads,cbg2,false);
viewSelectionPanel.add(cbDegrees);viewSelectionPanel.add(cbRadians);
viewSelectionPanel.add(cbGrads);
//checkbox
cbInv = new Checkbox(Inv );
cbInv.setState(false);
cbInv.addItemListener(this);
cbHyp = new Checkbox(Hyp );
cbHyp.setState(false);
cbHyp.addItemListener(this);
subPanel1.add(cbInv);subPanel1.add(cbHyp);
subPanel1.setLayout(new FlowLayout(0));// button in subPanel2btnBackSpace = new Button(BackSpace);
btnBackSpace.setForeground(Color.red);
subPanel2.add(btnBackSpace);
btnCE = new Button( CE );
btnCE.setForeground(Color.red);
subPanel2.add(btnCE);
btnC = new Button( C );
btnC.setForeground(Color.red);
subPanel2.add(btnC);
subPanel2.setLayout(new FlowLayout(2));
//button in subPanel3
subPanel3.setLayout(new GridLayout(5,5));
btnSta = new Button(Sta);
btnSta.setForeground(Color.blue);
btnFE = new Button(F-E);
btnFE.setForeground(Color.PINK);
btnOpen = new Button(();
btnOpen.setForeground(Color.PINK);
btnClose = new Button());
btnClose.setForeground(Color.PINK);
btnMC = new Button(MC);
btnMC.setForeground(Color.red);
btnAve = new Button(Ave);
btnAve.setEnabled(false);
btndms = new Button(dms);
btndms.setForeground(Color.PINK);
btnExp = new Button(Exp);
btnExp.setForeground(Color.PINK);
btnln = new Button(ln);
btnln.setForeground(Color.PINK);
btnMR = new Button(MR);
btnMR.setForeground(Color.red);
btnSum = new Button(Sum);
btnSum.setEnabled(false);
btnsin = new Button(sin);
btnsin.setForeground(Color.PINK);
btnxbyy = new Button(x^y);
btnxbyy.setForeground(Color.PINK);
btnlog = new Button(log);
btnlog.setForeground(Color.PINK);
btnMS = new Button(MS);
btnMS.setForeground(Color.red);
btns = new Button(s);
btns.setEnabled(false);
btncos = new Button(cos);
btncos.setForeground(Color.PINK);
btnxby3 = new Button(x^3);
btnxby3.setForeground(Color.PINK);
btnfac = new Button(n!);
btnfac.setForeground(Color.PINK);
btnMPlus = new Button(M+);
btnMPlus.setForeground(Color.red);
btnDat = new Button(Dat);
btnDat.setEnabled(false);
btntan = new Button(tan);
btntan.setForeground(Color.PINK);
btnxby2 = new Button(x^2);
btnxby2.setForeground(Color.PINK);
btnDevideX = new Button(1/x);
btnDevideX.setForeground(Color.PINK);
btnpi = new Button(pi);
btnpi.setForeground(Color.blue);
subPanel3.add(btnSta);subPanel3.add(btnFE);subPanel3.add(btnOpen);
subPanel3.add(btnClose);subPanel3.add(btnMC);subPanel3.add(btnAve);
subPanel3.add(btndms);subPanel3.add(btnExp);subPanel3.add(btnln);
subPanel3.add(btnMR);subPanel3.add(btnSum);subPanel3.add(btnsin);
subPanel3.add(btnxbyy);subPanel3.add(btnlog);subPanel3.add(btnMS);
subPanel3.add(btns);subPanel3.add(btncos);subPanel3.add(btnxby3);
subPanel3.add(btnfac);subPanel3.add(btnMPlus);subPanel3.add(btnDat);
subPanel3.add(btntan);subPanel3.add(btnxby2);subPanel3.add(btnDevideX);
subPanel3.add(btnpi);
// button
subPanel4.setLayout(new GridLayout(5,6));
Button btn1 = new Button(1);
btn1.setForeground(Color.blue);
Button btn2 = new Button(2);
btn2.setForeground(Color.blue);
Button btn3 = new Button(3);
btn3.setForeground(Color.blue);
Button btn4 = new Button(4);
btn4.setForeground(Color.blue);
Button btn5 = new Button(5);
btn5.setForeground(Color.blue);
Button btn6 = new Button(6);
btn6.setForeground(Color.blue);
Button btn7 = new Button(7);
btn7.setForeground(Color.blue);
Button btn8 = new Button(8);
btn8.setForeground(Color.blue);
Button btn9 = new Button(9);
btn9.setForeground(Color.blue);
Button btn0 = new Button(0);
btn0.setForeground(Color.blue);
//숫자 입력
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btn0.addActionListener(this);
Button btnPorM = new Button(+/-);
btnPorM.setForeground(Color.blue);
Button btnPoint = new Button(.);
btnPoint.setForeground(Color.blue);
Button btndevide = new Button(/);
btndevide.setForeground(Color.red);
Button btnMod = new Button(Mod);
btnMod.setForeground(Color.red);
Button btnAnd = new Button(And);
btnAnd.setForeground(Color.red);
Button btnMulti = new Button(*);
btnMulti.setForeground(Color.red);
Button btnOr = new Button(Or);
btnOr.setForeground(Color.red);
Button btnXor = new Button(Xor);
btnXor.setForeground(Color.red);
Button btnMinus = new Button(Minus);
btnMinus.setForeground(Color.red);
Button btnLsh = new Button(Lsh);
btnLsh.setForeground(Color.red);
Button btnNot = new Button(Not);
btnNot.setForeground(Color.red);
Button btnPlus = new Button(+);
btnPlus.setForeground(Color.red);
Button btnResult = new Button(=utton(=);
btnResult.setForeground(Color.red);
Button btnInt = new Button(Int);
btnInt.setForeground(Color.red);
Button btnAA = new Button(A);
btnAA.setEnabled(false);
Button btnBB = new Button(B);
btnBB.setEnabled(false);
Button btnCC = new Button(C);
btnCC.setEnabled(false);
Button btnDD = new Button(D);
btnDD.setEnabled(false);
Button btnEE = new Button(E);
btnEE.setEnabled(false);
Button btnFF = new Button(F);
btnFF.setEnabled(false);
subPanel4.add(btn7);subPanel4.add(btn8);subPanel4.add(btn9);
subPanel4.add(btndevide);subPanel4.add(btnMod);subPanel4.add(btnAnd);
subPanel4.add(btn4);subPanel4.add(btn5);subPanel4.add(btn6);
subPanel4.add(btnMulti);subPanel4.add(btnOr);subPanel4.add(btnXor);
subPanel4.add(btn1);subPanel4.add(btn2);subPanel4.add(btn3);
subPanel4.add(btnMinus);subPanel4.add(btnLsh);subPanel4.add(btnNot);
subPanel4.add(btn0);subPanel4.add(btnPorM);subPanel4.add(btnPoint);
subPanel4.add(btnPlus);subPanel4.add(btnResult);subPanel4.add(btnInt);
subPanel4.add(btnAA);subPanel4.add(btnBB);subPanel4.add(btnCC);
subPanel4.add(btnDD);subPanel4.add(btnEE);subPanel4.add(btnFF);
//계산기 정보 보기
dlgInfo = new Dialog(this);
dlgInfo.setResizable(false);
ver_Info = new Label(계산기 버젼 : 1.0);
name_Info = new Label(만 든 이 : 이 준);
ID_Info = new Label(학번 : 012481);
Button btnok = new Button(확 인);
btnok.addActionListener(this);
dlgInfo.add(ver_Info);
dlgInfo.add(name_Info);
dlgInfo.add(ID_Info);
dlgInfo.add(btnok);
dlgInfo.setTitle(계산기 정보);
dlgInfo.setBounds(150,150,220,150);
dlgInfo.setLayout(new GridLayout(4,1));
// 프레임 초기화
addWindowListener(new WinCloser());
setTitle(계산기 S012481);
setBounds(100, 100, 560, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent ae){
Object obj = new Object();
obj = ae.getSource();
if(obj == btn1 || obj == btn2 || obj == btn3 || obj == btn4 ||
obj == btn5 || obj == btn6 || obj == btn7 || obj == btn8 ||
obj == btn9 || obj == btn0)
{
str = 1;
printNum(str);
}
if(ae.getActionCommand().equals(계산기 정보))
dlgInfo.setVisible(true);
if(ae.getActionCommand().equals(확 인))
dlgInfo.setVisible(false);
}
private void printNum(String n) {
//String str = resultScreen.getText();
resultScreen.setText(n);
}}