blob: 7aec96d77d59d909d3821017c91f212832040c05 [file] [log] [blame]
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# A silly little calculator implemented in Jacl using
# Java components for the UI.
package require java
set f [java::new java.awt.Frame "BSH Calculator (jacl/tcl)"]
bsf addEventListener $f "window" "windowClosing" "exit"
set p [java::new java.awt.Panel]
set f1 [java::new java.awt.TextField]
$f1 setColumns 20
bsf addEventListener $f1 "action" "" "doMath"
set f2 [java::new java.awt.TextField]
bsf addEventListener $f2 "text" "" "doMath"
set p [java::new java.awt.Panel]
$p setLayout [java::new java.awt.GridLayout 2 2]
$p add [java::new java.awt.Label "Enter Operand"]
$p add $f1
$p add [java::new java.awt.Label "Enter Operand"]
$p add $f2
$f add "North" $p
$f add "Center" [java::new java.awt.Label "Results:"]
set p [java::new java.awt.Panel]
$p setLayout [java::new java.awt.GridLayout 4 2]
$p add [java::new java.awt.Label "Sum"]
$p add [set sum [java::new java.awt.TextField]]
$sum setColumns 20
$p add [java::new java.awt.Label "Difference"]
$p add [set diff [java::new java.awt.TextField]]
$p add [java::new java.awt.Label "Product"]
$p add [set prod [java::new java.awt.TextField]]
$p add [java::new java.awt.Label "Quotient"]
$p add [set quo [java::new java.awt.TextField]]
$f add "South" $p
$f pack
$f show
$f toFront
proc getField {f} {
set t [$f getText]
if {$t == ""} {
return 0
} else {
return [java::call java.lang.Integer parseInt $t]
}
}
proc doMath {} {
global f1 f2 sum diff prod quo
set n1 [getField $f1]
set n2 [getField $f2]
$sum setText [expr $n1 + $n2]
$diff setText [expr $n1 - $n2]
$prod setText [expr $n1 * $n2]
$quo setText [expr $n1 / $n2]
}