java界面编程初探
一个简易计算器的实现,只能对整数进行加减乘除运算(写的很乱)
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ButtonTest {
public static void main(String[] args) {
int[] num = new int[100];
boolean isys=false;
Event event=new Event();
event.btn_event();
}
}
class Event
{
private boolean isys=false;
private int[] num=new int[100];
Interface f=new Interface();
int ysfh=-1;
public void btn_event()
{
f.btn[0].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.num.setText(f.num.getText()+"1");
if(!isys)
num[0]=Integer.parseInt(f.num.getText());
else num[1]=Integer.parseInt(f.num.getText());
}
});
f.btn[1].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.num.setText(f.num.getText()+"2");
if(!isys)
num[0]=Integer.parseInt(f.num.getText());
else num[1]=Integer.parseInt(f.num.getText());
}
});
f.btn[2].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.num.setText(f.num.getText()+"3");
if(!isys)
num[0]=Integer.parseInt(f.num.getText());
else num[1]=Integer.parseInt(f.num.getText());
}
});
//加
f.btn[3].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.num.setText("");
isys=true;
ysfh=1;
}
});
f.btn[4].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.num.setText(f.num.getText()+"4");
if(!isys)
num[0]=Integer.parseInt(f.num.getText());
else num[1]=Integer.parseInt(f.num.getText());
}
});
f.btn[5].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.num.setText(f.num.getText()+"5");
if(!isys)
num[0]=Integer.parseInt(f.num.getText());
else num[1]=Integer.parseInt(f.num.getText());
}
});
f.btn[6].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.num.setText(f.num.getText()+"6");
if(!isys)
num[0]=Integer.parseInt(f.num.getText());
else num[1]=Integer.parseInt(f.num.getText());
}
});
//减
f.btn[7].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.num.setText("");
isys=true;
ysfh=2;
}
});
f.btn[8].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.num.setText(f.num.getText()+"7");
if(!isys)
num[0]=Integer.parseInt(f.num.getText());
else num[1]=Integer.parseInt(f.num.getText());
}
});
f.btn[9].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.num.setText(f.num.getText()+"8");
if(!isys)
num[0]=Integer.parseInt(f.num.getText());
else num[1]=Integer.parseInt(f.num.getText());
}
});
f.btn[10].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.num.setText(f.num.getText()+"9");
if(!isys)
num[0]=Integer.parseInt(f.num.getText());
else num[1]=Integer.parseInt(f.num.getText());
}
});
//乘
f.btn[11].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.num.setText("");
isys=true;
ysfh=3;
}
});
f.btn[12].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.num.setText(f.num.getText()+"0");
if(!isys)
num[0]=Integer.parseInt(f.num.getText());
else num[1]=Integer.parseInt(f.num.getText());
}
});
//清除
f.btn[13].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.num.setText("");
num[0]=0;
num[1]=0;
ysfh=-1;
isys=false;
}
});
//等于
f.btn[14].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(isys)
{
switch(ysfh)
{
case 1:{f.num.setText(Integer.toString(num[0]+num[1]));num[0]=0;num[1]=0;ysfh=-1;isys=false;}
case 2:{f.num.setText(Integer.toString(num[0]-num[1]));num[0]=0;num[1]=0;ysfh=-1;isys=false;}
case 3:{f.num.setText(Integer.toString(num[0]*num[1]));num[0]=0;num[1]=0;ysfh=-1;isys=false;}
case 4:{f.num.setText(Float.toString(num[0]/num[1]));num[0]=0;num[1]=0;ysfh=-1;isys=false;}
}
}
}
});
//除
f.btn[15].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.num.setText("");
isys=true;
ysfh=4;
}
});
f.show();
}
}
class Interface
{
public JButton[] btn = new JButton[20];
public JTextArea num=new JTextArea(1, 30);
private JFrame frame= new JFrame("My First Frame");
public Interface()
{
JPanel panel_input=new JPanel();//输入区域
JPanel panel_numbers=new JPanel();//数字区域
String[] str={"1","2","3","+","4","5","6","-","7","8","9","×","0","C","=","÷"};
//初始化窗体
frame.setSize(400, 300);
frame.setLayout(null);
//创建文本区域
num.setFont(new Font("黑体",4, 25));
panel_input.setBounds(0, 0, 400, 30);
panel_input.add(num,BorderLayout.WEST);
//按钮
for(int i=0;i<str.length;i++)
{
btn[i]=new JButton(str[i]);
panel_numbers.add(btn[i]);
}
panel_numbers.setBounds(0, 4``, 375, 250);
panel_numbers.setLayout(new GridLayout(5,4));
panel_numbers.setVisible(true);
frame.add(panel_input);
frame.add(panel_numbers);
}
public void show()
{
frame.setVisible(true);
}
}
运行截图: