아래의 예제는 awt의 이벤트 처리 부분을 사용하여 구현해 본 간단한 에플릿 프로그램
같이구현
/*
* CheckboxTest.java
*
* Created on 2006년 12월 1일 (금), 오후 3:03
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
/**
*
* @author admin
*/
// awt의 이벤트 처리 입니다.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class CheckboxTest extends Frame implements ItemListener
{
/** Initialization method that will be called after the applet is loaded
* into the browser.
* Frame을 상속 받고 ItemListener 인터페이스를 상속 받습니다.
*/
Panel p;
Checkbox cb1,cb2;
TextArea txtStatus;
public CheckboxTest()
{
setLayout(new BorderLayout());
txtStatus = new TextArea();
p = new Panel();
cb1 = new Checkbox("딸기");
cb2 = new Checkbox("사과");
p.add(cb1);
p.add(cb2);
cb1.addItemListener(this); // 콤보박스1에 인터페이스를 건다.
cb2.addItemListener(this); // 콤보박스2에 인터페이스를 건다.
add(txtStatus,BorderLayout.CENTER);
add(p,BorderLayout.NORTH);
addWindowListener(new WindowAdapter()
{
// 윈도우가 종료 버튼 이벤트를 받을시 처리 되는 내부 무명 클래스..
public void windowClosing(WindowEvent evt)
{
dispose(); // 종료 매서드를 실행후
System.exit(0);// 시스템을 강제 종료 시킨다.
}
});
}
public void init() {
// TODO start asynchronous download of heavy resources
}
public static void main(String args[])
{
CheckboxTest cbt = new CheckboxTest();
cbt.setSize(300,300);
cbt.setVisible(true);
}
// TODO overwrite start(), stop() and destroy() methods
// 체크 박스가 선택 될시에 처리 되는곳
public void itemStateChanged(ItemEvent e)
{
Checkbox cb = (Checkbox)e.getSource();
if(cb.equals(cb1))
{
if(cb.getState())
txtStatus.setText("딸기가 선택되었어요");
else
txtStatus.setText("딸기선택을 해제했어요");
}
else
{
if(cb.getState())
txtStatus.setText("사과가 선택되었어요");
else
txtStatus.setText("사과선택을 해제했어요");
}
}
}
따로구현
/*
* ChoiceTest.java
*
* Created on 2006년 12월 1일 (금), 오후 3:45
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
/**
*
* @author admin
*/
import java.awt.*;
import java.awt.event.*;
public class ChoiceTest extends Frame
{
/** Initialization method that will be called after the applet is loaded
* into the browser.
*/
String[] item = {"서울","대전","대구","부산"};
Choice c;
TextArea txtStatus;
public ChoiceTest()
{
ChoiceHandler myHandler;
setLayout(new BorderLayout());
txtStatus = new TextArea();
c = new Choice();
myHandler = new ChoiceHandler(txtStatus,c);
c.addItemListener(myHandler);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
dispose();
System.exit(0);
}
});
for(int i=0;i<4;i++)
c.addItem(item[i]);
add(txtStatus,BorderLayout.CENTER);
add(c,BorderLayout.NORTH);
}
public void init() {
// TODO start asynchronous download of heavy resources
}
public static void main(String args[])
{
ChoiceTest ct = new ChoiceTest();
ct.setSize(300,300);
ct.setVisible(true);
}
// TODO overwrite start(), stop() and destroy() methods
}
class ChoiceHandler implements ItemListener
{
TextArea txtStatus;
Choice choice;
public ChoiceHandler(TextArea ta,Choice c)
{
txtStatus = ta;
choice = c;
}
public void itemStateChanged(ItemEvent e)
{
txtStatus.setText(choice.getSelectedItem()+"선택");
}
}
Trackback url :: http://hahakbs.dothost.co.kr/trackback/36


댓글을 달아 주세요