标签:计算机
package cn.hncu.gaming; import java.awt.Color; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class CopyOfMyReferenceGame extends JFrame implements ActionListener { JButton[] btns; JTextField tfd; String str = "",bite = "+";//bite默认符号位为加法运算 Double sum = 0.0; Color color = Color.green; public CopyOfMyReferenceGame(){ super("计算机"); setBounds(400, 240, 260, 260); {//在北面加一个文本行 JPanel pan1 = new JPanel(); getContentPane().add(pan1,"North"); pan1.setLayout(new FlowLayout(FlowLayout.RIGHT)); tfd = new JTextField("0",20); tfd.setHorizontalAlignment(JTextField.RIGHT); tfd.setEditable(true); pan1.add(tfd); } {//加计算机按钮 MyCalculator(); } setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } private void MyCalculator() {//加四个功能+ - * / JPanel calculatorPanel = new JPanel(); getContentPane().add(calculatorPanel); {//加按钮 JPanel pan2 = new JPanel(); getContentPane().add(pan2); pan2.setLayout(new GridLayout(5,4)); String[] calculatorLabel = { "color"," "," ","C", "7","8","9","+", "4","5","6","*", "1","2","3","/", "-","0",".","=" }; btns = new JButton[21];//用1-16号,0号暂时不用,可做颜色按钮。 for (int i = 1; i < btns.length; i++) { btns[i] = new JButton(calculatorLabel[i-1]); pan2.add(btns[i]); btns[i].addActionListener(this); } } } public static void main(String[] args) { new CopyOfMyReferenceGame(); } @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("+")){ calculate("+"); tfd.setText(str = ""); bite = "+"; }else if(e.getActionCommand().equals("-")){ calculate("-"); tfd.setText(str = ""); bite = "-"; }else if(e.getActionCommand().equals("*")){ calculate("*"); tfd.setText(str = ""); bite = "*"; }else if(e.getActionCommand().equals("/")){ calculate("/"); tfd.setText(str = ""); bite = "/"; }else if(e.getActionCommand().equals("=")){ calculate("="); tfd.setText(str = ""+sum); sum = 0.0; bite = "+"; }else if(e.getActionCommand().equals(".")){ str += "."; tfd.setText(str); }else if(e.getActionCommand().equals("C")){ sum = 0.0; tfd.setText(str = ""); bite = "+"; }else if(e.getActionCommand().equals(" ")){ }else if(e.getActionCommand().equals("color")){ color = JColorChooser.showDialog(this, "颜色选择", Color.green); for (int i = 1; i < btns.length; i++) { btns[i].setForeground(color); } }else{ str += e.getActionCommand(); tfd.setText(str); if(str.length()>5){ JOptionPane.showMessageDialog(this, "输入位数大于5,请重新输入!!!"); tfd.setText(str = ""); } } } private void calculate(String string){//给个计算命令 if(string.equals("=")){ if(bite.equals("+")){ sum += Double.parseDouble(str); }else if(bite.equals("-")){ sum -= Double.parseDouble(str); }else if(bite.equals("*")){ sum *= Double.parseDouble(str); }else if(bite.equals("/")){ sum /= Double.parseDouble(str); } }else{ sum = Double.parseDouble(str); } } }
标签:计算机
原文地址:http://blog.csdn.net/hncu1306602liuqiang/article/details/46390265