4つの第2の動作

輸入javax.swing.JButtonの。
輸入java.awt.Graphics;
輸入java.awt.FlowLayout;
輸入javax.swing.JFrame;
輸入javax.swing.JTextField。
インポートをjava.awt.Color;
輸入java.awt.Containerの。
輸入java.awt.Dimension。
輸入javax.swing.JPanel。
輸入いるjava.awt.font。
輸入java.awt.event.ActionListener。
輸入java.awt.event.ActionEvent。
公共のクラスのコンピュータは、JFrameの{延びている
コンピュータ(){
スーパー( "简易计算器")。
フォントF =新たなフォント( "黑体"、30、30)。
JTextFieldのJT1 =新しいJTextFieldの(NULL);
JTextFieldのJT2 =新しいJTextFieldの(NULL);
jt1.setFont(F)。
jt2.setFont(F)。
jt1.setHorizo​​ntalAlignment(JTextField.RIGHT)。
jt2.setHorizo​​ntalAlignment(JTextField.RIGHT)。
容器C = getContentPane()。
c.setLayout(新しいFlowLayoutの(FlowLayout.LEFT、5、5));
c.add(JT1)。
c.add(JT2)。
jt1.setPreferredSize(新しい次元(370、70));
jt2.setPreferredSize(新しい次元(370、75));
jt1.setEditable(偽);
jt2.setEditable(偽);
JButtonのJB [] =新しいJButtonの[16]。
(INT J = 0であり、j <16; J ++){ため
JB [J] =新しいJButtonの()。
JB [J] .setBackground(Color.WHITE)。
JB [J](偽).setFocusable。
JB [J] .setPreferredSize(新次元(90、90))。
JB [J] .setFont(F)。
c.add(JB [J])。
}
this.setResizable(偽)。
//对各个按钮的名称赋值
JB [0] .setText( "+")。
JB [1] .setText( " - ")。
JB [2] .setText( "*")。
JB [3] .setText( "/")。
JB [4] .setText( "1")。
JB [5] .setText( "2")。
JB [6] .setText( "3")。
JB [7] .setText( "C")。
JB [7] .setBackground(Color.ORANGE)。
JB [8] .setText( "4")。
JB [9] .setText( "5")。
JB [10] .setText( "6")。
JB [11] .setText( "0")。
JB [12] .setText( "7")。
JB [13] .setText( "8")。
JB [14] .setText( "9")。
JB [15] .setText( "=")。

JButtonのButton_num [] = {
JB [4]、
[5] JB、
JB [6]、
JB [8]、
JB [9]、
JB [10]、
JB [11]、




{ため(; iがButton_num.lengthを<I ++は、I = 0 INT)
Button_num [i]が.addActionListener(新規のActionListener(){
公共ボイドのactionPerformed(のActionEvent e)の{
JButtonのaction_Button =(JButtonの)e.getSource();
JT2。 setText(jt2.getText()+ action_Button.getText());
}
})。
}
(INT J = 0; J <4; J ++){ため
JB [J] .addActionListener(新規のActionListener(){
公共ボイドのactionPerformed(のActionEvent e)の{
文字列S = jt2.getText();
CHAR CH = s.charAt (s.length() - 1)、

IF(CH == '+' || CH == ' - ' || CH == '*' || CH == '/')((JButtonの)e.getSource ())は、setEnabled(偽);
他{
jt1.setText(jt2.getText());
。jt2.setText(((JButtonの)e.getSource())のgetText())。


}
})。
}
JB [7] .addActionListener(新規のActionListener(){
公共ボイドのactionPerformed(のActionEvent e)の{
jt1.setText(NULL);
jt2.setText(NULL);
}
}

)。
JB [15] .addActionListener(新規のActionListener(){
公共ボイドのactionPerformed(のActionEvent e)の{
試みる{
文字列Calculate_String = jt1.getText()+ jt2.getText();
int型の結果= calculation.result(Calculate_String);
jt1.setText (NULL);
jt2.setText(持つInteger.toString(結果));
}キャッチ(EXはArithmeticException){
jt1.setText(NULL);
jt2.setText( "ERROR");
}
}
})。
}
公共の静的な無効メイン(文字列引数[]){
コンピュータアプリ=新しいコンピュータ()。
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)。
app.setSize(400、580)。
app.setVisible(真の);
}
}

//再定义一个计算类calculation.java:
パブリッククラス算出{
演算(){}。
ブールis_operator(チャーCH){
IF(CH == '+' || CH == ' - ' || CH == '*' || CH == '/')trueを返します。
そうでない場合はfalseを返します。
}
静的int型の結果(文字列S){
文字列CH [] = {
"+"、
" - "、
"*"、
"/"
}。
int型私= 0;
int型のインデックス。
(s.indexOf(CH [i])と、一方== -1 && iが<
4){ iが++します;
}
(I == 4)戻った場合は- 1。
他インデックス= s.indexOf(CH [I])。
ストリングS1 = s.substring(0、インデックス)。
ストリングS2 = s.substring(インデックス+ 1、s.length())。
IF(I == 0)Integer.parseInt(S1)+ Integer.parseInt(S2)を返します。
(I == 1)Integer.parseIntを返した場合(S1) - Integer.parseInt(S2)。
(I == 2)Integer.parseInt(S1)を返す場合* Integer.parseInt(S2)。
場合(!私の==は3 && Integer.parseInt(S2)= 0)を返すInteger.parseInt(S1)/ Integer.parseInt(S2)。
それ以外の場合(私の== 3 && Integer.parseInt(S2)== 0){
)(新しいはArithmeticExceptionを投げます。
}
リターン- 1。
}

}

おすすめ

転載: www.cnblogs.com/lee18/p/11010798.html