Layout Manager: 1개의 글

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