ddit/Java

Swing

ssong2ku 2022. 9. 5. 19:12
728x90

스윙이란?

자바에서 GUI(Graphic User Interface)를 구현하기 위해 JDK에서 기본적으로 제공하는 개발 툴킷으로 선 마이크로시스템즈의 자바 기반 클래스의 일부이다.

스윙은 기존에 발표되었던 AWT(Abstract Window Toolkit)가 OS(Operating System) 및 윈도 시스템의 자원을 그대로 제공하기 때문에 자바에서 지향하는 "Write Once, Run Everywhere(WORE)"를 구현하기 위해 각종 시스템에서 공통적으로 제공하는 버튼, 대화창 등만을 구현하고 표나 트리 등의 좀 더 복잡하고 다양한 그래픽 컴포넌트를 사용할 수 없는 단점을 보완하기 위하여 JDK 1.2 버전부터 사용되었다.

 

Frame, JFrame으로 창띄우기

 - > jframe은 java swing 클래스의 일부이며 구현되는 하나의 창

 - >Frame(AWT)은 운영체제에 의존적이므로 OS환경에 따라 다르게 구현
     JFrame은 Frame을 운영체제에 도움을 받지 않고 구현.

-Frame

package day04;

import java.awt.Frame;

public class AwtTest {
	public static void main(String[] args) {
		Frame frm  = new Frame();
		frm.setVisible(true);
		frm.setSize(400,400);
	}
}

-JFrame

package day04;

import java.awt.Frame;

public class AwtTest {
	public static void main(String[] args) {
		Frame frm  = new Frame();
		frm.setVisible(true);
		frm.setSize(400,400);
	}
}

-MySwing01

package day04;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MySwing01 extends JFrame {
		
		

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MySwing01 frame = new MySwing01();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MySwing01(){
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lbl = new JLabel("GoodMorning");
		lbl.setBounds(62, 28, 86, 15);
		getContentPane().add(lbl);
		
		JButton btn = new JButton("Click");
		btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				//System.out.println("myclick");
				lbl.setText("Good Evening");
			}
		});

		btn.setBounds(272, 24, 101, 23);
		contentPane.add(btn);
	}
}

-MySwing02

package day04;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MySwing02 extends JFrame {

	private JPanel contentPane;
	private JTextField tf;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MySwing02 frame = new MySwing02();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MySwing02() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		tf = new JTextField();
		tf.setText("100");
		tf.setBounds(35, 56, 116, 21);
		contentPane.add(tf);
		tf.setColumns(10);
		
		JButton btn = new JButton("increase");
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
			String a =	tf.getText();
			int aa = Integer.parseInt(a);
			aa++;
			tf.setText(Integer.toString(aa));
			
			}
		});
		btn.setBounds(241, 55, 97, 23);
		contentPane.add(btn);
	}
}

-MySwing03

package day04;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MySwing03 extends JFrame {

	private JPanel contentPane;
	private JTextField tfA;
	private JTextField tfC;
	private JTextField tfB;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MySwing03 frame = new MySwing03();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MySwing03() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		tfA = new JTextField();
		tfA.setBounds(23, 47, 84, 21);
		contentPane.add(tfA);
		tfA.setColumns(10);
		
		tfC = new JTextField();
		tfC.setColumns(10);
		tfC.setBounds(338, 47, 84, 21);
		contentPane.add(tfC);
		
		tfB = new JTextField();
		tfB.setColumns(10);
		tfB.setBounds(154, 47, 84, 21);
		contentPane.add(tfB);
		
		JLabel lbl = new JLabel("*");
		lbl.setBounds(126, 50, 29, 15);
		contentPane.add(lbl);
		
		JButton btn = new JButton("=");
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				myclick();
			}
		});
		btn.setBounds(268, 47, 43, 21);
		contentPane.add(btn);
	}
	public void myclick() {
		String a = tfA.getText();
		String b = tfB.getText();
		
		int aa = Integer.parseInt(a);
		int bb = Integer.parseInt(b);
		
		int sum = aa+bb;
		
		tfC.setText(sum+"");
	}
}

-MySwing04

package day04;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MySwing04 extends JFrame {

	private JPanel contentPane;
	JLabel lbl1;
	JLabel lbl2;
	JLabel lbl3;
	JLabel lbl4;
	JLabel lbl5;
	JLabel lbl6;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MySwing04 frame = new MySwing04();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MySwing04() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		lbl1 = new JLabel("__");
		lbl1.setBounds(27, 32, 24, 15);
		contentPane.add(lbl1);
		
		lbl2 = new JLabel("__");
		lbl2.setBounds(63, 32, 24, 15);
		contentPane.add(lbl2);
		
		lbl3 = new JLabel("__");
		lbl3.setBounds(99, 32, 24, 15);
		contentPane.add(lbl3);
		
		lbl4 = new JLabel("__");
		lbl4.setBounds(135, 32, 24, 15);
		contentPane.add(lbl4);
		
		lbl5 = new JLabel("__");
		lbl5.setBounds(182, 32, 24, 15);
		contentPane.add(lbl5);
		
		lbl6 = new JLabel("__");
		lbl6.setBounds(218, 32, 24, 15);
		contentPane.add(lbl6);
		
		JButton btn = new JButton("로또생성하기");
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				myclick();
			}
		});
		btn.setBounds(27, 61, 215, 23);
		contentPane.add(btn);
	}
	
	public void myclick() {
		int[] arr45 = {
			1,2,3,4,5,	6,7,8,9,10,
			11,12,13,14,15,	16,17,18,19,20,
			21,22,23,24,25,	26,27,28,29,30,
			31,32,33,34,35,	36,37,38,39,40,
			41,42,43,44,45
		};
		
		for(int i=0;i<1000;i++) {
			int rnd = (int)(Math.random()*45);
			int a = arr45[rnd];
			int b = arr45[0];
			arr45[0]=a;
			arr45[rnd]=b;			
		}
		
		lbl1.setText(arr45[0]+"");
		lbl2.setText(arr45[1]+"");
		lbl3.setText(arr45[2]+"");
		lbl4.setText(arr45[3]+"");
		lbl5.setText(arr45[4]+"");
		lbl6.setText(arr45[5]+"");
		

		
	}

}

