Univ Study/JAVA 프로그래밍: 7개의 글

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();
            }
        });
    }
}

 

저작자 표시 비영리 변경 금지
신고

JAVA 프로그래밍 - NumberAddition V3 Event Handling

Posted by Patchman
2010.02.15 18:54 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 NumberAdditionV3 implements ActionListener {

 //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;

 /* constructor */
 public NumberAdditionV3() {
  //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(this);
        jButtonClear = new JButton("Clear");
   jButtonClear.setSize(80,30);
   jButtonClear.addActionListener(this);
        jButtonExit = new JButton("Exit");
   jButtonExit.setSize(80,30);
   jButtonExit.addActionListener(this);
  

  //-----------------------
  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 void actionPerformed(ActionEvent e){

  jButtonPressed = (JButton) e.getSource();

  if("Add".equals(jButtonPressed.getText())){

   jTextFieldResult.setText(""+(Double.parseDouble(jTextFieldFirstNumber.getText()) + Double.parseDouble(jTextFieldSecondNumber.getText())));
  
  } else if("Clear".equals(jButtonPressed.getText())){

   jTextFieldFirstNumber.setText("");
   jTextFieldSecondNumber.setText("");
   jTextFieldResult.setText("");

  } else{

   System.exit(0);
  }
 }

    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 NumberAdditionV3();
            }
        });
    }
}

 

저작자 표시 비영리 변경 금지
신고

JAVA 프로그래밍 - NumberAddition V2 Layout Manager

Posted by Patchman
2010.02.15 18:52 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.SpringLayout;
import java.awt.Container;

public class NumberAdditionV2 {

 private JFrame frame;
 private JPanel jPanel;
 private JTextField jTextFieldFirstNumber, jTextFieldSecondNumber, jTextFieldThirdNumber;
 private JLabel jLabel1, jLabel2, jLabel3;
 private JButton jButtonAdd, jButtonClear, jButtonExit;
 private Container contentPane;

 public NumberAdditionV2(){

  initComponents(); 
 }

 private void initComponents(){

  frame = new JFrame("NumberAdditionV2");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  addWidgets();

  frame.pack();
     frame.setVisible(true);
 }

 private void addWidgets(){

  //Set up the content pane.
        contentPane = frame.getContentPane();
      
  jLabel1 = new JLabel("First Number : ");
  jLabel2 = new JLabel("Second Number : ");
  jLabel3 = new JLabel("Result : ");

        jTextFieldFirstNumber = new JTextField(15);
  jTextFieldSecondNumber = new JTextField(15);
     jTextFieldThirdNumber = new JTextField(15);

  jButtonAdd = new JButton("Add");
  jButtonClear = new JButton("Clear");
  jButtonExit = new JButton("Exit");

        contentPane.add(jLabel1);
  contentPane.add(jLabel2);
  contentPane.add(jLabel3);

        contentPane.add(jTextFieldFirstNumber);
        contentPane.add(jTextFieldSecondNumber);
  contentPane.add(jTextFieldThirdNumber);

  contentPane.add(jButtonAdd);
        contentPane.add(jButtonClear);
  contentPane.add(jButtonExit);
  
  layoutComponents();
 }


 private void layoutComponents(){

  SpringLayout layout = new SpringLayout();
        contentPane.setLayout(layout);

  // Label layout
        layout.putConstraint(SpringLayout.WEST, jLabel1, 38, SpringLayout.WEST, contentPane);
        layout.putConstraint(SpringLayout.NORTH, jLabel1, 20, SpringLayout.NORTH, contentPane);

  layout.putConstraint(SpringLayout.WEST, jLabel2, 20, SpringLayout.WEST, contentPane);
        layout.putConstraint(SpringLayout.NORTH, jLabel2, 35, SpringLayout.NORTH, jLabel1);

  layout.putConstraint(SpringLayout.WEST, jLabel3, 75, SpringLayout.WEST, contentPane);
        layout.putConstraint(SpringLayout.NORTH, jLabel3, 35, SpringLayout.NORTH, jLabel2);

  // TextField layout
        layout.putConstraint(SpringLayout.WEST, jTextFieldFirstNumber, 5, SpringLayout.EAST, jLabel1);
        layout.putConstraint(SpringLayout.NORTH, jTextFieldFirstNumber, 20, SpringLayout.NORTH, contentPane);

        layout.putConstraint(SpringLayout.WEST, jTextFieldSecondNumber, 5, SpringLayout.EAST, jLabel2);
        layout.putConstraint(SpringLayout.NORTH, jTextFieldSecondNumber, 35, SpringLayout.NORTH, jLabel1);

  layout.putConstraint(SpringLayout.WEST, jTextFieldThirdNumber, 5, SpringLayout.EAST, jLabel3);
        layout.putConstraint(SpringLayout.NORTH, jTextFieldThirdNumber, 35, SpringLayout.NORTH, jLabel2);

        // Button layout
  layout.putConstraint(SpringLayout.WEST, jButtonAdd, 85, SpringLayout.WEST, contentPane);
  layout.putConstraint(SpringLayout.NORTH, jButtonAdd, 50, SpringLayout.NORTH, jLabel3);
  
        layout.putConstraint(SpringLayout.WEST, jButtonClear, 10, SpringLayout.EAST, jButtonAdd);
        layout.putConstraint(SpringLayout.NORTH, jButtonClear, 50, SpringLayout.NORTH, jLabel3);

  layout.putConstraint(SpringLayout.WEST, jButtonExit, 200, SpringLayout.WEST, contentPane);
        layout.putConstraint(SpringLayout.NORTH, jButtonExit, 40, SpringLayout.NORTH, jButtonClear);

  // east, south margin
  layout.putConstraint(SpringLayout.EAST, contentPane, 20, SpringLayout.EAST, jTextFieldThirdNumber);
        layout.putConstraint(SpringLayout.SOUTH, contentPane, 20, SpringLayout.SOUTH, jButtonExit);

        // Frame layout
  CenterScreen cs = new CenterScreen();
  frame.setLocation(cs.getX(frame), cs.getY(frame));  
 }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
    new NumberAdditionV2();
            }
        });
    }
}

저작자 표시 비영리 변경 금지
신고