做这个东西主要是为了练习一下以前学习过的java Swing,所以那些复杂的算法就没有加载到里面去........
先展示一下效果....,额,当然不是这个样子....
---》
代码:
1 package dialog_color; 2 3 import java.awt.BorderLayout; 4 import java.awt.Color; 5 import java.awt.Container; 6 import java.awt.FlowLayout; 7 import java.awt.Font; 8 import java.awt.GridLayout; 9 import java.awt.Insets; 10 import java.awt.event.ActionEvent; 11 import java.awt.event.ActionListener; 12 import java.math.BigInteger; 13 import java.util.ArrayList; 14 import java.util.List; 15 import java.util.Stack; 16 import java.util.regex.Matcher; 17 import java.util.regex.Pattern; 18 19 import javax.swing.BorderFactory; 20 import javax.swing.ImageIcon; 21 import javax.swing.JButton; 22 import javax.swing.JFrame; 23 import javax.swing.JLabel; 24 import javax.swing.JPanel; 25 import javax.swing.JTextField; 26 import javax.swing.UIManager; 27 /*简易计算器*/ 28 public class Simple_jisuanqi { 29 public static void main(String args []) { 30 31 Caculate mycacu = new Caculate(); 32 // Container con =mycacu.getContentPane(); 33 mycacu.setTitle("GXjun计算器"); 34 35 } 36 } 37 class Caculate extends JFrame implements ActionListener 38 { 39 Container mycon; //设置重量级容器 40 JPanel jpanel_1,jpanel_2,jpanel_3; //设置轻量级布局 41 myJButton button []= new myJButton [20]; 42 JTextField mytext; 43 JLabel label; 44 GridLayout grid = new GridLayout(4,4); 45 Caculate(){ //构造函数 46 mycon = getContentPane(); 47 jpanel_1 = new JPanel(); /*默认的格式为FlowLayout*/ 48 jpanel_2 =new JPanel(); 49 jpanel_3 = new JPanel(); 50 jpanel_2.setLayout(grid); 51 mytext = new JTextField(14); 52 mytext.setEditable(false); //设置成为不能 53 mytext.setFont(new Font("Arial",Font.BOLD,22)); 54 /* for(int i=0;i<20;i++) 55 button[i]=new myJButton(null);*/ 56 button[0] = new myJButton(" 后退 ",22); 57 button[0].addActionListener(this); 58 button[1] = new myJButton(" 归零 ",22); 59 button[1].addActionListener(this); 60 jpanel_1.add(button[0]); 61 jpanel_1.add(mytext); 62 jpanel_1.add(button[1]); 63 label = new JLabel(new ImageIcon("C:\\Users\\Administrator.USERSWO-MKA59UH\\Desktop\\java\\bin\\dialog_color\\a.jpg")); 64 // label = new JLabel(new ImageIcon("D:\\2\\2.gif")); 65 jpanel_3.add(label); 66 for(int i=2;i<12;i++) 67 button[i]=new myJButton(" "+i+" ",26); 68 char str[][]={{‘7‘,‘8‘,‘9‘,‘/‘},{‘4‘,‘5‘,‘6‘,‘*‘},{‘1‘,‘2‘,‘3‘,‘-‘},{‘0‘,‘.‘,‘+‘,‘=‘}}; 69 for(int i=0;i<4;i++) 70 for(int j=0;j<4;j++){ 71 button[2+i*4+j] =new myJButton(" "+str[i][j]+" ",26); 72 button[2+i*4+j].addActionListener(this); 73 jpanel_2.add(button[2+i*4+j]); 74 } 75 mycon.add(jpanel_1,BorderLayout.CENTER); 76 mycon.add(jpanel_3,BorderLayout.NORTH); 77 mycon.add(jpanel_2,BorderLayout.SOUTH); 78 init(); 79 } 80 void init() /*相关组件的一系列的初始化*/ 81 { 82 setBounds(100, 100, 477, 280); 83 setVisible(true); 84 setResizable(false); 85 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 86 } 87 String str =""; 88 @Override 89 public void actionPerformed(ActionEvent e) { 90 // TODO Auto-generated method stub 91 //System.out.println(e.getActionCommand()); 92 if(e.getSource()==button[0]||e.getSource()==button[1]) 93 { 94 if(e.getSource()==button[0]){ 95 System.out.println(str); 96 if(!str.isEmpty()){ 97 str=str.substring(0, str.length()-1) ; //后退一格 98 } 99 } 100 else str=""; 101 } 102 else 103 { 104 if(e.getSource()==button[17]){ 105 //等于号的时候,又该如何处理? 正则表达式 106 //System.out.println(e.getActionCommand()); 107 /*System.out.println(sove_calc(str));*/ 108 Double judge=sove_calc(str.trim()); 109 if(judge==Double.MAX_VALUE) 110 { 111 mytext.setText("error!"); 112 validate(); 113 return; 114 } 115 else str=judge.toString(); 116 } 117 else{ 118 str+=e.getActionCommand().toString().trim(); 119 } 120 } 121 if(str.isEmpty()) mytext.setText("0"); 122 else mytext.setText(str); 123 validate(); 124 } 125 private Double sove_calc(String str) 126 { 127 /*怎么提起出字符串里面的运算符号呢*/ 128 Pattern mypa = Pattern.compile("\\+|\\-|\\*|\\/"); 129 Matcher mtma = mypa.matcher(str); 130 List<String> listhoge = new ArrayList(); 131 while(mtma.find()) listhoge.add(mtma.group()); 132 String [] hoge = listhoge.toArray(new String [0]); //获取运算符号 133 /*突然遇到一个问题怎么从字符串提起浮点型数据呢?*/ 134 Pattern mypart = Pattern.compile("\\.\\d|\\d+(\\.\\d+)?"); 135 Matcher mtmatcher =mypart.matcher(str); 136 List<String> listval = new ArrayList(); 137 while(mtmatcher.find()) 138 listval.add(mtmatcher.group()); 139 String [] val=listval.toArray(new String[0]); 140 double sum=Double.parseDouble(val[0]); 141 for( int i=0;i<hoge.length;i++) 142 { 143 System.out.println(hoge[i].charAt(0)); 144 switch(hoge[i].charAt(0)) 145 { 146 case ‘+‘: sum += Double.parseDouble(val[i+1]); break; 147 case ‘-‘: sum -= Double.parseDouble(val[i+1]); break; 148 case ‘*‘: sum *= Double.parseDouble(val[i+1]); break; 149 case ‘/‘: 150 { 151 Double tem=Double.parseDouble(val[i+1]); 152 if(tem.equals(0)) return Double.MAX_VALUE; 153 sum/=tem; 154 break; 155 } 156 default : System.out.println("error!"); break; 157 } 158 } 159 return sum; 160 } 161 } 162 class myJButton extends JButton 163 { 164 public myJButton(String name,int size) 165 { 166 // TODO Auto-generated constructor stub 167 super(name); 168 setFont(new Font("仿宋",Font.BOLD,size)); 169 setMargin(new Insets(2,2,2,2)); 170 setBorder(BorderFactory.createRaisedBevelBorder()); 171 setBackground(Color.LIGHT_GRAY); 172 } 173 }
java设计之简单的JAVA计算器,布布扣,bubuko.com
时间: 2024-12-28 10:47:25