Java版简易计算器 PK Scala版简易计算器

Java版简易计算器:

package IT01;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class App1
{
    public static TextField text1 = new TextField(10);
    public static TextField text2 = new TextField(10);
    public static TextField text3 = new TextField(10);
    public static void main(String[] args)
    {
        Frame f = new Frame("Java版相加计算器");
        f.setLayout(new FlowLayout());
        Label label = new Label("+");        
        Button button = new Button("=");
        button.addActionListener(new MyListener());        
        f.add(text1);
        f.add(label);
        f.add(text2);
        f.add(button);
        f.add(text3);
        f.pack();
        f.addWindowListener(new WindowAdapter()
        {
            public void windowClosed(WindowEvent e) 
            {
                System.exit(-1);
            }
        });
        f.setVisible(true);      
    }
    public static class MyListener implements ActionListener {
        public void actionPerformed(ActionEvent e)
        {
           Integer sum = Integer.parseInt(text1.getText()) + Integer.parseInt(text2.getText());
           text3.setText(String.valueOf(sum));      
        }       
    }
}

代码行数为:48行
效果展示:

Scala版简易计算器:

package IT03

import scala.swing._  //调用一个包中的所有的类
import scala.swing.event.ButtonClicked

object App1 extends SimpleSwingApplication
{
   val text1 = new TextField { columns = 8}
   val label = new Label    { text = "+"}
   val text2 = new TextField { columns = 8}
   val button = new Button  { text = "="}
   listenTo(button)  //增加一个事件监听器
   val text3 = new TextField { columns = 8}

   val top2 = new FlowPanel
   {
      contents += text1
      contents += label
      contents += text2
      contents += button
      contents += text3
   }

   def top = new MainFrame
   {
      title = "Scala版相加计算器"
      contents = top2
   }
   reactions += 
   {
     case ButtonClicked(button) =>
     {
        val sum = Integer.parseInt(text1.text) + Integer.parseInt(text2.text)
        text3.text = String.valueOf(sum)
     }
   }   
}

代码行数:37
效果展示:
这里写图片描述
2016年6月TIOBE编程语言排行榜:
Java第1 、 Scala第30
也许将来Scala将来真的就超过Java了呢!!!!

你可能感兴趣的:(java,scala)