看了毕老师的图形用户界面自己做的
不过用了swing。略渣哈哈- package MyCalculator;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class CalFrame extends JFrame{
- //private static JPanel calP= new JPanel();
- private JPanel calPanel=new JPanel();
- private JPanel tePanel=new JPanel();
- private JPanel buPanel=new JPanel();
- private String[] myButton={"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"};
- private JButton[] jb=new JButton[myButton.length];
- private JTextField caltext = new JTextField("0",20);
- private String str,m,op=null;
- private boolean point=true,flag=true;
- public void iniCalFrame()
- {
- iniCalPanel();
- this.setSize(180,240);
- this.setResizable(false);
- this.setLocation(300,200);
- this.add(calPanel);
- setVisible(true);
- }
- private void iniCalPanel()
- {
- iniTePanel();
- iniBuPanel();
- calPanel.setLayout(new BorderLayout(10, 1));
- calPanel.add(tePanel,BorderLayout.NORTH);
- calPanel.add(buPanel,BorderLayout.CENTER);
- }
- private void iniTePanel()
- {
- tePanel.setLayout(new BorderLayout());
- caltext.setEditable(false);
- caltext.setBackground(Color.WHITE);
- caltext.setHorizontalAlignment(JTextField.RIGHT); //设置文本右对齐
- tePanel.add(caltext,BorderLayout.NORTH);
- }
- private void iniBuPanel()
- {
- buPanel.setLayout(new GridLayout(4,4,5,5));
- for( int i=0;i<jb.length;i++)
- {
- jb[i]= new JButton(myButton[i]);
- if(myButton[i].matches("[0-9]"))
- {
- jb[i].addActionListener(new GetData(myButton[i]));
- }
- else if(myButton[i].equals("."))
- {
- jb[i].addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- str=caltext.getText();
- if(point)
- {
- caltext.setText(str+".");
- point=false;
- }
- }
- });
- }
- else if(myButton[i].equals("+")||myButton[i].equals("-")||myButton[i].equals("*")||myButton[i].equals("/"))
- {
- jb[i].addActionListener(new GetOp(myButton[i]));
- }
- else if(myButton[i].equals("="))
- {
- jb[i].addActionListener(new GetAw ());
- }
- jb[i].setMargin(new Insets(0,0,0,0));
- buPanel.add(jb[i]);
- }
- }
- class GetData implements ActionListener
- {
- private String mb;
- public GetData(String mb)
- {
- this.mb=mb;
- }
- public void actionPerformed(ActionEvent e)
- {
- str=caltext.getText();
- if(flag)
- {
- str=mb;
- flag=false;
- }
- else
- {
- str=str+mb;
- }
- caltext.setText(str);
- }
- }
- class GetOp implements ActionListener
- {
- private String mb;
- public GetOp(String mb)
- {
- this.mb=mb;
- }
- public void actionPerformed(ActionEvent e)
- {
- m=caltext.getText();
- op=mb;
- point=true;
- flag=true;
- }
- }
- class GetAw implements ActionListener
- {
- private String temp;
- public void actionPerformed(ActionEvent e)
- {
- temp=caltext.getText();
- float a,b;
- a=Float.parseFloat(m);
- b=Float.parseFloat(temp);
- if(op=="+") a+=b;
- else if(op=="-") a-=b;
- else if(op=="*") a*=b;
- else if(op=="/") a/=b;
- caltext.setText((new Float(a)).toString());
- point=true;
- flag=true;
- }
- }
- }
复制代码
MyCalculator.zip
(1.67 KB, 下载次数: 13)
|