|
1 |
| -import{ |
2 |
| - javax.swing.*; |
3 |
| - java.awt.*; |
4 |
| - java.awt.event.*; |
| 1 | +import { |
| 2 | + javax.swing.* |
| 3 | + java.awt.* |
| 4 | + java.awt.event.* |
5 | 5 | }
|
6 | 6 |
|
7 | 7 | class Calculator : JFrame <: ActionListener {
|
8 |
| - @text :JTextField; |
9 |
| - @left :Long; |
10 |
| - @right :Long; |
11 |
| - @operator :String; |
12 |
| - @isError :Boolean; |
| 8 | + @text :JTextField |
| 9 | + @left :Long |
| 10 | + @right :Long |
| 11 | + @operator :String |
| 12 | + @isError :Boolean |
13 | 13 | public:
|
14 | 14 | def setValue(value : Long) { @text.setText(JLong::toString(value)); }
|
15 | 15 | def setValue(value : String) { @text.setText(value); }
|
16 | 16 | def actionPerformed(event : ActionEvent) {
|
17 |
| - label = event.source$JButton.label; |
| 17 | + label = event.source$JButton.label |
18 | 18 | try{
|
19 | 19 | if @isError && !(label == "C") {
|
20 |
| - return; |
| 20 | + return |
21 | 21 | }
|
22 |
| - input = JLong::parseLong(label); |
| 22 | + input = JLong::parseLong(label) |
23 | 23 | if @operator != null {
|
24 |
| - @right = @right * 10 + input; |
25 |
| - setValue(@right); |
| 24 | + @right = @right * 10 + input |
| 25 | + setValue(@right) |
26 | 26 | }else{
|
27 |
| - @left = @left * 10 + input; |
28 |
| - setValue(@left); |
| 27 | + @left = @left * 10 + input |
| 28 | + setValue(@left) |
29 | 29 | }
|
30 | 30 | }catch e :NumberFormatException{
|
31 | 31 | if label == "C" {
|
32 |
| - @left = 0; |
33 |
| - @right = 0; |
34 |
| - @operator = null; |
35 |
| - @text.setText(JLong::toString(0)); |
36 |
| - @isError = false; |
| 32 | + @left = 0 |
| 33 | + @right = 0 |
| 34 | + @operator = null |
| 35 | + @text.setText(JLong::toString(0)) |
| 36 | + @isError = false |
37 | 37 | }else{
|
38 | 38 | if @operator != null {
|
39 | 39 | if @operator == "/" {
|
40 | 40 | if @right == 0L{
|
41 |
| - setValue("0で割ることはできません。"); |
42 |
| - @left = 0L; |
43 |
| - @right = 0L; |
44 |
| - @isError = true; |
| 41 | + setValue("0で割ることはできません。") |
| 42 | + @left = 0L |
| 43 | + @right = 0L |
| 44 | + @isError = true |
45 | 45 | }else{
|
46 |
| - @left = @left / @right; |
| 46 | + @left = @left / @right |
47 | 47 | }
|
48 |
| - setValue(@left); |
| 48 | + setValue(@left) |
49 | 49 | }else{
|
50 | 50 | select @operator {
|
51 |
| - case "+": @left = @left + @right; |
52 |
| - case "-": @left = @left - @right; |
53 |
| - case "*": @left = @left * @right; |
54 |
| - case "/": @left = @left / @right; |
| 51 | + case "+": @left = @left + @right |
| 52 | + case "-": @left = @left - @right |
| 53 | + case "*": @left = @left * @right |
| 54 | + case "/": @left = @left / @right |
55 | 55 | }
|
56 |
| - setValue(@left); |
| 56 | + setValue(@left) |
57 | 57 | }
|
58 | 58 | }
|
59 |
| - @operator = label; |
60 |
| - @right = 0; |
| 59 | + @operator = label |
| 60 | + @right = 0 |
61 | 61 | }
|
62 | 62 | }
|
63 | 63 | }
|
64 | 64 |
|
65 |
| - def new:("簡易電卓") { |
66 |
| - setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE); |
67 |
| - setSize(800, 600); |
68 |
| - @text = new JTextField; |
69 |
| - @text.setHorizontalAlignment(JTextField::RIGHT); |
70 |
| - pane = getContentPane(); |
71 |
| - north = new JPanel; |
72 |
| - north.setLayout(new BorderLayout); |
73 |
| - north.add(@text, BorderLayout::NORTH); |
74 |
| - center = new JPanel; |
75 |
| - center.setLayout(new GridLayout(4, 5, 4, 3)); |
76 |
| - center.setFont(new Font(null, Font::PLAIN, 8)); |
77 |
| - labels = [ |
| 65 | + def this:("簡易電卓") { |
| 66 | + setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE) |
| 67 | + setSize(800, 600) |
| 68 | + @text = new JTextField |
| 69 | + @text.setHorizontalAlignment(JTextField::RIGHT) |
| 70 | + pane = getContentPane() |
| 71 | + north = new JPanel |
| 72 | + north.setLayout(new BorderLayout) |
| 73 | + north.add(@text, BorderLayout::NORTH) |
| 74 | + center= new JPanel |
| 75 | + center.setLayout(new GridLayout(4, 5, 4, 3)) |
| 76 | + center.setFont(new Font(null, Font::PLAIN, 8)) |
| 77 | + labels = [ |
78 | 78 | "7", "8", "9", "C",
|
79 | 79 | "4", "5", "6", "*",
|
80 | 80 | "1", "2", "3", "-",
|
81 | 81 | "0", "=", "/", "+"
|
82 |
| - ]; |
| 82 | + ] |
83 | 83 | foreach label:String in labels {
|
84 |
| - button = new JButton(label$String); |
85 |
| - button.addActionListener(self); |
86 |
| - button.setPreferredSize(new Dimension(42, 28)); |
87 |
| - center.add(button); |
| 84 | + button = new JButton(label$String) |
| 85 | + button.addActionListener(self) |
| 86 | + button.setPreferredSize(new Dimension(42, 28)) |
| 87 | + center.add(button) |
88 | 88 | }
|
89 |
| - pane.add(north, BorderLayout::NORTH); |
90 |
| - pane.add(center, BorderLayout::CENTER); |
| 89 | + pane.add(north, BorderLayout::NORTH) |
| 90 | + pane.add(center, BorderLayout::CENTER) |
91 | 91 | }
|
92 | 92 |
|
93 | 93 | static def main(args :String[]){
|
94 |
| - UIManager::setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); |
95 |
| - frame = new Calculator; |
96 |
| - frame.pack; |
97 |
| - frame.setVisible(true); |
| 94 | + frame = new Calculator |
| 95 | + frame.pack |
| 96 | + frame.setVisible(true) |
98 | 97 | }
|
99 | 98 | }
|
0 commit comments