Java / AWT 맛보기
Posted 2007/11/07 10:10, Filed under: 프로그래밍/Java
import java.awt.*;
import java.awt.event.*;
public class AWT extends Frame implements ActionListener, ItemListener
{
protected MenuItem exit;
protected TextField field;
protected TextArea text;
protected Choice choice;
public AWT()
{
super("AWT테스트");
makeMenu();
makeBody();
setSize(400, 500);
setVisible(true);
}
protected void makeMenu()
{
MenuBar bar = new MenuBar();
Menu file = new Menu("File");
exit = new MenuItem("Exit");
exit.addActionListener(this);
file.add(exit);
bar.add(file);
setMenuBar(bar);
}
protected void makeBody()
{
Panel top = new Panel(new BorderLayout());
top.add(new Label("데이터"), BorderLayout.WEST);
field = new TextField();
field.addActionListener(this);
top.add(field, BorderLayout.CENTER);
add(top, BorderLayout.NORTH);
text = new TextArea();
add(text, BorderLayout.CENTER);
choice = new Choice();
choice.addItemListener(this);
choice.add("사과");
choice.add("배");
choice.add("귤");
add(choice, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if (o == exit)
{
setVisible(false);
System.exit(0);
}else if(o == field){
text.append(field.getText());
text.append("\n");
field.setText("");
}
}
public void itemStateChanged(ItemEvent e)
{
Object o = e.getSource();
if (o == choice)
{
if (e.getStateChange() == ItemEvent.SELECTED)
{
String item = choice.getSelectedItem();
text.append(item);
text.append("\n");
}
}
}
public static void main(String args[])
{
new AWT();
}
}

Response :
0 Trackback
,
0 Comment
Trackback URL : http://mysilpir.net/trackback/271



