文章出處
文章列表
package yz; //引入類 import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.math.BigDecimal; public class Yz extends JFrame implements ActionListener { JPanel jsq; JTextField txt; JButton[] Nums; int f= 0 ; Double g= 0.0 ,h= 0.0 ,j= 0.0 ; String fh= "" ; public static void main(String[] args) { Yz y= new Yz(); } public Yz(){ jsq= new JPanel(); jsq.setLayout( new GridLayout( 5 , 4 , 4 , 4 )); Nums= new JButton[ 20 ]; String []a={ "←" , "CE" , "C" , "%" , "7" , "8" , "9" , "/" , "4" , "5" , "6" , "*" , "1" , "2" , "3" , "-" , "." , "0" , "=" , "+" }; for ( int i= 0 ;i<Nums.length;i++){ Nums[i]= new JButton(a[i]); jsq.add(Nums[i]); Nums[i].addActionListener( this ); //注冊監聽 } txt= new JTextField( 10 ); txt.setText( "" ); txt.setEditable( false ); txt.setRequestFocusEnabled( false ); txt.setHorizontalAlignment(JTextField.RIGHT); this .add(txt,BorderLayout.NORTH); this .add(jsq); this .setVisible( true ); //顯示窗體 this .setSize( 300 , 300 ); //窗體大小 this .setLocation( 550 , 300 ); //出現位置 this .setTitle( "計算器" ); //窗體標題 this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關閉進程 } //監聽 public void actionPerformed(ActionEvent e){ String a=e.getActionCommand(); int b= "0123456789" .indexOf(a); int c= "+-*/%" .indexOf(a); if (b!=- 1 ){ //如果按下的是數字 if (f== 0 ){ txt.setText(a); f= 1 ; } else { String d = txt.getText(); txt.setText(d+a); } } else if (c!=- 1 ) //如果按下的是運算符 { if (c== 1 &&f== 0 ){ g= 0.0 ; txt.setText(a); f= 1 ; } else if (txt.getText().equals( "" )){ JOptionPane.showMessageDialog( null , "錯誤" , "不能為零請重新輸入" , JOptionPane.ERROR_MESSAGE); } else { g=Double.valueOf(txt.getText()); fh=a; txt.setText( "" ); f= 0 ; } } else if ( "=" ==a){ //如果按下的是等號 if (txt.getText().equals( "" )){ h= 0.0 ; } else h=Double.valueOf(txt.getText()); if (fh== "+" ){ //如果按下的是加號 String d1 = String.valueOf(g+h).substring( 0 , String.valueOf(g+h).length()); double i; i=Double.valueOf(d1); int r=( int )i; if (r==Double.valueOf(d1)){ d1=d1.substring( 0 , d1.length()- 2 ); txt.setText(d1); } else txt.setText(d1); } if (fh== "-" ){ //如果按下的是減號 String d2 = String.valueOf(g-h).substring( 0 , String.valueOf(g-h).length()); double i; i=Double.valueOf(d2); if (i>= 0 ) { float r=( float )i; if (r==Double.valueOf(d2)){ d2=d2.substring( 0 , d2.length()- 2 ); txt.setText(d2); } else { txt.setText(d2); } } else { txt.setText( new BigDecimal(String.valueOf(g-h).substring( 0 , String.valueOf(g-h).length())).stripTrailingZeros().toString()); } } if (fh== "*" ){ //如果按下的是乘號 String d3 = String.valueOf(g*h).substring( 0 , String.valueOf(g*h).length()); double i; i=Double.valueOf(d3); int r=( int )i; if (r==i){ d3=d3.substring( 0 , d3.length()- 2 ); txt.setText(d3); } else { txt.setText(d3); } } if (fh== "/" ){ //如果按下的是除號 if (h== 0.0 ){ JOptionPane.showMessageDialog( null , "錯誤" , "除數不能為零請重新輸入" , JOptionPane.ERROR_MESSAGE); txt.setText( "" ); } else { String d4 = String.valueOf(g/h).substring( 0 , String.valueOf(g/h).length()); double i; i=Double.valueOf(d4); int r=( int )i; if (r==Double.valueOf(d4)){ d4=d4.substring( 0 , d4.length()- 2 ); txt.setText(d4); } else { txt.setText(d4); } } } if (fh== "%" ){ String n = txt.getText(); txt.setText(String.valueOf(g/ 100 )); } } else if ( "CE" ==a){ h= 0.0 ; txt.setText( "" ); } else if ( "C" ==a){ g= 0.0 ; h= 0.0 ; f= 0 ; txt.setText( "" ); } else if ( "." ==a){ String n = txt.getText(); if (txt.getText().trim().indexOf( "." )!=- 1 ) ; else txt.setText(n+ "." ); } else if ( "←" ==a){ String xs=txt.getText(); if (xs.length()>= 1 ) xs=xs.substring( 0 , xs.length()- 1 ); txt.setText(xs); } } }
|
文章列表
全站熱搜