高手帮忙指点下新人哈!

解决方案 »

  1.   

    没有了   楼上的大哥
    这会才弄那些,离挂也就一层纸了我是最近想想学习点东西才想弄来看的我自己已经用AWT做了界面   可是   event   没看明白就想学习前辈的经验而已
      

  2.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.lang.Math;public class Jisuanqi extends JFrame implements ActionListener  {

    private JButton[]  btn1,btn2;
    private JTextField text;
    private JButton bq=new JButton("Çå¿Õ");
    private JButton bd=new JButton("=");
    private JButton square=new JButton("ƽ·½");
    private JButton kaifang=new JButton("¿ª·½");
    private JButton sin=new JButton("sin");
    private JButton cos=new JButton("cos");
    private JMenuBar mbar=new JMenuBar();
    private JMenu menu1,menu2;
    private JMenuItem mitem1,mitem2;
    //private BorderLayout boder =new  BoderLayout();
    private GridLayout grid=new GridLayout(4,4);
    private JPanel panel;
    Container cp;
    int a1=10,a2=4;
    String str=new String();
    String str1=new String();
    String str2=new String();
    String str3=new String();
    int i,j,c;
    boolean flag=true;
    double c1,c2,r;     public Jisuanqi(){
            
          btn1=new JButton[10];
          for(i=0;i<a1;i++){
          btn1[i]=new JButton(str.valueOf(i));
         
          }
          //mbar=new JMenuBar();
          setJMenuBar(mbar);
          menu1=new JMenu("HELP");
         
          menu2=new JMenu("exit");
          mbar.add(menu1);
          mbar.add(menu2);
          mitem1=new JMenuItem("Writer");
          mitem2=new JMenuItem("Introduction");
          menu1.add(mitem1);
          menu1.addSeparator();
          menu1.add(mitem2);
         
         
          btn2=new JButton[4];
          btn2[0]=new JButton("+");
          btn2[1]=new JButton("-");
          btn2[2]=new JButton("*");
          btn2[3]=new JButton("/");
         
          text=new JTextField("0",10);
          cp=getContentPane();
            cp.add(text,"North");
          panel=new JPanel();
          panel=new JPanel(grid);
          for(i=0;i<a1;i++){
            panel.add(btn1[i]);
            btn1[i].addActionListener(this);
            
          }
          for(j=0;j<4;j++){
          panel.add(btn2[j]);
         btn2[j].addActionListener(new operator());
          }
          panel.add(bq);
          panel.add(bd);
          panel.add(square);
          panel.add(kaifang);
          panel.add(sin);
          panel.add(cos);
         
          //cp.add(panel1,"North");
          cp.add(panel,"South");
          bq.addActionListener(this);
          bd.addActionListener(this);
         }
         public void actionPerformed(ActionEvent e1){
         
         
          for(i=0;i<a1;i++)
          {   
          if(e1.getSource()==btn1[i]){
          if(flag){
              str=str.valueOf(i);
              
              str1+=str;
              text.setText(str1);
              c1=Double.parseDouble(str1);
                 }
              else {
         
          str=str.valueOf(i);
          str2+=str;
              text.setText(str2);
          c2=Double.parseDouble(str2);
               }
          }  
          if(e1.getSource()==kaifang){
          double d=Double.parseDouble(text.getText());
          // d=d*d;
          text.setText(String.valueOf(Math.sqrt(d)));
         
          }
          if(e1.getSource()==bq)
          { text.setText(""); 
            str1=str=str2=""; 
            flag=true; 
            //str1="0";
           // text.setText(str1);
           // c1=0;
            } 
            }
            
            
            if(e1.getSource()==bd){
                r=0;
             switch(c){
             case 0: r=c1+c2;text.setText(str3.valueOf(r));str1=str3.valueOf(r);break;
             case 1: r=c1-c2;text.setText(str3.valueOf(r));str1=str3.valueOf(r);break;
             case 2: r=c1*c2;text.setText(str3.valueOf(r));str1=str3.valueOf(r);break;
             case 3: r=c1/c2;text.setText(str3.valueOf(r));str1=str3.valueOf(r);break;
             };
              //str1=str3;
                c1=Double.parseDouble(str1);
                str2=str3="";
            
            }
         }
         
         class operator implements ActionListener{
         public void actionPerformed(ActionEvent e2){
          for(j=0;j<a2;j++)
          {if(e2.getSource()==btn2[j])
            {c=j;flag=false;}
            }
            
         }
      }
         public static void  main(String[] args){
          Jisuanqi jj=new Jisuanqi();
          jj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          jj.setSize(200,200);
          jj.pack();
          jj.setVisible(true);
          jj.setTitle("&frac14;&AElig;&Euml;&atilde;&AElig;÷");
          jj.setResizable(false);
         }
     
    }刚学java那会写得有好多漏洞 凑或看吧
      

  3.   

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.*;public class Calculator extends JFrame implements ActionListener{
     public static final int WIDTH = 800;            
         public static final int HEIGHT = 600;           
         public static final int BUTTON_WIDTH = 80;      
         public static final int BUTTON_HEIGHT = 60;          private CardLayout dealer;                      
         private JPanel     deckPanel;                        private JTextField result;                           private double head = 0.0;                      
         private double tail = 0.0;                           private boolean substitution = true;                 private String operatedCommand = "NOTHING";     
         private String preOperatedCommand = "NOTHING";       public Calculator(){
             setSize(WIDTH, HEIGHT);
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             setTitle("Calculator");         Container contentPre = getContentPane();             dealer = new CardLayout();                  
             deckPanel = new JPanel();
             deckPanel.setLayout(dealer);         Box content = Box.createVerticalBox();               Box registration = Box.createVerticalBox();         JMenu fileMenu = new JMenu("File");
             JMenuItem exitItemOfFile = new JMenuItem("Exit");
             exitItemOfFile.addActionListener(this);
             fileMenu.add(exitItemOfFile);
             JMenuBar jMenuBar = new JMenuBar();
             jMenuBar.add(fileMenu);
             setJMenuBar(jMenuBar);         result = new JTextField("0.0");                     
             result.setBackground(Color.CYAN);                 
             result.setHorizontalAlignment(JTextField.RIGHT);           content.add(result);         content.add(paintButtons());         deckPanel.add("cal", content);                             deckPanel.add("reg", registration);                        contentPre.add(deckPanel);                   
         }
         public Box paintButtons(){
             Box vPanel1 = Box.createVerticalBox();         // add the seven button
             JButton seven = new JButton("7");
             seven.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             seven.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             seven.addActionListener(this);
             vPanel1.add(seven);         // add the four button
             JButton four = new JButton("4");
             four.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             four.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             four.addActionListener(this);
             vPanel1.add(four);         // add the one button
             JButton one = new JButton("1");
             one.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             one.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             one.addActionListener(this);
             vPanel1.add(one);         // add the zero button
             JButton zero = new JButton("0");
             zero.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             zero.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             zero.addActionListener(this);
             vPanel1.add(zero);         Box vPanel2 = Box.createVerticalBox();          // add the eight button
             JButton eight = new JButton("8");
             eight.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             eight.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             eight.addActionListener(this);
             vPanel2.add(eight);         // add the five button
             JButton five = new JButton("5");
             five.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             five.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             five.addActionListener(this);
             vPanel2.add(five);         // add the two button
             JButton two = new JButton("2");
             two.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             two.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             two.addActionListener(this);
             vPanel2.add(two);         // add the dot button
             JButton dot = new JButton(".");
             dot.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             dot.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             dot.addActionListener(this);
             vPanel2.add(dot);         Box vPanel3 = Box.createVerticalBox();         // add the nine button
             JButton nine = new JButton("9");
             nine.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             nine.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             nine.addActionListener(this);
             vPanel3.add(nine);         // add the six button
             JButton six = new JButton("6");
             six.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             six.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             six.addActionListener(this);
             vPanel3.add(six);         // add the three button
             JButton three = new JButton("3");
             three.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             three.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             three.addActionListener(this);
             vPanel3.add(three);         // add the plusMinus button
             JButton plusMinus = new JButton("\u00b1");
             plusMinus.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             plusMinus.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             plusMinus.addActionListener(this);
             vPanel3.add(plusMinus);
      

  4.   


             Box vPanel4 = Box.createVerticalBox();         // add the division button
             JButton division = new JButton("\u00F7");
             division.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             division.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             division.addActionListener(this);
             vPanel4.add(division);         // add the multiplication button
             JButton multiplication = new JButton("\u00d7");
             multiplication.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             multiplication.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             multiplication.addActionListener(this);
             vPanel4.add(multiplication);         // add the subtract button
             JButton subtract = new JButton("-");
             subtract.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             subtract.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             subtract.addActionListener(this);
             vPanel4.add(subtract);         // add the plus button
             JButton plus = new JButton("+");
             plus.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             plus.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             plus.addActionListener(this);
             vPanel4.add(plus);         Box vPanel5 = Box.createVerticalBox();         // add the C button
             JButton c = new JButton("C");
             c.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             c.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
             c.addActionListener(this);
             vPanel5.add(c);         // add a vertical strut
             Component verticalStrut = Box.createVerticalStrut(BUTTON_HEIGHT);
             vPanel5.add(verticalStrut);         // add the enter button
             JButton enter = new JButton("=");
             enter.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT * 2));
             enter.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT * 2));
             enter.addActionListener(this);
             vPanel5.add(enter);         Box buttonsPanel = Box.createHorizontalBox();
             
             buttonsPanel.add(vPanel1);
             buttonsPanel.add(vPanel2);
             buttonsPanel.add(vPanel3);
             buttonsPanel.add(vPanel4);
             buttonsPanel.add(vPanel5);         return buttonsPanel;
         }
         public void actionPerformed(ActionEvent e)
         {
             String actionCommand = e.getActionCommand();         // exit the system
             if(actionCommand.equals("Exit"))
                 System.exit(0);         // post the result
             else if(actionCommand.equals("="))
             {
                 // if the pre-operated command was "=", return the pre-step
                 if(!preOperatedCommand.equals("="))
                     tail = Double.parseDouble(result.getText().trim());             // if the two numbel are 0, we don't need calculate the result
                 if(tail!= 0.0 || head !=0.0)
                 {
                     // division
                     if(operatedCommand.equals("\u00F7"))
                     {
                         if(Math.abs(tail) > 0.0000000000000000001)
                         {
                             head = head/tail;
                             result.setText(Double.toString(head));
                         }
                         else
                         {
                             JOptionPane.showMessageDialog(this, "Cannot divide by zero.");
                             head = 0.0;
                             tail = 0.0;
                         }
                         substitution = true;
                     }
                                    // multiplacation
                     else if(operatedCommand.equals("\u00d7"))
                     {
                         head = head*tail;
                         result.setText(Double.toString(head));
                         substitution = true;
                     }
                                    // sub
                     else if(operatedCommand.equals("-"))
                     {
                         head = head-tail;
                         result.setText(Double.toString(head));
                         substitution = true;
                     }
                                    // plus
                     else if(operatedCommand.equals("+"))
                    {
                         head = head+tail;
                         result.setText(Double.toString(head));
                         substitution = true;
                     }
                     
                 }
                 preOperatedCommand = "=";
             }
            else if(actionCommand.equals("0"))
            {
                if(!(result.getText().equals("0") || result.getText().equals("-0")))
                {
                    if(substitution)
                    {
                        result.setText("0");
                    }
                    else
                        result.setText(result.getText() + "0");
                }
            }
            else if(actionCommand.equals("1"))
            {
                if(substitution)
                {
                    result.setText("1");
                    substitution = false;
                }
                else
                    result.setText(result.getText() + "1");
            }
            else if(actionCommand.equals("2"))
            {
                if(substitution)
                {
                    result.setText("2");
                    substitution = false;
                }
                else
                    result.setText(result.getText() + "2");
            }
      

  5.   

    else if(actionCommand.equals("3"))
            {
                if(substitution)
                {
                    result.setText("3");
                    substitution = false;
                }
                else
                    result.setText(result.getText() + "3");
            }
            else if(actionCommand.equals("4"))
            {
                if(substitution)
                {
                    result.setText("4");
                    substitution = false;
                }
                else
                    result.setText(result.getText() + "4");
            }
            else if(actionCommand.equals("5"))
            {
                if(substitution)
                {
                    result.setText("5");
                    substitution = false;
                }
                else
                    result.setText(result.getText() + "5");
            }
            else if(actionCommand.equals("6"))
            {
                if(substitution)
                {
                    result.setText("6");
                    substitution = false;
                }
                else
                    result.setText(result.getText() + "6");
            }
            else if(actionCommand.equals("7"))
            {
                if(substitution)
                {
                    result.setText("7");
                    substitution = false;
                }
                else
                    result.setText(result.getText() + "7");
            }
             else if(actionCommand.equals("8"))
             {
                if(substitution)
                {
                    result.setText("8");
                    substitution = false;
                }
                else
                    result.setText(result.getText() + "8");
            }
             else if(actionCommand.equals("9"))
             {
                if(substitution)
                {
                    result.setText("9");
                    substitution = false;
                }
                else
                    result.setText(result.getText() + "9");
            }
            else if(actionCommand.equals("."))
            {
                if(result.getText().length() == 0)
                    result.setText("0.");        }
            else if(actionCommand.equals("\u00b1"))
            {
                if(result.getText().charAt(0) != '-')
                    result.setText("-" + result.getText());
                else
                    result.setText(result.getText().substring(1));
            }
            else if(actionCommand.equals("C"))
            {
                result.setText("0");
                substitution = true;
            }
             // division
             else if(actionCommand.equals("\u00F7"))
             {
                 head = Double.parseDouble(result.getText().trim());
                 operatedCommand = "\u00F7";
                 substitution = true;
                 preOperatedCommand = "\u00F7";
             }
             // multiplication
             else if(actionCommand.equals("\u00d7"))
             {
                 head = Double.parseDouble(result.getText().trim());
                 operatedCommand = "\u00d7";
                 substitution = true;
                 preOperatedCommand = "\u00d7";
             }
                    // sub
             else if(actionCommand.equals("-"))
             {
                 head = Double.parseDouble(result.getText().trim());
                 operatedCommand = "-";
                 substitution = true;
                 preOperatedCommand = "-";
             }
                    // plus
             else if(actionCommand.equals("+"))
             {
                 head = Double.parseDouble(result.getText().trim());
                 operatedCommand = "+";
                 substitution = true;
                 preOperatedCommand = "+";
             }
                  }
         public double factorial(double value)
         {
             if(value <= 1.0)
                 return value;
             else
                 return value*factorial(value-1);
         }
         public long calChecksum(String message)
         {
             long check=0;         for (int i=0;i<message.length();i++)
                      check += message.charAt(i);
              return check;
         }
         public static void main(String[] args)
         {
             Calculator calculator = new Calculator();
             calculator.pack();
             calculator.setVisible(true);
         } }
    //三段代码合起来就可以了,一个简单的计算器