您的位置:首页 > 编程语言 > Java开发

计算器界面的java代码

2013-12-01 14:15 120 查看
这算是自己第一次独立完成的java程序,小小纪念一下。

以下为Cal.java的内容

import java.awt.*;
import javax.swing.*;

public class Cal extends JFrame
{
private JTextField disp;
private JTextField none;
private JButton[] buttons1;
private JButton[] buttons2;
private JButton[] buttons3;

private JPanel panel1;
private JPanel panel2;
private JPanel panel3;

public Cal()
{
super();
this.Init();
}

public void Init()
{
this.setLayout(null);
disp=new JTextField();
disp.setLocation(5, 5);
disp.setSize(375, 30);

none=new JTextField();
none.setLocation(5, 40);
none.setSize(60, 35);
none.setEnabled(false);
none.setBackground(Color.GRAY);

this.add(disp);
this.add(none);
this.InitPanel1();
this.InitPanel2();
this.InitPanel3();

this.setTitle("计算器");
this.setVisible(true);
this.setSize(400,300);
}

public void InitPanel1()
{
String[] texts={"BackSpace","CE","C"};
panel1=new JPanel();
panel1.setSize(300,35);
panel1.setLocation(80,40);
buttons1=new JButton[3];
panel1.setLayout(new GridLayout(1,2,3,2));
for(int i=0;i<buttons1.length;i++)
{
buttons1[i]=new JButton(texts[i]);
panel1.add(buttons1[i]);
}

this.add(panel1);

}

public void InitPanel2()
{
String[] texts={"MC","MR","MS","M+"};
panel2=new JPanel();
panel2.setSize(60,170);
panel2.setLocation(5,85);
buttons2=new JButton[4];
panel2.setLayout(new GridLayout(4,2,1,2));
for(int i=0;i<buttons2.length;i++)
{
buttons2[i]=new JButton(texts[i]);
panel2.add(buttons2[i]);
}

this.add(panel2);
}

public void InitPanel3()
{
String[] texts={"7","8","9","/","sqrt",
"4","5","6","*","%",
"1","2","3","-","1/x",
"0","+/-",".","+","="};
panel3=new JPanel();
panel3.setSize(300,170);
panel3.setLocation(80,85);
buttons3=new JButton[20];
panel3.setLayout(new GridLayout(4,2,5,2));
for(int i=0;i<buttons3.length;i++)
{
buttons3[i]=new JButton(texts[i]);
panel3.add(buttons3[i]);
}

this.add(panel3);
}
}


以下为Test.java的内容

public class Test {
public static void main(String[] args) {
Cal cal=new Cal();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java