JAVA 프로그래밍 - NumberAddition V4 Inner Class

Posted by Patchman
2010.02.15 18:56 Univ Study/JAVA 프로그래밍



// 컴퓨터공학과 3학년 안치영

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.BorderFactory;
import javax.swing.SpringLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class NumberAdditionV4{

 //define reference variables for GUI components
 private JFrame frame;
 private JPanel jPanel;
    private JTextField jTextFieldFirstNumber, jTextFieldSecondNumber, jTextFieldResult;
 private JLabel jLabel1, jLabel2, jLabel3;
 private JButton jButtonAdd, jButtonClear, jButtonExit, jButtonPressed;
 private SpringLayout s;

 // jButtonAdd 의 Inner class method
 public void addButtonPressed(ActionEvent evt){
  jTextFieldResult.setText(""+(Double.parseDouble(jTextFieldFirstNumber.getText()) + Double.parseDouble(jTextFieldSecondNumber.getText())));
 }

 // jButtonClear 의 Inner class method
 public void clearButtonPressed(ActionEvent evt){
  jTextFieldFirstNumber.setText("");
  jTextFieldSecondNumber.setText("");
  jTextFieldResult.setText("");
 }

 // jButtonExit 의 Inner class method
 public void exitButtonPressed(ActionEvent evt){
  System.exit(0);
 }

 /* constructor */
 public NumberAdditionV4() {
  //Place codes for an initializing object other than GUI
  //...

  //Create and Show up GUI
  initComponents();
 }

 private void initComponents() {
        //(1) Create and set up the window.
        frame = new JFrame("Number Addition Version 3: Layout Manager");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setSize(400, 300);

  //(2) Create Component and attach onto jPanel
  jPanel = new JPanel();
   //jPanel.setBorder(BorderFactory.createEmptyBorder(10,20,10,20)); //top, left, bottom, right
   //jPanel.setBorder(BorderFactory.createTitledBorder("Number Addition"));
   jPanel.setBorder(BorderFactory.createMatteBorder(10,20,10,20, java.awt.Color.GREEN));

  //-----------------------
  addWidgets();  //create each GUI components and attatch onto the panel
  //-----------------------

        //(3) Add jPanel to Content Pane
        frame.getContentPane().add(jPanel);

        //(4) Display the window.
  //center the frame ------------------------------------------------------------
        frame.setLocation(((java.awt.Toolkit.getDefaultToolkit().getScreenSize().width
                            - frame.getSize().width) / 2),
                         ((java.awt.Toolkit.getDefaultToolkit().getScreenSize().height
                            - frame.getSize().height) / 2));
  //-----------------------------------------------------------------------------

  frame.setVisible(true);

 }
 private void addWidgets() {
  //create widgets
        jLabel1 = new JLabel("First Number :");
        jLabel2 = new JLabel("Second Number :");
        jLabel3 = new JLabel("Result :");

        jTextFieldFirstNumber = new JTextField(20);
        jTextFieldSecondNumber = new JTextField(20);
        jTextFieldResult = new JTextField(20);

        jButtonAdd = new JButton("Add");
   jButtonAdd.setSize(80,30);
   jButtonAdd.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent evt){
              addButtonPressed(evt);
             }
            });

        jButtonClear = new JButton("Clear");
   jButtonClear.setSize(80,30);
   jButtonClear.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent evt){
              clearButtonPressed(evt);
             }
            });

        jButtonExit = new JButton("Exit");
   jButtonExit.setSize(80,30);
   jButtonExit.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent evt){
              exitButtonPressed(evt);
             }
            });;
  
  //-----------------------
  layoutComponents(); //define layout method
  //-----------------------

  //add widgets onto the panel
  jPanel.add(jLabel1);
  jPanel.add(jTextFieldFirstNumber);
  jPanel.add(jLabel2);
  jPanel.add(jTextFieldSecondNumber);
  jPanel.add(jLabel3);
  jPanel.add(jTextFieldResult);
  jPanel.add(jButtonAdd);
  jPanel.add(jButtonClear);
  jPanel.add(jButtonExit);
 }
 private void layoutComponents() {
  //define layout method
  s = new SpringLayout();
  jPanel.setLayout(s);

  //add components to the Panel

  //define constants for gaps between components
  final int HORIZONTAL_GAP = 10;
  final int VERTICAL_GAP = 20;

  //jLabel1
  s.putConstraint(SpringLayout.WEST, jLabel1, 30,
               SpringLayout.WEST, jPanel);
  s.putConstraint(SpringLayout.NORTH, jLabel1, 30,
             SpringLayout.NORTH, jPanel);
  //jTextFieldFirstNumber
  s.putConstraint(SpringLayout.WEST, jTextFieldFirstNumber, HORIZONTAL_GAP,
               SpringLayout.EAST, jLabel1);
  s.putConstraint(SpringLayout.NORTH, jTextFieldFirstNumber, 0,
             SpringLayout.NORTH, jLabel1);

  //jLabel2
  s.putConstraint(SpringLayout.EAST, jLabel2, 0,
               SpringLayout.EAST, jLabel1);
  s.putConstraint(SpringLayout.NORTH, jLabel2, VERTICAL_GAP,
             SpringLayout.SOUTH, jLabel1);
  //jTextFieldSecondNumber
  s.putConstraint(SpringLayout.WEST, jTextFieldSecondNumber, HORIZONTAL_GAP,
               SpringLayout.EAST, jLabel2);
  s.putConstraint(SpringLayout.NORTH, jTextFieldSecondNumber, 0,
             SpringLayout.NORTH, jLabel2);

  //jLabel3
  s.putConstraint(SpringLayout.EAST, jLabel3, 0,
               SpringLayout.EAST, jLabel2);
  s.putConstraint(SpringLayout.NORTH, jLabel3, VERTICAL_GAP,
             SpringLayout.SOUTH, jLabel2);
  //jTextFieldResult
  s.putConstraint(SpringLayout.WEST, jTextFieldResult,  HORIZONTAL_GAP,
               SpringLayout.EAST, jLabel3);
  s.putConstraint(SpringLayout.NORTH, jTextFieldResult, 0,
             SpringLayout.NORTH, jLabel3);

  //jButtonAdd
  s.putConstraint(SpringLayout.WEST, jButtonAdd, 0,
               SpringLayout.WEST, jTextFieldFirstNumber);
  s.putConstraint(SpringLayout.NORTH, jButtonAdd, VERTICAL_GAP + 5,
             SpringLayout.SOUTH, jTextFieldResult);
  //jButtonClear
  s.putConstraint(SpringLayout.WEST, jButtonClear, HORIZONTAL_GAP + 5,
               SpringLayout.EAST, jButtonAdd);
  s.putConstraint(SpringLayout.NORTH, jButtonClear, 0,
             SpringLayout.NORTH, jButtonAdd);
  //jButtonExit
  s.putConstraint(SpringLayout.WEST, jButtonExit, HORIZONTAL_GAP + 5,
               SpringLayout.WEST, jButtonClear);
  s.putConstraint(SpringLayout.NORTH, jButtonExit, VERTICAL_GAP,
             SpringLayout.SOUTH, jButtonClear);

 }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new NumberAdditionV4();
            }
        });
    }
}