📄 calculator.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import sun.audio.*;
import java.io.*;
import java.util.*;
class frame
{
//用于框架的各个变量
JFrame myframe;//边框类的对象
JTextArea showresult;//多行文本框的对象
JButton button[]=new JButton[20];
JButton buttonx;
Container con;//容器类的对象
String str="123456789";
String strx="点击使用计算器";
JPanel panel1, panel2, panel3, panel4; //四个面板
CardLayout card;//定义卡片布局管理器的对象
JLabel label,labelx,labely;//定义label用来存放图片,labelx用来存放时间,labely用来存放日期
Dimension screen;//存放屏幕的大小
Icon picture;//定义一个图标变量用来存放图片
//用于计算的各个变量
int temp1=0,select,count1=0,count2=0,count3=0,leng,shuliang=0;double operator1=0.0,operator2=0.0;
char operater=' ';
String ss=" ";
InputStream in;
AudioStream as;
Font ziti;//设置字体
Time t;
public frame()
{
myframe=new JFrame("标准型计算器");
t=new Time();
t.start();
JMenuBar mb = new JMenuBar(); //菜单条
myframe.setJMenuBar(mb);
JMenu m1 = new JMenu("作者");//菜单
JMenuItem mi = new JMenuItem("孟瑞&&张瑞红"); //菜单项
mb.add(m1);
m1.add(mi);
labelx = new JLabel();//显示时间的标签
mb.add(labelx);
labely=new JLabel();//显示日期的标签
mb.add(labely);
labely.setText("日期:"+t.getdate());
ziti=new Font("楷体_GB2312",Font.BOLD,20);
//shuzi=new Font("Times New Roman",Font.BOLD,15);
buttonx=new JButton(strx);
buttonx.setForeground(Color.red);
buttonx.setFont(ziti);
con = myframe.getContentPane(); //获取内容窗格
card=new CardLayout();
con.setLayout(card);
picture=new ImageIcon("xiao.gif");
label = new JLabel(picture); //创建带有指定图片的标签
panel4=new JPanel(new BorderLayout());
panel4.add(BorderLayout.CENTER,buttonx);
panel4.add(BorderLayout.NORTH,label);
panel3=new JPanel();
panel3.setLayout(new BorderLayout());
panel1=new JPanel();
panel2=new JPanel();
panel3.setBorder(BorderFactory.createLineBorder(Color.blue)); //设置面板3边框的颜色
panel2.setLayout(new GridLayout(5, 4, 5, 5));//面板2采用网格布局管理器
showresult=new JTextArea(2,24);
showresult.setEditable(false);
showresult.setBackground(Color.cyan);
//showresult.setFont(shuzi);
for(int i=0,j=0;i<12;i++)
{
if((i+1)%4==0)continue;
button[i]=new JButton(String.valueOf(str.charAt(j)));
j++;
}
button[3]=new JButton("+");button[7]=new JButton("-");
button[11]=new JButton("*");button[15]=new JButton("/");
button[12]=new JButton("0");button[13]=new JButton(".");
button[14]=new JButton("=");button[16]=new JButton("音乐");
button[17]=new JButton("退格");button[18]=new JButton("清零");button[19]=new JButton("退出");
eventhandler handler=new eventhandler();
buttonx.addActionListener(handler); //注册事件监听器
for(int i=0;i<20;i++)
{
button[i].addActionListener(handler);
button[i].setForeground(Color.red);
button[i].setBackground(Color.blue);
}
panel1.add(showresult);
for(int i=0;i<20;i++)
{
panel2.add(button[i]);
}
panel3.add(panel1,BorderLayout.NORTH);
panel3.add(panel2,BorderLayout.CENTER);
con.add("2",panel4);
con.add("1",panel3);
screen=Toolkit.getDefaultToolkit().getScreenSize();
myframe.setLocation((screen.width) / 2-125, screen.height-600);
myframe.setSize(280, 300);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myframe.setVisible(true);
while(true)
{
labelx.setText("时间:"+t.gettime());
try{
Thread.sleep(1000);
}catch(Exception e){}
}
}
class eventhandler implements ActionListener //事件处理类
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == buttonx) //若选择此按键,则进入第二个卡片
card.next(con);
if(e.getSource()!=buttonx&&e.getSource()!=button[13]&&e.getSource()!=button[3]&&e.getSource()!=button[7]&&e.getSource()!=button[11]&&e.getSource()!=button[14]&&e.getSource()!=button[15]&&e.getSource()!=button[16]&&e.getSource()!=button[17]&&e.getSource()!=button[18]&&e.getSource()!=button[19])
{
count1 = 0; count2 = 0; count3 = 0;
temp1 = Integer.parseInt(((JButton)e.getSource()).getLabel()); //对获取的数字进行处理
switch(temp1)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:showresult.setText(ss+String.valueOf(temp1));
ss=showresult.getText();
leng = ss.length();
break;
}
}
else if(e.getSource()==button[3])//加
{
count1++;
if(count1==2)
{
JOptionPane.showMessageDialog(panel2, "操作错误输入了两次操作数,请重新输入正确的数据:");
ss=" ";
}
else
{
operator2=Double.parseDouble(ss.trim());
switch(operater)
{
case '*':operator1*=operator2;operator2=operator1;break;
case '-':operator1-=operator2;operator2=operator1;break;
case '/':operator1/=operator2;operator2=operator1;break;
}
if(operater==' '||operater=='+')
operator1+=operator2;
//if(operator1==0)
//operator1=Double.parseDouble(ss.trim());
showresult.setText("");
operater='+';
ss=" ";
}
}
else if(e.getSource()==button[7])//减
{
count1++;
if(count1==2)
{
JOptionPane.showMessageDialog(panel2, "操作错误输入了两次操作数,请重新输入正确的数据:");
ss=" ";
}
else
{
if(operator2!=0)
{
operator2=Double.parseDouble(ss.trim());
switch(operater)
{
case '*':operator1*=operator2;operator2=operator1;break;
case '+':operator1+=operator2;operator2=operator1;break;
case '/':operator1/=operator2;operator2=operator1;break;
}
if(operater==' '||operater=='-')
operator1-=operator2;
}
if(operater==' '||operater=='-')
operator2=Double.parseDouble(ss.trim());
if(operator1==0)
operator1=Double.parseDouble(ss.trim());
showresult.setText("");
operater='-';
ss=" ";
}
}
else if(e.getSource()==button[11])//乘
{
count1++;
if(count1==2)
{
JOptionPane.showMessageDialog(panel2, "操作错误输入了两次操作数,请重新输入正确的数据:");
ss=" ";
}
else
{
operator2=Double.parseDouble(ss.trim());
switch(operater)
{
case '+':operator1+=operator2;operator2=operator1;break;
case '-':operator1-=operator2;operator2=operator1;break;
case '/':operator1/=operator2;operator2=operator1;break;
}
if(operater==' '||operater=='*')
operator1*=operator2;
if(operator1==0)
operator1=Double.parseDouble(ss.trim());
showresult.setText("");
operater='*';
ss=" ";
}
}
else if(e.getSource()==button[13])//小数点
{
count2++;
if(count2==2)
{
JOptionPane.showMessageDialog(panel2, "操作错误输入了两次小数点,请重新输入正确的数据:");
}
else
{
showresult.setText(ss+".");
ss=showresult.getText();
}
}
else if (e.getSource() == button[14])//等于号
{
switch(operater)
{
case '+':
operator2=Double.parseDouble(ss.trim());
operator1+=operator2;
showresult.setText(String.valueOf(operator1).trim());break;
case '-':
operator2=Double.parseDouble(ss.trim());
operator1-=operator2;
showresult.setText(String.valueOf(operator1).trim());break;
case '*':
operator2=Double.parseDouble(ss.trim());
operator1*=operator2;
showresult.setText(String.valueOf(operator1).trim());break;
case '/':
operator2=Double.parseDouble(ss.trim());
if(operator2==0)
{
JOptionPane.showMessageDialog(panel2, "除数为零,请重新输入除数:");
showresult.setText("");
ss="";
}
else
{
operator1/=operator2;
showresult.setText(String.valueOf(operator1).trim());
}
break;
}
}
else if(e.getSource()==button[15])//除
{
count1++;
if(count1==2)
{
JOptionPane.showMessageDialog(panel2, "操作错误输入了两次操作数,请重新输入正确的数据:");
ss=" ";
}
else
{
if(operator2==0)
operator2=1;
else if(operator1!=0)
{
operator2=Double.parseDouble(ss.trim());
switch(operater)
{
case '*':operator1*=operator2;operator2=operator1;break;
case '+':operator1+=operator2;operator2=operator1;break;
case '-':operator1-=operator2;operator2=operator1;break;
}
if(operater==' '||operater=='/')
operator1/=operator2;
}
if(operater==' '||operater=='/')
{
operator2=Double.parseDouble(ss.trim());
}
if(operator1==0)
operator1=Double.parseDouble(ss.trim());
//if(operator2==0)
//JOptionPane.showMessageDialog(panel2, "除数为零,请重新输入:");
showresult.setText("");
operater='/';
ss=" ";
}
}
else if(e.getSource()==button[16])//play
{
try{
in=new FileInputStream("1.wav");
as=new AudioStream(in);
}
catch(Exception a){}
//button[16].setLabel("stop");
//System.out.println(button[16].getLabel());
if (shuliang%2==0)
{
AudioPlayer.player.start(as);
button[16].setLabel("停止");
}
if (shuliang % 2 != 0)
{
//AudioPlayer.player.stop(as);
button[16].setLabel("音乐");
AudioPlayer.player.stop(as);
}
shuliang++;
}
else if(e.getSource()==button[17])//退格
{
count3++;
StringBuffer str = new StringBuffer(ss);
str = str.deleteCharAt(leng-count3);
ss = str.toString();
showresult.setText(ss);
}
else if(e.getSource()==button[18])//清零
{
showresult.setText("");
temp1=0;
operator1=0;
operator2=0;
ss=" ";
operater=' ';
count3 = 0;
}
else if(e.getSource()==button[19])//退出
{
select=JOptionPane.showConfirmDialog(panel2, "确定要退出吗?","确定",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(select==JOptionPane.OK_OPTION)
System.exit(0);
}
}
}
}
class Time extends Thread //响应时间类
{
int h = 0, m = 0, s = 0,year=0,month=0,day=0,week=0;
public void run()
{
Calendar c = Calendar.getInstance();
h = c.get(Calendar.HOUR_OF_DAY);
m = c.get(Calendar.MINUTE);
s = c.get(Calendar.SECOND);
year=c.get(Calendar.YEAR);
month=c.get(Calendar.MONTH)+1;
day=c.get(Calendar.DAY_OF_MONTH);
week=c.get(Calendar.WEEK_OF_MONTH)-1;
while (true)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e) { }
if (s < 59)
s += 1;
else if (m < 59)
{
s = 0;
m += 1;
}
else if(h<23)
{
s = 0;
m = 0;
h+=1;
}
else
{ h = 0; m = 0; s = 0; }
}
}
String gettime(){
return String.valueOf(h)+":"+String.valueOf(m)+":"+String.valueOf(s);
}
String getdate()
{
return String.valueOf(year)+"-"+String.valueOf(month)+"-"+String.valueOf(day)+"星期:"+week;
}
}
public class calculator//计算器类
{
public static void main(String args[])
{
frame f=new frame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -