본문 바로가기
IT창고/JAVA

싱글 채팅방..!!

by 창구창고 2007. 1. 22.
반응형

import java.awt.*;
import java.awt.event.*;

public class ChatClient extends Frame implements ActionListener{
 Button quit, print;
 Panel p1;
 Choice username;
 
 private TextArea output;
 private TextField input;
 private MenuItem quitMenu;
 private MenuItem aboutMenu ;


 public ChatClient(){
 
  super("Chat Room");
 
  print = new Button("Send");
  print.addActionListener(this);
 
  quit = new Button("Exit");
  quit.addActionListener(this);
 
  output = new TextArea(10,50);
  input = new TextField(50);
 
  username = new Choice();
  username.add("Na");
  username.add("Ne");
  username.add("mint");
  username.add("J.Two");
  username.add("lovelysoul");
  p1 = new Panel();
  p1.add(print);
  p1.add(quit);
  p1.add(username);
 
  MenuBar menubar = new MenuBar();
  setMenuBar(menubar);
  
  Menu file = new Menu("File");
  quitMenu = new MenuItem("quit");
  file.add(quitMenu);
  
  Menu help = new Menu("Help");
  aboutMenu = new MenuItem("about");
  help.add(aboutMenu);
  
  menubar.add(file);
  menubar.add(help);
 
  quitMenu.addActionListener(this);
  aboutMenu.addActionListener(this);
   
  add(p1, BorderLayout.EAST);
  add(output, BorderLayout.WEST);
  add(input, BorderLayout.SOUTH);
  input.addActionListener(this);
  
  setSize(440,210);
  setVisible(true);
  addWindowListener(new WindowHandler());
 }
 public void actionPerformed(ActionEvent e) {
  if(e.getSource() == print) {
   output.append("\n" + username.getSelectedItem() + ": " + input.getText());
   input.setText("");
  }else if(e.getSource() == input){
   output.append("\n" + username.getSelectedItem() + ": " + input.getText());
   input.setText("");  
  }else if(e.getSource() ==aboutMenu){
   AboutDialog mydlg = new AboutDialog(this, "about", true);
   mydlg.show();
  }else if(e.getSource() == quit) {
   setVisible(false);
   dispose();
   System.exit(0);
  }
 }
 
 public class WindowHandler extends WindowAdapter {
  public void windowClosing(WindowEvent e) {
   Window  w = e.getWindow();
   w.setVisible(false);
   w.dispose();
   System.exit(0);
  }
 }

 private class AboutDialog extends Dialog implements ActionListener {
  public AboutDialog(Frame parent, String title, boolean modal) {
   super(parent, title, modal);
   add(new Label("이름"), BorderLayout.NORTH);
   Button b = new Button("OK");
   add(b, BorderLayout.SOUTH);
   pack();
   b.addActionListener(this);
  }
 
  public void actionPerformed(ActionEvent e) {
   setVisible(false);
  }
 }
 
 public static void main(String[] args) {
  ChatClient client = new ChatClient();
 }
}

반응형

"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."