Java Study 3 Swing

1. Swing

GUI를 프로그래밍 할 수 있도록 작업 가능한 패키지

2. Swing 프로임 만들기

import javax.swing.JFrame;
public class swing1 extends JFrame{
	public swing1() {
		setTitle("스윙 프레임 만들기"); //창 상단 타이틀 설정
		setSize(500,500); //창 사이즈
                //프레임이 닫힐 때 프로세스 같이 종료 없으면 프로세스 살이있음
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true); //설정한 frame 보이기
	}
	public static void main(String[] args) {
		swing1 frame = new swing1(); //swing1 생성자 호출
	}
}

3. Swing Pane

public class swing1 extends JFrame{
	public swing1() {
		setTitle("스윙 프레임 만들기");
		setSize(500,500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container container = getContentPane();
		container.setBackground(Color.ORANGE);
		container.setLayout(new FlowLayout());
		//FlowLayout : 컴포넌트가 삽입되는 순서대로 인쪽부터 배치
		//배치할 공간이 없으면 내려와서 반복
		container.add(new JButton("OK")); //패널에 버튼 붙이기
		container.add(new JButton("Cancel"));
		container.add(new JButton("Ignore"));
		setVisible(true);
	}
	public static void main(String[] args) {
		swing1 frame = new swing1();
	}
}

배치 관리자 대표유형

  • FlowLayout : 컴포넌트가 삽입되는 순서대로 왼쪽부터 배치, 배치할 공간이 없으면 아래로 내려와 반복한다.
  • BordeLayout : 컨테이너의 공간을 동,서, 남, 북, 중앙 5개 영역으로 나눈다
  • GridLayout : 컨테이너를 프로그램에서 설정한 동일한 크기의 2차원 격자로 나눈다, 배치 형식은 FlowLayout과 동일
  • CardLayout : 컨테이너의 공간에 카드를 쌓아 놓듯이 컴포넌트를 포개어 배치

4. Swing Button ActionListener

public class swing1 extends JFrame{
	public swing1() {
		setTitle("스윙 프레임 만들기");
		setSize(500,500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null);
		JTextField txtNum1 = new JTextField(20);
		txtNum1.setBounds(10,10,200,25);
		this.add(txtNum1);
		JTextField txtNum2 = new JTextField(20);
		txtNum2.setBounds(10,50,200,25);
		this.add(txtNum2);
		JLabel lblResult = new JLabel("결과출력");
		lblResult.setBounds(10,90,200,25);
		this.add(lblResult);
		JButton btnAdd = new JButton("Plus");
		btnAdd.setBounds(10,130,200,25);
		this.add(btnAdd);
		btnAdd.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				double n1 = Double.parseDouble(txtNum1.getText().toString());
				double n2 = Double.parseDouble(txtNum2.getText().toString());
				double sum = n1+n2;
				lblResult.setText(sum+"");
			}
		});
		setVisible(true);
	}
	public static void main(String[] args) {
		swing1 frame = new swing1();
	}
}

5. Swing 계산기

public class swing1 extends JFrame implements ActionListener{
	JTextField txtNum1;
	JTextField txtNum2;
	JLabel lblResult;
	String[] op = new String[] {"+","-","*","/","%"};
	public swing1() {
		setTitle("계산");
		setSize(500,500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null);
		 txtNum1 = new JTextField(20);
		txtNum1.setBounds(10,10,200,25);
		this.add(txtNum1);
		txtNum2 = new JTextField(20);
		txtNum2.setBounds(10,50,200,25);
		this.add(txtNum2);
		lblResult = new JLabel("결과출력");
		lblResult.setBounds(10,90,200,25);
		this.add(lblResult);
		JButton btns[] = new JButton[op.length];
		for(int i =0;i<op.length;i++) {
			btns[i] = new JButton();
			btns[i].setBounds(10,i*30+150,200,25);
			btns[i].setText(op[i]);
			this.add(btns[i]);
			btns[i].addActionListener(this);
		}
		setVisible(true);
	}
	public static void main(String[] args) {
		swing1 frame = new swing1();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		double a = Double.parseDouble(txtNum1.getText().toString());
		double b = Double.parseDouble(txtNum2.getText().toString());
		double result = 0;
		switch (e.getActionCommand()) {
			case "+":
				result=a+b;
				break;
			case "-":
				result=a-b;
				break;
			case "*":
				result=a*b;
				break;
			case "/":
				result=a/b;
				break;
			case "%":
				result=a%b;
				break;
		}
		lblResult.setText(result+"");
	}
}

Leave a Comment