AWT>Button
java.awt.Button 클래스
- addActionListener() : 버튼이 클릭되면 통보받는다.
- setLabel() : 버튼을 위한 레이블 텍스트 설정
- removeActionListener() : 버튼과 리스터 클래스 사이의 연결 제거
* 프레임에 버튼 추가하기
import java.awt.*;
import java.awt.event.*;
public class TestButton extends Frame implements ActionListener {
Button btnExit; //버튼객체의 주소를 할당받을수 있는 변수
public TestButton() {
btnExit = new Button("Exit"); //실제 객체 생성//Button의 레이블에 나타날 글꼴 제어
btnExit.setFont(new Font("Courier", Font.BOLD, 24));
//배경색 설정
btnExit.setBackground(Color.cyan);//Cursor객체 : 마우스가 객체 위에 있을때 커서모양 제어
Cursor curs = new Cursor(HAND_CURSOR); //손모양 커서로 지정
btnExit.setCursor(curs);
btnExit.addActionListener(this);
add(btnExit);
this.setLayout(new FlowLayout());
addWindowListener(new WinCloser());
setTitle("Using a Button and an ActionListener");
setBounds(100,100,300,300);
setVisible(true);
}
//버튼이 클릭될때마다 호출된다.
public void actionPerformed(ActionEvent ae) {
if(ae.getActionCommand().equals("Exit"));//버튼이 클릭되었을때 어떤 버튼인지 설정한다.
System.exit(0);
}
public static void main(String[] args) {
TestButton td = new TestButton();
}
}
class WinCloser extends WindowAdapter {
public void WindowClosing(WindowEvent e) {
System.exit(0);
}
}*