2011年1月17日月曜日

Eclipseで電卓のような何かをつくる

[したいこと]

Eclipseで電卓のような何かをつくる

[環境]


Ubuntu Maverick Meerkat 10.10 in VirtualBox 4
Eclipse 3.5

録画したのでYouTubeにアップしてみた


Eclipse_simpleCalc_1of3


Eclipse_simpleCalc_2of3


Eclipse_simpleCalc_3of3


CalculatorFrame.java
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class CalculatorFrame extends JFrame{
 
 public JLabel firstNumberLabel;
 public JTextField firstNumberText;
 
 public JLabel secondNumberLabel;
 public JTextField secondNumberText;
 
 public JLabel answerLabel;
 public JTextField answerText;
 
 public JButton plusButton;
 public JButton minusButton;
 
 public CalculatorFrame(){
  
  this.setTitle("Calculator");
  this.setSize(new Dimension(250,220));
  this.setLayout(new FlowLayout());
  this.setResizable(false);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  this.setLocation(MouseInfo.getPointerInfo().getLocation());
  
  this.addComponentListener(new ComponentListener(){

   @Override
   public void componentHidden(ComponentEvent arg0) {
    // TODO Auto-generated method stub
    
   }

   @Override
   public void componentMoved(ComponentEvent arg0) {
    // TODO Auto-generated method stub
    
   }

   @Override
   public void componentResized(ComponentEvent arg0) {
    // TODO Auto-generated method stub
    
   }

   @Override
   public void componentShown(ComponentEvent arg0) {
    // TODO Auto-generated method stub
    CalculatorFrame_Load();
   }
   
  });
  
  this.firstNumberLabel = new JLabel();
  this.firstNumberLabel.setText("First Number");
  
  this.firstNumberText = new JTextField();
  this.firstNumberText.setText("                                                  ");
  
  
  this.secondNumberLabel = new JLabel();
  this.secondNumberLabel.setText("Second Number");
  
  this.secondNumberText = new JTextField();
  this.secondNumberText.setText("                                                  ");
  
  this.answerLabel = new JLabel();
  this.answerLabel.setText("Answer is");
  
  this.answerText = new JTextField();
  this.answerText.setText("                                                  ");
  this.answerText.setEditable(false);
  
  this.plusButton = new JButton();
  this.plusButton.setText("      +      ");
  this.plusButton.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent event){
    plusButton_ActionPerformed();
   }
  });
  
  this.minusButton = new JButton();
  this.minusButton.setText("      -      ");
  this.minusButton.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent event){
    minusButton_ActionPerformed();
   }
  });
  
  add(firstNumberLabel);
  add(firstNumberText);
  add(secondNumberLabel);
  add(secondNumberText);
  add(plusButton);
  add(minusButton);
  add(answerLabel);
  add(answerText);
 }
 
 private void plusButton_ActionPerformed(){
  int num1 = 0, num2 = 0, answer = 0;
  
  try{
   num1 = Integer.parseInt(firstNumberText.getText());
  }catch(Exception e){
   JOptionPane.showMessageDialog(null, "Invalid First Number");
  }
  
  try{
   num2 = Integer.parseInt(secondNumberText.getText());
  }catch(Exception e){
   JOptionPane.showMessageDialog(null, "Invalid Second Number");
  }
  
  answer = num1 + num2;
  answerText.setText(String.valueOf(answer));
  
 }
 
 private void minusButton_ActionPerformed(){
int num1 = 0, num2 = 0, answer = 0;
  
  try{
   num1 = Integer.parseInt(firstNumberText.getText());
  }catch(Exception e){
   JOptionPane.showMessageDialog(null, "Invalid First Number");
  }
  
  try{
   num2 = Integer.parseInt(secondNumberText.getText());
  }catch(Exception e){
   JOptionPane.showMessageDialog(null, "Invalid Second Number");
  }
  
  answer = num1 - num2;
  answerText.setText(String.valueOf(answer));
 }
 
 private void CalculatorFrame_Load(){
  firstNumberText.setText("");
  secondNumberText.setText("");
  answerText.setText("");
 }
}




main.java
public class main {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  CalculatorFrame frame = new CalculatorFrame();
  frame.setVisible(true);
 }

}

0 コメント: