package awt;
import java.awt.*;
import java.awt.event.*;
public class ButtonTest2 extends Frame implements ActionListener
{
Label lblOut; //라벨 만들기
Button btnLeft, btnRight; //버튼 선언
public ButtonTest2(String title)
{
super(title);
this.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
this.setDesign(); //생성자에 호출
this.setLocation(300,100);
this.setSize(300,300);
this.setVisible(true);
}
//버튼 액션 구현
@Override
public void actionPerformed(ActionEvent e)
{
Object ob=e.getSource();
if(ob==btnLeft)
{
lblOut.setText("왼쪽 버튼을 누름");
}
else if(ob==btnRight)
{
lblOut.setText("오른쪽 버튼을 누름");
}
}
private void setDesign()
{
lblOut=new Label("Button Click!!!");
btnLeft=new Button("Left");
btnRight=new Button("Right");
btnLeft.setForeground(Color.red);
btnRight.setForeground(Color.blue);
Panel pTop=new Panel();
pTop.setBackground(Color.orange);
this.add("North",pTop);
pTop.add(btnLeft);
pTop.add(btnRight);
Panel pBottom=new Panel(new BorderLayout());
pBottom.setBackground(new Color(200,255,255));
pBottom.add(lblOut);
this.add("South",pBottom);
//버튼에서 이벤트 발생
btnLeft.addActionListener(this);
btnRight.addActionListener(this);
}
public static void main(String[] args)
{
new ButtonTest2("버튼 생성 연습");
}
}
...................................................기존까지는 class를 만들어서 했는데..
import해서 하니까 API를 안 살펴보니까.. 완전 ㅠㅠ 그냥 이거 만들어 하면 못할듯.
한 코드 한 코드 뜯어봐야겠다.
본 블로그는 페이스북 댓글을 지원합니다.