JAVA 프로그래밍 - NumberAddition V1 Swing GUI

Posted by Patchman
2010.02.15 18:49 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.SwingConstants;
import javax.swing.BorderFactory;
import java.awt.BorderLayout;

public class NumberAddtionV1 {

  /* Constructor */
 public NumberAddtionV1(){
    
  // (1) Create and set up the window.
  JFrame frame = new JFrame("NumberAddtionV1");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
     // (2) Create a main panel.
   JPanel mainPanel = new JPanel();
 
     // (3) Add widgets onto the main panel.
  addWidgets(mainPanel);
   
     // (4) Add the main panel to Content Pane
  frame.getContentPane().add(mainPanel);
 
     // (5) Display the window.
  CenterScreen cs = new CenterScreen();
  frame.setLocation(cs.getX(frame), cs.getY(frame));
  frame.pack();
  frame.setVisible(true);
 }

 private void addWidgets(JPanel mainPanel){

   // TextField
   JTextField TextFirst = new JTextField(10);
   JTextField TextSecond = new JTextField(10);
   JTextField TextThird = new JTextField(10);

    // Label
   JLabel LabelFirst = new JLabel("First Number : ");
   JLabel LabelSecond = new JLabel("Second Number : ");
   JLabel LabelThird = new JLabel("Result : ");

    // Button
   JButton ButtonAdd = new JButton("Add");
   JButton ButtonClear = new JButton("Clear");
   JButton ButtonExit = new JButton("Exit");

  
    mainPanel.add(LabelFirst);
    mainPanel.add(TextFirst);

    mainPanel.add(LabelSecond);
    mainPanel.add(TextSecond);

    mainPanel.add(LabelThird);
    mainPanel.add(TextThird);

    mainPanel.add(ButtonAdd);
    mainPanel.add(ButtonClear);
    mainPanel.add(ButtonExit);
 }

 private static void createAndShowGUI(){
  NumberAddtionV1 h = new NumberAddtionV1();
 }
 
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}