-MySwing05

package day04;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MySwing05 extends JFrame {

	private JPanel contentPane;
	private JTextField tf__mine1;
	private JTextField tf__com;
	private JTextField tf__result;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MySwing05 frame = new MySwing05();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MySwing05() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lbl_mine = new JLabel("나 :");
		lbl_mine.setBounds(27, 32, 62, 15);
		contentPane.add(lbl_mine);
		
		JLabel lbl_com = new JLabel("com :");
		lbl_com.setBounds(27, 65, 62, 15);
		contentPane.add(lbl_com);
		
		JLabel lbl_result = new JLabel("결과 :");
		lbl_result.setBounds(27, 107, 62, 15);
		contentPane.add(lbl_result);
		
		tf__mine1 = new JTextField();
		tf__mine1.setBounds(191, 29, 116, 21);
		contentPane.add(tf__mine1);
		tf__mine1.setColumns(10);
		
		tf__com = new JTextField();
		tf__com.setColumns(10);
		tf__com.setBounds(191, 62, 116, 21);
		contentPane.add(tf__com);
		
		tf__result = new JTextField();
		tf__result.setColumns(10);
		tf__result.setBounds(191, 104, 116, 21);
		contentPane.add(tf__result);
		
		JButton btn = new JButton("게임하기");
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				myclick();
			}
		});
		btn.setBounds(27, 145, 280, 23);
		contentPane.add(btn);
	}
	public void myclick() {
		String a = tf__mine1.getText();
		double b = Math.random();
		String com = "";
		if(b<0.5) {
			 com ="홀";
		}else {
			 com ="짝";
		}
	
		tf__com.setText(com);
		
		if(com.equals(a)) {
			tf__result.setText("이겼다");
			
		}else {
			tf__result.setText("졌다");
		}
	}
}

-MySwing06

package day04;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.SwingConstants;

public class MySwing06 extends JFrame {

	private JPanel contentPane;
	private JTextField tf;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MySwing06 frame = new MySwing06();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MySwing06() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		tf = new JTextField();
		tf.setHorizontalAlignment(SwingConstants.RIGHT);
		tf.setBounds(12, 38, 171, 21);
		contentPane.add(tf);
		tf.setColumns(10);
		
		JButton btn1 = new JButton("1");
		btn1.setBounds(12, 69, 53, 37);
		contentPane.add(btn1);
		
		JButton btn2 = new JButton("2");
		btn2.setBounds(77, 69, 49, 37);
		contentPane.add(btn2);
		
		JButton btn3 = new JButton("3");
		btn3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btn3.setBounds(127, 69, 56, 37);
		contentPane.add(btn3);
		
		JButton bt4 = new JButton("4");
		bt4.setBounds(12, 118, 64, 33);
		contentPane.add(bt4);
		
		JButton btn5 = new JButton("5");
		btn5.setBounds(76, 118, 70, 33);
		contentPane.add(btn5);
		
		JButton btn6 = new JButton("6");
		btn6.setBounds(137, 117, 76, 35);
		contentPane.add(btn6);
		
		JButton btn7 = new JButton("7");
		btn7.setBounds(12, 161, 53, 37);
		contentPane.add(btn7);
		
		JButton btn8 = new JButton("8");
		btn8.setBounds(78, 161, 68, 37);
		contentPane.add(btn8);
		
		JButton btn9 = new JButton("9");
		btn9.setBounds(147, 161, 56, 37);
		contentPane.add(btn9);
		
		JButton btn0 = new JButton("0");
		btn0.setBounds(12, 199, 70, 52);
		contentPane.add(btn0);
	
		JButton btncall = new JButton("call");
		btncall.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				mycall();
			}
		});
		btncall.setBounds(95, 199, 88, 52);
		contentPane.add(btncall);
		
		btn1.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {myclick(e);}});
		btn2.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {myclick(e);}});
		btn3.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {myclick(e);}});
		bt4.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {myclick(e);}});
		btn5.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {myclick(e);}});
		btn6.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {myclick(e);}});
		btn7.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {myclick(e);}});
		btn8.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {myclick(e);}});
		btn9.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {myclick(e);}});
		btn0.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {myclick(e);}});
		}
	
		public void mycall() {
			String str_tel = tf.getText();
			JOptionPane.showMessageDialog(null, "Calling\n"+str_tel);
		}
	
	
		public void myclick(MouseEvent e) {
//		System.out.println(e.getSource());
//		System.out.println(e.getComponent());
		JButton btn = (JButton)e.getSource();
		String str_old= tf.getText();
		String str_new= btn.getText();
		tf.setText(str_old+str_new); 
		System.out.println(btn.getText());
	}

}

 

728x90

'ddit > Java' 카테고리의 다른 글

ibatis  (0) 2022.07.29
싱글톤패턴(Singleton)  (0) 2022.07.27
MVC패턴(model-view-controller)< VO,DAO,Service,Controller>  (0) 2022.07.26
Stack, Queue, Sort  (0) 2022.06.22
Collection Framework(Array List, Vector...), generic / 문제풀기  (0) 2022.06.22