⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compute.java

📁 一个简单的java应用程序
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.lang.Math.*;
public class Compute extends JFrame implements ActionListener
{
  private double sum1,sum2;
  private char fuhao;
  private JMenu bianji=new JMenu("编辑");  //设计菜单
  private JMenu help=new JMenu("帮助");
  private JMenuItem copy=new JMenuItem("复制");
  private JMenuItem zt=new JMenuItem("粘贴");
  private JMenuItem gy=new JMenuItem("关于");
  JMenuBar menubar;
  private JTextField text=new JTextField(32);   //结果区域
  JPanel pan2=new JPanel();
  JPanel pan3=new JPanel();  //设计面板
  JPanel pan4=new JPanel();
  JPanel pan5=new JPanel();
  Clipboard clipboard=null;
  //JButton nul=new JButton();
  JButton back=new JButton("Back");//第一行按钮
  JButton ce=new JButton("CE");
  JButton c=new JButton("C");
  JButton num1=new JButton("1");
  JButton num2=new JButton("2");//数据按钮
  JButton num3=new JButton("3");
  JButton num4=new JButton("4");
  JButton num5=new JButton("5");
  JButton num6=new JButton("6");
  JButton num7=new JButton("7");
  JButton num8=new JButton("8");
  JButton num9=new JButton("9");
  JButton num0=new JButton("0");
  JButton num10=new JButton("+/-");
  JButton num11=new JButton("=");
  JButton fh1=new JButton("+");
  JButton fh2=new JButton("-");//算数符号按钮
  JButton fh3=new JButton("*");
  JButton fh4=new JButton("/");
  JButton sqr=new JButton("sqr");
  JButton qy=new JButton("%");
  JButton ch=new JButton("1/x");
  JButton poin=new JButton(".");
  JButton sin=new JButton("SIN");
  JButton cos=new JButton("COS");
  JButton tan=new JButton("TAN");
  JButton log=new JButton("LOG");
  Compute()
  {
    setTitle("计算器");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    JPanel pan1=new JPanel();
    text.setHorizontalAlignment(JTextField.RIGHT);
    bianji.setForeground(Color.orange);
    help.setForeground(Color.orange);
    sin.setForeground(Color.red);
    cos.setForeground(Color.red);
    tan.setForeground(Color.red);
    log.setForeground(Color.red);
    back.setForeground(Color.red);
    ce.setForeground(Color.red);
    c.setForeground(Color.red);
    fh1.setForeground(Color.red);
    fh2.setForeground(Color.red);
    fh3.setForeground(Color.red);
    fh4.setForeground(Color.red);
    num11.setForeground(Color.red);
    pan1.setBackground(Color.orange);       //设置背景颜色
    pan2.setBackground(Color.orange);
    pan3.setBackground(Color.orange);
    pan4.setBackground(Color.orange);
    pan5.setBackground(Color.orange);
    pan2.setLayout(new GridLayout(4,5,5,5));   //布局各个面板
    pan3.setLayout(new GridLayout(1,4,35,5));
    pan4.setLayout(new BorderLayout(10,5));
    pan5.setLayout(new GridLayout(4,1,5,20));    
    back.addActionListener(this);
    ce.addActionListener(this);
    c.addActionListener(this);
    fh1.addActionListener(this);
    fh2.addActionListener(this);
    fh3.addActionListener(this);              //添加事件监听器
    fh4.addActionListener(this);
    num0.addActionListener(this);
    num1.addActionListener(this);
    num2.addActionListener(this);
    num3.addActionListener(this);
    num4.addActionListener(this);
    num5.addActionListener(this);
    num6.addActionListener(this);
    num7.addActionListener(this);
    num8.addActionListener(this);
    num9.addActionListener(this);
    num10.addActionListener(this);
    num11.addActionListener(this);
    poin.addActionListener(this);
    sqr.addActionListener(this);
    sin.addActionListener(this);
    cos.addActionListener(this);
    tan.addActionListener(this);
    log.addActionListener(this);
    copy.addActionListener(this);
    zt.addActionListener(this);
    gy.addActionListener(this); 
    qy.addActionListener(this);  
    ch.addActionListener(this);   
    bianji.add(copy);                           //菜单添加
    bianji.add(zt);
    help.add(gy);
    menubar=new JMenuBar();
    menubar.add(bianji);
    menubar.add(help);
    setJMenuBar(menubar);       
    //pan3.add(nul);
    pan3.add(back);
    pan3.add(ce);
    pan3.add(c);                      //向各个面板添加按钮
    pan2.add(num7);
    pan2.add(num8);
    pan2.add(num9);
    pan2.add(fh1);
    pan2.add(sqr);
    pan2.add(num4);
    pan2.add(num5);
    pan2.add(num6);
    pan2.add(fh2);
    pan2.add(qy);
    pan2.add(num1);
    pan2.add(num2);
    pan2.add(num3);
    pan2.add(fh3);
    pan2.add(ch);
    pan2.add(num0);
    pan2.add(num10);
    pan2.add(poin);
    pan2.add(fh4);
    pan2.add(num11);
    pan4.add(pan5,BorderLayout.WEST);
    pan4.add(pan2,BorderLayout.EAST);
    pan5.add(sin); 
    pan5.add(cos);
    pan5.add(tan); 
    pan5.add(log);
    //pan1.add(bianji);
    pan1.add(menubar);
    text.setEditable(false);
    pan1.add(text);
    pan1.add(pan3);
    pan1.add(pan4);
    setContentPane(pan1);
  }
  public static void main(String[] args)
  {
    JFrame frame=new Compute();
    //frame.isResizable(false);
    frame.setSize(400,330);
    frame.setVisible(true);
  }
  public void actionPerformed(ActionEvent e)
  {
    Object ob=e.getSource();
    try {
          if(ob==num1)
            text.setText(text.getText()+1);
          if(ob==num2)
            text.setText(text.getText()+2);
          if(ob==num3)
            text.setText(text.getText()+3);
          if(ob==num4)
            text.setText(text.getText()+4);
          if(ob==num5)
            text.setText(text.getText()+5);
          if(ob==num6)
            text.setText(text.getText()+6);
          if(ob==num7)
            text.setText(text.getText()+7);
          if(ob==num8)
            text.setText(text.getText()+8);
          if(ob==num9)
            text.setText(text.getText()+9);
          if(ob==num0)
            text.setText(text.getText()+0);
          if(ob==num10)
            text.setText('-'+text.getText());
          if(ob==poin)
            text.setText(text.getText()+'.');
          if(ob==fh1)
            sum1=Double.parseDouble(text.getText());
          if(ob==fh2)
            sum1=Double.parseDouble(text.getText());
          if(ob==fh3)
            sum1=Double.parseDouble(text.getText());
          if(ob==fh4)
            sum1=Double.parseDouble(text.getText());
          if(ob==back)
            {
               String str=text.getText();
              if(str.length()>1)
                 {
                 str=str.substring(0,str.length()-1);
                 text.setText(str);
                 }
              if(str.length()==1)
                {
                 str="0";
                 text.setText(str);
                }
           }
         if(ob==fh1)
               {sum1=Double.parseDouble(text.getText());
               text.setText("");
               fuhao='+';}
         else if(ob==fh2)
               {sum1=Double.parseDouble(text.getText());
               text.setText("");
               fuhao='-';}
         else if(ob==fh3)
               {sum1=Double.parseDouble(text.getText());
               text.setText("");
               fuhao='*';}
         else if(ob==fh4)
               {sum1=Double.parseDouble(text.getText());
               text.setText("");
               fuhao='/';}
         else if(ob==qy)
               {sum1=Double.parseDouble(text.getText());
               text.setText("");
               fuhao='%';}      
         if(ob==num11)
            {
               sum2=Double.parseDouble(text.getText());
               switch(fuhao)
               {
                  case '+' :
                      text.setText(String.valueOf((sum1+sum2)));
                      break;
                  case '-' :
                      text.setText(String.valueOf((sum1-sum2)));
                      break;
                  case '*' :
                      text.setText(String.valueOf((sum1*sum2)));
                      break;
                 case '/' :
                      text.setText(String.valueOf((sum1/sum2)));
                      break;
                 case '%' :
                      text.setText(String.valueOf((sum1%sum2)));
                      break;
               }
            }
          if(ob==ch)
          {  sum1=Double.parseDouble(text.getText());
             text.setText(String.valueOf((1/sum1)));
          }
         if(ob==c)
            {text.setText("");}
         else if(ob==ce)
            {text.setText("0");}
         
    }
    catch (Exception ex) {}
    if(e.getSource()==sqr)
    {  text.setText(String.valueOf(Math.sqrt(Double.parseDouble(text.getText()))));
    }
    else if(e.getSource()==sin)
    {  text.setText(String.valueOf(Math.sin(Double.parseDouble(text.getText()))));
    }
    else if(e.getSource()==cos)
    {  text.setText(String.valueOf(Math.cos(Double.parseDouble(text.getText()))));
    }
    else if(e.getSource()==tan)
    {  text.setText(String.valueOf(Math.tan(Double.parseDouble(text.getText()))));
    }
    if(e.getSource()==log)
    {  text.setText(String.valueOf(Math.log(Double.parseDouble(text.getText()))));
    }
    else if(e.getSource()==copy)
    {  String temp=text.getSelectedText();  
       StringSelection text=new StringSelection(temp);
       clipboard.setContents(text,null);
    }
    else if(e.getSource()==zt)
    {  Transferable contents=clipboard.getContents(this);
       DataFlavor  flavor= DataFlavor.stringFlavor;
       if( contents.isDataFlavorSupported(flavor))
       try{  String str;
       str=(String)contents.getTransferData(flavor);
       text.setText(str);
       }
      catch(Exception ee){}
     }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -