반응형

 

버튼 클릭 시 글자 변경

 

1

 

1. show events 클릭

 

2

 

2. mouse > clicked 더블 클릭

 

3

 

3. 소스 내용 추가된 것을 확인 > 소스 추가 후 테스트

 

 

 

  • 버튼 클릭 시 숫자 1 증가
package day04;

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 MySwing02 extends JFrame {

	private JPanel contentPane;
	JLabel lbl;

	/**
	 * 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);
		
		lbl = new JLabel("200");
		lbl.setBounds(36, 30, 57, 15);
		contentPane.add(lbl);
		
		// 버튼 클릭 시 아래 작동
		JButton btn = new JButton("INCREASE");
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				myclick();
			}
		});
		btn.setBounds(145, 26, 97, 23);
		contentPane.add(btn);
	}
	
	void myclick() {
		int su = Integer.parseInt(lbl.getText());
		su = su + 1;
		lbl.setText(String.valueOf(su));
	}

}

 

결과 화면1

 

 

 

  • 두 수를 받아와 버튼 클릭 시 곱하기
package day04;

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.JComboBox;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MySwing03 extends JFrame {

	private JPanel contentPane;
	private JTextField tf01;
	private JTextField tf03;
	private JTextField tf02;

	/**
	 * 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);
		
		tf01 = new JTextField();
		tf01.setBounds(12, 25, 86, 21);
		contentPane.add(tf01);
		tf01.setColumns(10);
		
		tf03 = new JTextField();
		tf03.setColumns(10);
		tf03.setBounds(306, 25, 116, 21);
		contentPane.add(tf03);
		
		tf02 = new JTextField();
		tf02.setColumns(10);
		tf02.setBounds(131, 25, 86, 21);
		contentPane.add(tf02);
		
		JLabel lbl = new JLabel("*");
		lbl.setBounds(110, 28, 20, 15);
		contentPane.add(lbl);
		
		JButton btn = new JButton("=");
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				myclick();
			}
		});
		btn.setBounds(229, 24, 65, 23);
		contentPane.add(btn);
	}
	
	void myclick() {
		String txt1 = tf01.getText();
		int txt01 = Integer.parseInt(txt1);
		String txt2 = tf02.getText();
		int txt02 = Integer.parseInt(txt2);
		
		int result = txt01 * txt02;
		
		tf03.setText(String.valueOf(result));
	}
}

 

결과 화면2

 

 

 

  • 구구단  출력하기
package day04;

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 javax.swing.JTextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MySwing04 extends JFrame {

	private JPanel contentPane;
	private JTextField tf;
	JTextArea ta;

	/**
	 * 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, 240, 523);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lbl = new JLabel("출력단수");
		lbl.setBounds(12, 10, 57, 15);
		contentPane.add(lbl);
		
		tf = new JTextField();
		tf.setBounds(103, 7, 109, 21);
		contentPane.add(tf);
		tf.setColumns(10);
		
		JButton btn = new JButton("출력하기");
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				myClick();
			}
		});
		btn.setBounds(12, 35, 200, 23);
		contentPane.add(btn);
		
		ta = new JTextArea();
		ta.setBounds(12, 68, 200, 406);
		contentPane.add(ta);
	}
	
	void myClick() {
		String tex = tf.getText();
		int te = Integer.parseInt(tex);
		System.out.println(te);
		
		String result= "";
		
		for(int i=1; i<=9; i++) {
			result += te + " x " + i + " = " + te*i + "\n";
//			System.out.println(result);
		}
		ta.setText(result);
	}
}

 

결과 화면3

 

 

 

  • 로또 번호 출력
package day04;

import java.awt.EventQueue;

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

public class MySwing05 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 {
					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);
		
		lbl1 = new JLabel("__");
		lbl1.setFont(new Font("굴림", Font.BOLD | Font.ITALIC, 18));
		lbl1.setBounds(32, 38, 31, 15);
		contentPane.add(lbl1);
		
		lbl2 = new JLabel("__");
		lbl2.setFont(new Font("굴림", Font.BOLD | Font.ITALIC, 18));
		lbl2.setBounds(90, 38, 31, 15);
		contentPane.add(lbl2);
		
		lbl3 = new JLabel("__");
		lbl3.setFont(new Font("굴림", Font.BOLD | Font.ITALIC, 18));
		lbl3.setBounds(155, 38, 31, 15);
		contentPane.add(lbl3);
		
		lbl4 = new JLabel("__");
		lbl4.setFont(new Font("굴림", Font.BOLD | Font.ITALIC, 18));
		lbl4.setBounds(225, 38, 31, 15);
		contentPane.add(lbl4);
		
		lbl5 = new JLabel("__");
		lbl5.setFont(new Font("굴림", Font.BOLD | Font.ITALIC, 18));
		lbl5.setBounds(301, 38, 31, 15);
		contentPane.add(lbl5);
		
		lbl6 = new JLabel("__");
		lbl6.setFont(new Font("굴림", Font.BOLD | Font.ITALIC, 18));
		lbl6.setBounds(366, 38, 31, 15);
		contentPane.add(lbl6);
		
		JButton btn = new JButton("로또 생성 하기");
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				myClick();
			}
		});
		btn.setBounds(32, 78, 365, 23);
		contentPane.add(btn);
	}
	
	void myClick() {
		int[] arr = {
						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<999; i++) {
			int rnd = (int)(Math.random()*45);
			int temp = arr[0];
			arr[0] = arr[rnd];
			arr[rnd] = temp;
		}
		
		System.out.println(arr[0] + "\t" + arr[1] + "\t" + arr[2] + "\t" + arr[3] + "\t" + arr[4] + "\t" + arr[5]);
		
		lbl1.setText(String.valueOf(arr[0]));
		lbl2.setText(String.valueOf(arr[1]));
		lbl3.setText(String.valueOf(arr[2]));
		lbl4.setText(String.valueOf(arr[3]));
		lbl5.setText(String.valueOf(arr[4]));
		lbl6.setText(String.valueOf(arr[5]));
	}

}

 

결과 화면4

 

 

반응형