黑马程序员技术交流社区
标题:
自制java计算器
[打印本页]
作者:
Leejuen
时间:
2013-5-11 12:18
标题:
自制java计算器
看了毕老师的图形用户界面自己做的
QQ截图20130511121719.png
(9.75 KB, 下载次数: 0)
下载附件
2013-5-11 12:16 上传
不过用了swing。略渣哈哈
MyCalculator
复制代码
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)
2013-5-11 12:18 上传
点击文件名下载附件
作者:
Jacky_Chen1990
时间:
2013-5-11 12:32
支持下。
作者:
李德全
时间:
2013-5-11 12:50
顶顶顶 羡慕会图形化的大神。现在只会控制台...{:soso_e130:}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2