簡単なGUIアプリ

勉強会へ向けて何かアプリを作っておくことになったので、
簡単なGUIアプリを作りました。
swingがわからないと作れなさそうだったので、
Javaの本を久しぶりに買いました。

この本、楽しい本でした。かなり気に入りました。
作ったアプリは元利均等方式の返済金額を計算してくれるというものです。

  • 借り入れ金額、返済年数、金利を入力し、計算を押すと返済額を計算してくれます。
  • 数値以外を入力すると例外です。(なにも処理していません。)
  • scalac LoanProcessor.scala
  • scala xibbar.LoanProcessor


以下ソース。

package xibbar

import javax.swing.{JFrame,JLabel,JTextField,JButton,JPanel,BoxLayout}
import java.awt.event.{ActionListener,ActionEvent}

object LoanProcessor {
  def main(args: Array[String]){
    val window=new MainWindow()
    window.show
    window.updateResult
  }
  
}

class MainWindow extends JFrame{
  this.setSize(200,200)
  this.setTitle("ローン計算")
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  val panel=new JPanel()
  val amountField=new JTextField(10)
  val yearField=new JTextField(10)
  val rateField=new JTextField(10)
  val monthlyPayLabel=new JLabel()
  val yearlyPayLabel=new JLabel()
  val processButton=new JButton("計算")
  val finishButton=new JButton("終了")
  val boxLayout=new BoxLayout(panel,BoxLayout.Y_AXIS)
  this.getContentPane().add(panel)
  panel.setLayout(boxLayout)

  panel.add(amountField)
  panel.add(yearField)
  panel.add(rateField)
  panel.add(monthlyPayLabel)
  panel.add(yearlyPayLabel)
  panel.add(processButton)
  panel.add(finishButton)

  amountField.setText("4000")
  yearField.setText("35")
  rateField.setText("3.05")
  
  processButton.addActionListener(new ActionListener(){
      def actionPerformed(e:ActionEvent){
        updateResult
      }
    }
  )
  finishButton.addActionListener(new ActionListener(){
      def actionPerformed(e:ActionEvent){
        System.exit(0)
      }
    }
  )

  def updateResult{
    val amount=amountField.getText().toInt
    val year=yearField.getText().toInt
    val rate=rateField.getText().toFloat
    val result=processing(amount,year,rate)
    monthlyPayLabel.setText("毎月支払額:"+(result).toInt)
    yearlyPayLabel.setText("年間支払額:"+(result).toInt*12)
  }
  def processing(amount: Int, year: Int, rate: Float): Double={
    amount*10000*(rate/12/100)/(1-Math.pow(1+rate/12/100,-year*12))
  }
}

全然scalaっぽいアプリではありません。
かしこ。