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

📄 calculator1.java

📁 这是用java实现的一个简单的计算器程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class calculator extends JFrame implements ActionListener{
 JFrame frame;
 private objConversion convert = new objConversion();//֮ת
 JMenuItem fileMenu,exitItemOfFile,s,t, about, me;
 JRadioButton sixteen,ten,eight,two;//ѡŤ
 JButton Back,ce,c,num0,num1,num2,num3,num4,num5,num6,num7,num8,num9;
 JButton a,b,cc,dd,ee,ff,jia,jian,cheng,chu,quyu,deng,fu,dian,kai,dao;
 Container cp;//ڼ
 JTextField text;
 String copycontent="";
 boolean clickable=true,clear=true;
 int all=0;
 double qian; 
 String fuhao;
 int jin=10,first=1;
public calculator() {
  setTitle("");
  setSize(300,300);
  setLocation(400,400);
  text=new JTextField(25);
  text.setHorizontalAlignment(JTextField.LEFT);//
  JPanel cp1=new JPanel();
  JPanel cp2=new JPanel();
  JPanel cp3=new JPanel();
  cp=getContentPane();
  cp.add(cp1,"North");
  cp.add(cp2,"Center");
  cp.add(cp3,"South");
  cp1.setLayout(new GridLayout(1,1));
  cp2.setLayout(new GridLayout(2,4));
  cp3.setLayout(new GridLayout(6,5));
sixteen=new JRadioButton("16");
  sixteen.setVisible(false);
  ten=new JRadioButton("10",true);
  ten.setVisible(false);
  eight=new JRadioButton("8");
  eight.setVisible(false); 
  two=new JRadioButton("2");
  two.setVisible(false);
  a = new JButton("A");
  a.setVisible(false);
  b = new JButton("B");
  b.setVisible(false);
  cc = new JButton("C");
  cc.setVisible(false);
  dd = new JButton("D");
  dd.setVisible(false);
  ee = new JButton("E");
  ee.setVisible(false);
  ff = new JButton("F");
  ff.setVisible(false);
  jia = new JButton("+");
  jian = new JButton("-");
  cheng = new JButton("*");
  chu = new JButton("/");
  quyu = new JButton("%");
  deng = new JButton("=");
  fu = new JButton("+/-");
  dian = new JButton(".");
  kai = new JButton("sqrt");
  dao = new JButton("1/x");
  num0=new JButton("0"); 
  num1=new JButton("1"); 
  num2=new JButton("2"); 
  num3=new JButton("3"); 
  num4=new JButton("4"); 
  num5=new JButton("5"); 
  num6=new JButton("6"); 
  num7=new JButton("7"); 
  num8=new JButton("8"); 
  num9=new JButton("9"); 
  sixteen.addActionListener(this);
  ten.addActionListener(this);
  eight.addActionListener(this);
  two.addActionListener(this);
  ButtonGroup btg=new ButtonGroup();//һ
  btg.add(sixteen); 
  btg.add(ten); 
  btg.add(eight); 
  btg.add(two);
  JTextField t3=new JTextField(25);
  cp1.add(text);
  text.setEditable(false);
  text.setBackground(Color.white);
  Back=new JButton("Back");//Back
  Back.setForeground(Color.red);
  Back.setBackground(Color.orange);
  Back.addActionListener(this);
  ce=new JButton("CE");//CE
  ce.setForeground(Color.red);
  ce.setBackground(Color.orange);
  ce.addActionListener(this);
  c=new JButton("C");//C
  c.setForeground(Color.red);
  c.setBackground(Color.orange);
  c.addActionListener(this);
  cp2.add(sixteen);
  cp2.add(ten);
  cp2.add(eight);
  cp2.add(two);
  cp2.add(Back);
  cp2.add(ce);
  cp2.add(c);
  cp3.add(num7);
  num7.setBackground(Color.yellow);
  num7.addActionListener(this);
  cp3.add(num8);
  num8.setBackground(Color.yellow);
  num8.addActionListener(this);
  cp3.add(num9);
  num9.setBackground(Color.yellow);
  num9.addActionListener(this);
  cp3.add(chu);
  chu.setForeground(Color.blue);
  chu.setBackground(Color.orange);
  chu.addActionListener(this);
  cp3.add(kai);
  kai.setForeground(Color.blue);
  kai.setBackground(Color.orange);
  kai.addActionListener(this);
  cp3.add(num4);
  num4.setBackground(Color.yellow);
  num4.addActionListener(this);
  cp3.add(num5);
  num5.setBackground(Color.yellow);
  num5.addActionListener(this);
  cp3.add(num6);
  num6.setBackground(Color.yellow);
  num6.addActionListener(this);
  cp3.add(cheng);
  cheng.setForeground(Color.blue);
  cheng.setBackground(Color.orange);
  cheng.addActionListener(this);
  cp3.add(quyu);
  quyu.setForeground(Color.blue);
  quyu.setBackground(Color.orange);
  quyu.addActionListener(this);
  cp3.add(num1);
  num1.setBackground(Color.yellow);
  num1.addActionListener(this);
  cp3.add(num2);
  num2.setBackground(Color.yellow);
  num2.addActionListener(this);
  cp3.add(num3);
  num3.setBackground(Color.yellow);
  num3.addActionListener(this);
  cp3.add(jian);
  jian.setForeground(Color.blue);
  jian.setBackground(Color.orange);
  jian.addActionListener(this);
  cp3.add(dao);
  dao.setForeground(Color.blue);
  dao.setBackground(Color.orange);
  dao.addActionListener(this);
  cp3.add(num0);
  num0.setBackground(Color.yellow);
  num0.addActionListener(this);
  cp3.add(fu);
  fu.setBackground(Color.yellow);
  fu.setForeground(Color.blue);
  fu.addActionListener(this);
  cp3.add(dian);
  dian.setForeground(Color.blue);
  dian.setBackground(Color.yellow);
  dian.addActionListener(this);
  cp3.add(jia);
  jia.setForeground(Color.blue);
  jia.setBackground(Color.orange);
  jia.addActionListener(this);
  cp3.add(deng);
  deng.setForeground(Color.red);
  deng.setBackground(Color.orange);
  deng.addActionListener(this);
  cp3.add(a);
  a.setForeground(Color.magenta);
  a.setBackground(Color.pink);
  a.addActionListener(this);
  cp3.add(b);
  b.setForeground(Color.magenta);
  b.setBackground(Color.pink);
  b.addActionListener(this);
  cp3.add(cc);
  cc.setForeground(Color.magenta);
  cc.setBackground(Color.pink);
  cc.addActionListener(this);
  cp3.add(dd);
  dd.setForeground(Color.magenta);
  dd.setBackground(Color.pink);
  dd.addActionListener(this);
  cp3.add(ee);
  ee.setForeground(Color.magenta);
  ee.setBackground(Color.pink);
  ee.addActionListener(this);
  cp3.add(ff);
  ff.setForeground(Color.magenta);
  ff.setBackground(Color.pink);
  ff.addActionListener(this);
  JMenuBar mainMenu = new JMenuBar();
  setJMenuBar(mainMenu); 
  JMenu editMenu = new JMenu("File"); 
  JMenu viewMenu = new JMenu("Edit"); 
  JMenu helpMenu = new JMenu("Help");
  mainMenu.add(editMenu);
  mainMenu.add(viewMenu);
  mainMenu.add(helpMenu);
  fileMenu = new JMenu("File");
  exitItemOfFile = new JMenuItem("Exit");
  fileMenu.addActionListener(this);
  exitItemOfFile.addActionListener(this);
  editMenu.add(fileMenu);
  editMenu.add(exitItemOfFile);
  t = new JMenuItem("Nomual"); 
  s = new JMenuItem("sitc"); 
  viewMenu.add(t);
  viewMenu.add(s);
  t.addActionListener(this);
  s.addActionListener(this);
  about = new JMenuItem("About");
  me = new JMenuItem("Call me");
  helpMenu.add(about);  
  helpMenu.add(me);
  about.addActionListener(this);
  me.addActionListener(this);
  addWindowListener(new WindowDestroyer());//
 }
       public void actionPerformed(ActionEvent e){//Ӧ
       if (first == 1)
              text.setText("");
       first = 0;//һΰı0.
       Object temp = e.getSource();
       if (temp == t){//׼
                     sixteen.setVisible(false);
                     ten.setVisible(false);
                     eight.setVisible(false);
                     two.setVisible(false);
                     t.setText("׼");
                     s.setText("    ѧ");
                     a.setVisible(false);
                     b.setVisible(false);
                     cc.setVisible(false);
                     dd.setVisible(false);
                     ee.setVisible(false);
                     ff.setVisible(false);
              }
       if (temp == s){//ѧ
                     sixteen.setVisible(true);
                     ten.setVisible(true);
                     eight.setVisible(true);
                     two.setVisible(true);
                     t.setText("   ׼");
                     s.setText("ѧ");
                     a.setVisible(true);
                     b.setVisible(true);
                     cc.setVisible(true);
                     dd.setVisible(true);
                     ee.setVisible(true);
                     ff.setVisible(true);
              }
       if (temp == about)
              JOptionPane.showMessageDialog(frame, "  http://qzome.327470428.com/", "ڼ", 
JOptionPane.INFORMATION_MESSAGE); ;
       if (temp == me){
              try{
                      Process  p=Runtime.getRuntime().exec("explorer http://qzome.327470428.com/");
                     }
              catch (Exception eeee){
                     System.out.println("ҳ");
                     }
              }
       try{
              if (temp == sixteen){
                            String hex = text.getText();
                            int inthex = Integer.parseInt(hex, jin);//ȰΪ10  
                            text.setText(convert.decHex(inthex));
                            jin = 16;
              }
              if (temp == eight){
                            String oct = text.getText();
                            int intoct = Integer.parseInt(oct, jin);
                            text.setText(convert.decOct(intoct));
                            jin = 8;
              }
              if (temp == two){
                            String bin = text.getText();
                            int intbin = Integer.parseInt(bin, jin);
                            text.setText(convert.decBin(intbin));
                            jin = 2;
              }
              if (temp == ten){
                            String dec = text.getText();
                            int intdec = Integer.parseInt(dec, jin);
                            text.setText(intdec + "");
                            jin = 10;
              }
       }

⌨️ 快捷键说明

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