-
23일 차 Swing 버튼클릭시 텍스트 변하게(ActionListener)삼성SDS_멀티캠퍼스/Java 2015. 10. 12. 10:49반응형
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Action {
public static void main(String[] args) {
Frame f = new Frame();
}
}
class Frame extends JFrame {
private JButton button;
private JPanel panel;
private JLabel label;
public Frame() {
this.setTitle("이벤트");
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
label = new JLabel("라벨임");
button = new JButton("버튼클릭");
button.addActionListener(new Listener());
panel.add(button);
panel.add(label);
this.add(panel);
this.setVisible(true);
}
class Listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button)
button.setText("눌렸어");
}
}
}
처리방식은 내부클래스로 했습니다.
내부클래스로 안하면 너무너무 힘들기 때문에
가급적이면 내부클래스로 합시다
반응형'삼성SDS_멀티캠퍼스 > Java' 카테고리의 다른 글
23일 차 Swing 계산기 만들기 (0) 2015.10.12 23일 차 Swing 버튼2개를 만들어서 클릭시 텍스트 변경 (0) 2015.10.12 23일 차 Swing의 기본 JFrame (0) 2015.10.12 22일 차 SVN 처음부터 끝까지! (下편) (0) 2015.10.08 22일 차 SVN 처음부터 끝까지! (上편) (0) 2015.10.08