1. 프레임 만들기

    2. 프레임에 스윙 컴포넌트 붙이기

    3. main() 메서드 작성

     

    package kr.or.kihd.swingtest;
    
    import javax.swing.JFrame;
    
    public class SwingTest1 {
    
    	public static void main(String[] args) {
    		
    		JFrame jFrame = new JFrame();
    		jFrame.setTitle("프레임연습");
    		jFrame.setSize(300, 300);
    		jFrame.setVisible(true);
    		
    	}
    
    }
    
    package kr.or.kihd.swingtest;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class SwingTest2 extends JFrame{
    
    	public SwingTest2() {
    		super("컴포넌트 추가하기");
    		//프레임이 닫힐때 프로그램이 같이 종료된다.
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    		Container contentPane = getContentPane();
    		contentPane.setLayout(new FlowLayout());
    		contentPane.setBackground(Color.yellow);
    		
    		setSize(500, 500);
    		setVisible(true);
    		
    		contentPane.add(new JButton("확인"));
    		contentPane.add(new JButton("취소"));
    
    	}
    	
    	public static void main(String[] args) {
    		new SwingTest2();
    		
    	}
    
    }
    

    Swing 

    package kr.or.kihd.swingtest;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class SwingTest3 extends JFrame{
    
    	Container container;
    	JButton btn1, btn2, btn3, btn4;
    	
    	public SwingTest3() {
    		super("버튼 제작 테스트");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    		container = getContentPane();
    		container.setLayout(new FlowLayout());
    		
    		//일반 버튼
    		btn1 = new JButton("일반 버튼");
    		btn1.addActionListener(new EventHandler());
    		container.add(btn1);
    		
    		//컬러와 모양을 바꾼 버튼
    		btn2 = new JButton("버튼2");
    		btn2.setOpaque(true);
    		btn2.setForeground(Color.blue);
    		btn2.setBackground(Color.yellow);
    		btn2.setFont(new Font("맑은고딕",Font.BOLD,20));
    		btn2.addActionListener(new EventHandler());
    		container.add(btn2);
    		
    		//비활성화 버튼
    		btn3 = new JButton("버튼 3");
    		btn3.setEnabled(false);
    		btn3.addActionListener(new EventHandler());
    		container.add(btn3);
    		
    		//토굴 버튼
    		btn4 = new JButton("토굴");
    		btn4.addActionListener(new EventHandler());
    		container.add(btn4);
    		
    		
    		setSize(300, 300);
    		setVisible(true);
    				
    		
    		setSize(300, 300);
    		this.setLocation(500,300);
    		setVisible(true);
    	}
    	
    	public class EventHandler implements ActionListener{
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			
    			Object obj = e.getSource();
    			
    			if(obj == btn1) {
    				System.out.println("위치 : " + btn1.getX() +
    						", " + btn1.getY() + 
    						" 크기: " + btn1.getSize());
    			}
    			else if(obj == btn1) {
    				System.out.println("폰트 : " + btn2.getFont() +
    						"글자색 : " + btn2);
    			}
    			else {
    				if(btn1.isVisible()) {
    					btn1.setVisible(false);
    					btn2.setVisible(false);
    					btn3.setVisible(false);
    				}else {
    					btn1.setVisible(true);
    					btn2.setVisible(true);
    					btn3.setVisible(true);
    				}
    			}
    			
    		}
    	}
    	
    	
    	public static void main(String[] args) {
    		new SwingTest3();
    
    	}
    
    }
    


    Swing 이벤트 유니코드 출력

    package kr.or.kihd.swingtest;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class KeyEventTest extends JFrame {
    	
    	JPanel jPanel = new JPanel();
    	JLabel[] lbMsg; 
    	
    	public KeyEventTest() {
    		super("키 이벤트 연습");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    		setContentPane(jPanel);
    		
    		lbMsg = new JLabel[3];
    		
    		lbMsg[0] = new JLabel("키코드값");
    		lbMsg[1] = new JLabel("유니코드값");
    		lbMsg[2] = new JLabel("키보드의 문자열");
    		
    		for(int i = 0 ; i<lbMsg.length; i++){
    			jPanel.add(lbMsg[i]);
    			lbMsg[i].setOpaque(true); //배경색을 불투명하게함.
    			lbMsg[i].setBackground(Color.green);
    			lbMsg[i].setFont(new Font("맑은고딕",Font.BOLD,20));
    			
    		}
    		setSize(500,300);
    		setLocation(500,300);
    		setVisible(true);
    		
    		jPanel.addKeyListener(new EventHandler());
    		jPanel.requestFocus();
    		
    	}
    	
    	//내부클래스
    	public class EventHandler extends KeyAdapter{
    		@Override
    		public void keyPressed(KeyEvent ke) {
    			int keyCode = ke.getKeyCode();
    			char keyChar = ke.getKeyChar();
    			
    			lbMsg[0].setText(Integer.toString(keyCode));
    			lbMsg[1].setText(Character.toString(keyChar));
    			lbMsg[2].setText(ke.getKeyText(keyCode));
    		}
    	}
    	
    	
    	
    	public static void main(String[] args) {
    		new KeyEventTest();
    		
    	}
    
    }
    


    가위바위보 게임 만들기

    img.zip
    0.02MB

    package kr.or.kihd.swingtest;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class GBBGame extends JFrame {
    	
    	ImageIcon[] img = {
    		new ImageIcon("img/kawi.jpg"),
    		new ImageIcon("img/bawi.jpg"),
    		new ImageIcon("img/bo.jpg")
    	};
    	
    	//가위바위보 버튼
    	class SelectPanel extends JPanel {
    		JButton[] btn = new JButton[3];
    		public SelectPanel() {
    			setBackground(Color.blue);
    			for(int i = 0; i<img.length; i++) {
    				btn[i] = new JButton(img[i]);
    				this.add(btn[i]); //패널에 버튼을 add.
    				
    				btn[i].addActionListener(new EventHandler());
    			}
    		}
    	}
    	SelectPanel sPanel = new SelectPanel();
    	
    	//가위바위보 결과
    	class ResultDisplay extends JPanel{
    		JLabel userlb = new JLabel("user");
    		JLabel com = new JLabel("computer");
    		JLabel result = new JLabel("winner");
    		public ResultDisplay() {//패널에 연결
    			setBackground(Color.yellow);
    			add(userlb); add(result); add(com);
    		}
    		
    		
    		public void output(Icon img, Icon comImage, String res) {
    			userlb.setIcon(img);
    			userlb.setHorizontalTextPosition(JLabel.LEFT);
    			com.setIcon(comImage);
    			result.setText(res);
    			result.setForeground(Color.red);
    			result.setFont(new Font(Font.DIALOG, Font.BOLD, 20));
    		}
    		
    	}
    	ResultDisplay resultDisplay = new ResultDisplay();
    	class EventHandler implements ActionListener{ //버튼 눌렀을때 이벤트가 되도록함
    
    		@Override
    		public void actionPerformed(ActionEvent ae) {
    			//사용자가 선택한 버튼
    			JButton btnSrc = (JButton) ae.getSource();
    			//컴퓨터의 랜덤선택 (0:가위, 1:바위, 2:보)
    			int btnCom = (int) (Math.random()*3);
    			
    			//가위바위보 결과값을 저장할 문자열 선언
    			String result = "";
    			
    			//사용자가 이기는 경우
    			if(btnSrc.getIcon() == img[0] && btnCom == 2 || 
    					btnSrc.getIcon() == img[1] && btnCom == 0 ||
    					btnSrc.getIcon() == img[2] && btnCom == 1) 
    				result = "당신이 이겼습니다.";
    			else if(btnSrc.getIcon() == img[0] && btnCom == 0 || 
    				btnSrc.getIcon() == img[1] && btnCom == 1 ||
    				btnSrc.getIcon() == img[2] && btnCom == 2)
    				result = "비겼습니다";
    			else
    				result = "당신이 졌습니다.";
    			//결과값을 출력한다.
    			resultDisplay.output(btnSrc.getIcon(),img[btnCom], result);
    		}
    		
    	}
    	
    	
    	public GBBGame() {
    		super("가위 바위 보 게임");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    		add(sPanel, "South");
    		add(resultDisplay,"Center");
    		
    		setSize(500, 500);
    		setLocation(500, 150);
    		setVisible(true);
    		}
    		
    	public static void main(String[] args) {
    		new GBBGame();
    		
    	}
    
    }
    


    Process(프로세스) 와 Thread(쓰레드)

     

    Swing Thread

    package kr.or.kihd.swingtest;
    
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Font;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class SwingThreadTest extends JFrame{
    
    	public SwingThreadTest() {
    		super("스레드 활용");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    		Container container = getContentPane();
    		container.setLayout(new FlowLayout());
    		
    		JLabel jLabel = new JLabel();
    		jLabel.setFont(new Font("Dotum", Font.ITALIC, 60));
    		
    		container.add(jLabel);
    		
    		setSize(300, 200);
    		setLocation(500, 300);
    		setVisible(true);
    		
    		TimeThread thread = new TimeThread(jLabel);
    		thread.start();
    	}
    
    	public static void main(String[] args) {
    		new SwingThreadTest();
    		
    	}
    
    }
    class TimeThread extends Thread {
    	JLabel timelb;
    	public TimeThread(JLabel timelb) {
    		this.timelb = timelb;
    	}
    	
    	@Override
    	public void run() {
    		int n = 0;
    		
    		while(true) {
    			timelb.setText(" " + Integer.toString(n) + " ");
    			n++;
    			try {
    				sleep(1000);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	
    }

     

    SwingThreadFlag

     

    • 네이버 블러그 공유하기
    • 네이버 밴드에 공유하기
    • 페이스북 공유하기
    • 카카오스토리 공유하기
    loading