티스토리 뷰
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354import·java.awt.Color;¬import·java.awt.event.ActionEvent;¬import·java.awt.event.ActionListener;¬¬import·javax.swing.JButton;¬import·javax.swing.JFrame;¬import·javax.swing.JPanel;¬¬//·이벤트를·처리하는·방법·1·-·이벤트·리스터·클래스를·내부에·정의·(추천)¬//·1)·클래스·내부에·ActionLister·인터페이스를·상속하는·리스너클래스를·만든다.¬//·2)·리스너·클래스·안에·actionPerformed·메소드를·오버라이딩·한다.¬//·3)·버튼에·addActionListener·메소드로·리스너·인스턴스를·추가한다.¬¬class·MyFrame13·extends·JFrame{¬————JPanel·p·=·new·JPanel();¬————JButton·b1·=·new·JButton("빨강");¬————JButton·b2·=·new·JButton("파랑");¬————¬————MyListener·ml·=·new·MyListener();¬————¬————public·MyFrame13(){¬————————b1.addActionListener(ml);¬————————b2.addActionListener(ml);¬————————p.add(b1);¬————————p.add(b2);¬————————add(p);¬————————¬————————setBounds(800,150,300,200);¬————————setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);¬————————setVisible(true);¬————}¬————¬————¬————¬————class·MyListener·implements·ActionListener{¬————————@Override¬————————public·void·actionPerformed(ActionEvent·e){¬————————————if·(·e.getSource()·==·b1·){¬————————————————p.setBackground(Color.RED);¬————————————}¬————————————if·(·e.getSource()·==·b2·){¬————————————————p.setBackground(Color.BLUE);¬————————————}¬————————}¬————}¬}¬public·class·이벤트1·{¬¬————public·static·void·main(String[]·args)·{¬————————new·MyFrame13();¬————}¬¬}¬¶
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import·java.awt.event.ActionEvent;¬import·java.awt.event.ActionListener;¬¬import·javax.swing.JButton;¬import·javax.swing.JFrame;¬import·javax.swing.JPanel;¬¬//·이벤트를·처리하는·방법·2·-·이벤트·리스터·클래스를·별도로·정의¬//·1)·외부에·ActionLister·인터페이스를·상속하는·리스너클래스를·만든다.¬//·2)·리스너·클래스·안에·actionPerformed·메소드를·오버라이딩·한다.¬//·---*·actionPerfomed·안에서·JButton의·인스턴스를·받아서·제어해야·함¬//·3)·버튼에·addActionListener·메소드로·리스너·인스턴스를·추가한다.¬¬¬class·MyListener·implements·ActionListener{¬————@Override¬————public·void·actionPerformed(ActionEvent·e){¬————————JButton·b·=·(JButton)e.getSource();¬————————String·s·=·b.getText();¬————————int·a·=·Integer.parseInt(s);¬————————a++;¬————————b.setText(Integer.toString(a));¬————}¬}¬¬class·MyFrame14·extends·JFrame{¬————JPanel·p·=·new·JPanel();¬————JButton·b1·=·new·JButton("0");¬————¬————MyListener·ml·=·new·MyListener();¬————public·MyFrame14(){¬————————b1.addActionListener(ml);¬————————p.add(b1);¬————————add(p);¬————————¬————————setBounds(800,150,300,200);¬————————setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);¬————————setVisible(true);¬————}¬¬}¬public·class·이벤트2·{¬¬————public·static·void·main(String[]·args)·{¬————————new·MyFrame14();¬————}¬¬}¬¶
123456789101112131415161718192021222324252627282930import·java.awt.FlowLayout;¬¬import·javax.swing.JFrame;¬import·javax.swing.JTextField;¬¬class·MyFrame4·extends·JFrame{¬————JTextField·tf1·=·new·JTextField();¬————JTextField·tf2·=·new·JTextField(20);¬————JTextField·tf3·=·new·JTextField("초기문자열",10);¬————JTextField·tf4·=·new·JTextField("초기문자열",10);¬————¬————public·MyFrame4(){¬————————setLayout(new·FlowLayout());¬————————tf4.setEditable(false);¬————————add(tf1);———add(tf2);———add(tf3);———add(tf4);¬————————¬————————setSize(300,200);¬————————setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);¬————————setVisible(true);¬————}¬}¬public·class·텍스트필드1·{¬¬————public·static·void·main(String[]·args)·{¬————————new·MyFrame4();¬¬————}¬¬}¬¶
123456789101112131415161718192021222324252627282930313233343536373839404142import·java.awt.FlowLayout;¬import·java.awt.event.ActionEvent;¬import·java.awt.event.ActionListener;¬¬import·javax.swing.JFrame;¬import·javax.swing.JTextField;¬¬//·이벤트를·처리하는·방법3¬//·:·컨테이너·클래스에서·직접·리스너·인터페이스를·구현¬¬class·MyFrame5·extends·JFrame·implements·ActionListener{¬————JTextField·tf1·=·new·JTextField(20);¬————JTextField·tf2·=·new·JTextField(20);¬————¬————¬————public·MyFrame5(){¬————————setLayout(new·FlowLayout());¬————————tf1.addActionListener(this);¬————————tf2.setEditable(false);¬————————add(tf1);·add(tf2);¬————————¬————————setSize(300,200);¬————————setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);¬————————setVisible(true);¬————————¬————}¬————¬————@Override¬————public·void·actionPerformed(ActionEvent·e){¬————————String·s·=·tf1.getText();¬————————tf2.setText(s);¬————}¬}¬public·class·텍스트필드2·{¬¬————public·static·void·main(String[]·args)·{¬————————new·MyFrame5();¬¬————}¬¬}¬¶
12345678910111213141516171819202122232425262728293031323334353637383940414243444546import·java.awt.FlowLayout;¬import·java.awt.event.ActionEvent;¬import·java.awt.event.ActionListener;¬¬import·javax.swing.JButton;¬import·javax.swing.JFrame;¬¬//·이벤트를·처리하는·방법·4¬//·리스너클래스를·익명클래스로·작성¬¬class·MyFrame10·extends·JFrame{¬————JButton·btn1·=·new·JButton("크기변경");¬————JButton·btn2·=·new·JButton("작게");¬————¬————public·MyFrame10(){¬————————setLayout(new·FlowLayout());¬————————btn1.addActionListener(new·ActionListener(){¬————————————@Override¬————————————public·void·actionPerformed(ActionEvent·e)·{¬————————————————setSize(600,800);¬————————————}¬————————});¬————————btn2.addActionListener(new·ActionListener(){¬————————————@Override¬————————————public·void·actionPerformed(ActionEvent·e)·{¬————————————————setSize(300,400);¬————————————}¬————————});¬————————¬————————add(btn1);——add(btn2);¬————————setSize(300,400);¬————————setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);¬————————setVisible(true);———————¬————}¬————¬————¬}¬public·class·이벤트4·{¬¬————public·static·void·main(String[]·args)·{¬————————new·MyFrame10();¬¬————}¬¬}¬¶
'JAVA > PRIMER' 카테고리의 다른 글
[JAVA] GUI - 키 이벤트 (0) | 2018.04.19 |
---|---|
[JAVA] GUI - JTextField, JTextArea (0) | 2018.04.19 |
[JAVA] GUI - 배치관리자 (0) | 2018.04.19 |
[JAVA] GUI - JPanel, JLabel, JButton (0) | 2018.04.19 |
[JAVA] GUI - JFrame (0) | 2018.04.19 |