fork download
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. public class app18_8b extends Frame implements ActionListener
  5. {
  6. static app18_8b frm=new app18_8b(); //用app18_8b類別產生frm物件
  7. static Panel pnl=new Panel(new GridLayout(4,3)); //建立面板物件pnl
  8. static Panel pnl2=new Panel(new GridLayout(4,1)); //建立面板物件pnl2
  9. static Label lab=new Label("0.",Label.RIGHT); //建立lab 初值為"0." 向右靠齊
  10. //產生button物件
  11. static Button btn0=new Button("0");
  12. static Button btn1=new Button("1");
  13. static Button btn2=new Button("2");
  14. static Button btn3=new Button("3");
  15. static Button btn4=new Button("4");
  16. static Button btn5=new Button("5");
  17. static Button btn6=new Button("6");
  18. static Button btn7=new Button("7");
  19. static Button btn8=new Button("8");
  20. static Button btn9=new Button("9");
  21. static Button btn10=new Button("+");
  22. static Button btn11=new Button("-");
  23. static Button btn12=new Button("*");
  24. static Button btn13=new Button("/");
  25. static Button btn14=new Button("C");
  26. static Button btn15=new Button("=");
  27.  
  28. public static void main(String args[])
  29. {
  30. frm.setLayout(null); //取消排版
  31. frm.setSize(200,200); //設定frm的大小
  32. frm.setLocation(100,100); //設定frm的初始位置
  33. frm.setBackground(Color.white); //設定frm得背景顏色
  34. frm.setResizable(false); //設定frm不可以再調整大小
  35.  
  36. lab.setBounds(20,30,160,20); //設定lab的位置與大小
  37. lab.setBackground(new Color(255,230,200)); //設定lab得背景顏色
  38. //介面一 0~9、c、=
  39. pnl.setBounds(20,60,120,120); //設定pnl的位置與大小
  40. pnl.add(btn7); //將按鍵加入介面pnl
  41. pnl.add(btn8);
  42. pnl.add(btn9);
  43. pnl.add(btn4);
  44. pnl.add(btn5);
  45. pnl.add(btn6);
  46. pnl.add(btn1);
  47. pnl.add(btn2);
  48. pnl.add(btn3);
  49. pnl.add(btn0);
  50. pnl.add(btn14);
  51. pnl.add(btn15);
  52. //介面二 + - * /
  53. pnl2.setBounds(140,60,40,120); //設定pnl2的位置與大小
  54. pnl2.add(btn10); //將按鍵加入pnl2
  55. pnl2.add(btn11);
  56. pnl2.add(btn12);
  57. pnl2.add(btn13);
  58. //註冊
  59. btn0.addActionListener(frm); //將frm向按鍵註冊
  60. btn1.addActionListener(frm);
  61. btn2.addActionListener(frm);
  62. btn3.addActionListener(frm);
  63. btn4.addActionListener(frm);
  64. btn5.addActionListener(frm);
  65. btn6.addActionListener(frm);
  66. btn7.addActionListener(frm);
  67. btn8.addActionListener(frm);
  68. btn9.addActionListener(frm);
  69. btn10.addActionListener(frm);
  70. btn11.addActionListener(frm);
  71. btn12.addActionListener(frm);
  72. btn13.addActionListener(frm);
  73. btn14.addActionListener(frm);
  74. btn15.addActionListener(frm);
  75.  
  76. frm.add(lab); //將lab加入frm
  77. frm.add(pnl); //將pnl加入frm
  78. frm.add(pnl2); //將pbl2加入frm
  79.  
  80. frm.setVisible(true);
  81. //關閉
  82. frm.addWindowListener(new WindowAdapter(){
  83. public void windowClosing(WindowEvent e){System.exit(0);}});
  84. }
  85.  
  86. public int operandl=0; //保留準備運算的值
  87. public String str=""; //輸入控制 將輸入用字串串起
  88. public String op=""; //記錄運算子
  89. public void actionPerformed(ActionEvent e)
  90. {
  91. Button btn=(Button) e.getSource(); //讀取輸入的button
  92. //運算
  93. if(btn==btn10||btn==btn11||btn==btn12||btn==btn13||btn==btn15)//按下+-*/=時
  94. {
  95. int number=Integer.parseInt(lab.getText());
  96. switch(op) //若之前有保留運算的先運算 op!=0
  97. {
  98. case "+": // 加
  99. number = operandl+number;
  100. break;
  101. case "-": // 減
  102. number = operandl-number;
  103. break;
  104. case "*": // 乘
  105. number = operandl*number;
  106. break;
  107. case "/": // 除
  108. if(number!=0) // 避免 /0
  109. number = operandl/number;
  110. break;
  111. }
  112. lab.setText(Integer.toString(number));
  113. op=btn.getLabel();
  114. if(op!="=")
  115. {
  116. operandl = number;
  117. str = "";
  118. }
  119. }
  120. //清除
  121. else if(btn==btn14)//按下c時
  122. {
  123. str="";
  124. operandl=0;
  125. op="";
  126. lab.setText("0.");
  127. }
  128. //數字
  129. else//按下0~9
  130. {
  131. int num=Integer.parseInt(btn.getLabel());
  132. if(num!=0 || str.length()!=0)//避免輸入的第一個字串為0
  133. {
  134. str+=btn.getLabel();
  135. lab.setText(str);
  136. }
  137. }
  138. }
  139. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:4: error: class app18_8b is public, should be declared in a file named app18_8b.java
public class app18_8b extends Frame implements ActionListener
       ^
1 error
stdout
Standard output is empty