初学Java,通过学习布局和事件监听做出一个简易计算器。实现过程还不算太难,但是有的细节需要注意,放上来希望和大家一起学习进步。
package example;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ui {
public String str1="",opt;
private JTextField display;
private double first,last,answer;
public ui(){
init();
}
public void init(){ //计算器布局
JFrame f=new JFrame("计算器");
f.setLayout(new BorderLayout()); //边框布局
f.add(creatpane1(),BorderLayout.NORTH); //布局1
f.add(creatpane2(),BorderLayout.CENTER);//布局2
f.setSize(300,300);
f.setLocation(400,200);
f.setVisible(true);
}
public JPanel creatpane1(){
JPanel jp1=new JPanel();
jp1.setLayout(new GridLayout());
display = new JTextField("0");
jp1.add(display);
return jp1;
}
private JPanel creatpane2(){
JPanel jp2=new JPanel();
jp2.setLayout(new GridLayout(4,4));//网格布局
JButton button1=new JButton("1");
JButton button2=new JButton("2");
JButton button3=new JButton("3");
JButton add=new JButton("+");
JButton button4=new JButton("4");
JButton button5=new JButton("5");
JButton button6=new JButton("6");
JButton sub=new JButton("-");
JButton button7=new JButton("7");
JButton button8=new JButton("8");
JButton button9=new JButton("9");
JButton mul=new JButton("*");
JButton floa=new JButton(".");
JButton button0=new JButton("0");
JButton equal=new JButton("=");
JButton div=new JButton("/");
jp2.add(button1);
jp2.add(button2);
jp2.add(button3);
jp2.add(add);
jp2.add(button4);
jp2.add(button5);
jp2.add(button6);
jp2.add(sub);
jp2.add(button7);
jp2.add(button8);
jp2.add(button9);
jp2.add(mul);
jp2.add(floa);
jp2.add(button0);
jp2.add(equal);
jp2.add(div);
button1.addActionListener(new ActionListener(){ //为按钮添加监听事件 , 下同。
public void actionPerformed(ActionEvent arg0) {
str1=str1+button1.getText();
display.setText(str1);
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
str1=str1+button2.getText();
display.setText(str1);
}
});
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
str1=str1+button3.getText();
display.setText(str1);
}
});
button4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
str1=str1+button4.getText();
display.setText(str1);
}
});
button5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
str1=str1+button5.getText();
display.setText(str1);
}
});
button6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
str1=str1+button6.getText();
display.setText(str1);
}
});
button7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
str1=str1+button7.getText();
display.setText(str1);
}
});
button8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
str1=str1+button8.getText();
display.setText(str1);
}
});
button9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
str1=str1+button9.getText();
display.setText(str1);
}
});
button0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
str1=str1+button0.getText();
display.setText(str1);
}
});
floa.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
str1=str1+".";
display.setText(str1);
}
});
add.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
opt="+";
setopt();
setshow("");
str1="";
getopt();
}
});
sub.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
opt="-"; //当前输入的操作
setopt();//将操作符之前的数保存起来
setshow("");//清空显示屏
str1="";//清空字符串为了输入下一个数字
getopt();//保存操作符
}
});
mul.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
opt="*";
setopt();
setshow("");
str1="";
getopt();
}
});
div.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
opt="/";
setopt();
setshow("");
str1="";
getopt();
}
});
equal.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
setanswer();//进行运算
str1="";//清空字符串,为下次运算准备
}
});
return jp2;
}
public Double getshow(String str) { //将显示屏上的字符串返回成double类型
return Double.valueOf(str);
}
public void setshow(String str){ //将数据发送到显示屏
display.setText(str);
}
public String getopt(){
return this.opt;
}
public void setopt(){
first=getshow(str1);
}
public void setanswer(){
last=getshow(str1);
if("+".equals(getopt()) )
{
answer=first+last;
setshow(String.valueOf(answer));
first=answer;//为下次输入操作符做准备
}
else if("-".equals(getopt()) ){
answer=first-last;
setshow(String.valueOf(answer));
first=answer;//为下次输入操作符做准备
}
else if("*".equals(getopt()) ){
answer=first*last;
setshow(String.valueOf(answer));
first=answer;//为下次输入操作符做准备
}
else if("/".equals(getopt())){
if(last==0) setshow("输入有误");
else {
answer=first/last;
setshow(String.valueOf(answer));
first=answer;//为下次输入操作符做准备
}
}
}
public static void main(String[] args)
{
new ui();
}
}