열혈강의 자바 22장 과제 1

JAVA/소스 2012. 1. 11. 02:54

//미완성... 수정 부분... 짱나서 여기까지...
import java.awt.*;
import java.awt.List;
import java.util.*;
import java.awt.event.*;

class F extends Frame implements ActionListener,MouseListener{
 
 private Button bt1 =new Button("글쓰기");
 private Button bt2 =new Button("새로고침");
 private Button bt3 =new Button("수정");
 private Button bt4 =new Button("삭제");
 private Button bt5 =new Button("종료");
 private Button bt6 =new Button("찾아보기");
 private Button bt7 =new Button("등록");
 private Button bt8 =new Button("취소");

 private Label lb1=new Label("글목록!",Label.LEFT);
 private Label lb2=new Label("글보기!",Label.LEFT);
 private Label lb3=new Label("글쓰기",Label.CENTER);
 private Label lb4=new Label("글제목 : ",Label.RIGHT);
 private Label lb5=new Label("작성자 : ",Label.RIGHT);
 private Label lb6=new Label("첨부파일 : ",Label.RIGHT);
  
 
 private List li=new List();
 
 private TextArea ta1=new TextArea();
 private TextArea ta2=new TextArea();

 private TextField tf1=new TextField();
 private TextField tf2=new TextField();
 private TextField tf3=new TextField();

 private FlowLayout fl1=new FlowLayout();
 private BorderLayout bl1=new BorderLayout(5,5);
 private GridLayout gl1=new GridLayout(1,2,5,5);
 private GridLayout gl2=new GridLayout(1,2,5,5);
 private GridLayout gl3=new GridLayout(1,2,5,5);
 private GridLayout gl4=new GridLayout(1,2,5,5);
 private GridLayout gl5=new GridLayout(1,3,5,5);
 
 private Panel p1=new Panel();//글목록! 글보기!
 private Panel p2=new Panel();//ta1 li
 private Panel p3=new Panel();//p4,p5
 private Panel p4=new Panel();//글쓰기 새로고침
 private Panel p5=new Panel();//수정 삭제 종료
 
 //////Dialog 변수 write함수 쪽에서 다씀  위쪽에 버튼이랑 텍스트 몇개 씀
 final Dialog dlg =new Dialog(this,"글쓰기",true);
 private BorderLayout br2=new BorderLayout();
 private BorderLayout br3=new BorderLayout();
 
 private GridLayout gl6 =new GridLayout(3,1);
 private GridLayout gl7 =new GridLayout(3,1);
 private GridLayout gl8 =new GridLayout(1,2);
 private FlowLayout fl2 =new FlowLayout(FlowLayout.RIGHT);
 
 private Panel p6=new Panel();
 private Panel p7=new Panel();
 private Panel p8=new Panel();
 private Panel p9=new Panel();
 private Panel p10=new Panel();
 
 //등록 할때 쓸거
 private String title;
 private String author;
 private String file;
 private int number=0;
 private ArrayList li1=new ArrayList();
 public F(){
  super("게시판!");
  
  this.init();
  this.start();
  
  this.setSize(500,400);
  
  Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
  Dimension frm=this.getSize();
  
  int x=(int)(screen.width/2-frm.width/2);
  int y=(int)(screen.height/2-frm.height/2);
  
  this.setLocation(x,y);
  this.setVisible(true);
  
 }
 public void init(){//초기화면 창
  this.setLayout(bl1);
  p1.setLayout(gl1);
  p1.add(lb1);
  p1.add(lb2);
  this.add("North",p1);
  p2.setLayout(gl2);
  p2.add(li);
  ta1.setEditable(false); //수정못함 설정
  ta1.setBackground(new Color(144,122,44));
  p2.add(ta1);
  this.add("Center",p2);
  p3.setLayout(gl3);
  p4.setLayout(gl4);
  p4.add(bt1);
  p4.add(bt2);
  p3.add(p4);
  p5.setLayout(gl5);
  p5.add(bt3);
  p5.add(bt4);
  p5.add(bt5);
  p3.add(p5);
  this.add("South",p3);

 }
 public void start(){
  bt7.addActionListener(this);
  bt1.addActionListener(this);
  bt6.addActionListener(this);
  bt2.addActionListener(this);
  bt4.addActionListener(this);//삭제
  bt3.addMouseListener(this);//수정
  li.addMouseListener(this);
  //취소,종료
  bt8.addActionListener(this);
  bt5.addActionListener(this);
  
  //게시판 x종료
  addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e){
    dispose();
    System.exit(0);
   }
  });
 }
 public void write(){ //글쓰기 창
  
  dlg.setSize(300,500);
  dlg.setLayout(br2);
  p6.setLayout(br3);
  
  p7.setLayout(gl6);
  p7.add(lb4);
  p7.add(lb5);
  p7.add(lb6);
  p6.add("West",p7);
  
  p8.setLayout(gl7);
  p8.add(tf1);
  p8.add(tf2);
  p9.setLayout(gl8);
  tf3.setEditable(false); //수정 못함 설정
  p9.add(tf3);
  p9.add(bt6);
  p8.add(p9);
  p6.add("Center",p8);
  p6.add("North",lb3);
 
  
  p10.setLayout(fl2);
  p10.add(bt7);
  p10.add(bt8);
  
  
  dlg.add("South",p10);
  dlg.add("Center",ta2);
  dlg.add("North",p6);
  
  //글쓰기 x 종료
  dlg.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e){
    dlg.setVisible(false);
   }
  });
  
  
  
  Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
  Dimension frm=this.getSize();
  
  int x=(int)(screen.width/2-frm.width/4);
  int y=(int)(screen.height/2-frm.height/2);
  dlg.setLocation(x,y);
  dlg.setVisible(true);
 }
 @Override
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  if(e.getSource()==bt1){
   write();
  }
  else if(e.getSource()==bt5){
   System.exit(0);
  }
  else if(e.getSource()==bt6){
   FileDialog fdlg=new FileDialog(this,"찾아보기",FileDialog.LOAD);
   fdlg.setVisible(true);
   String dir=fdlg.getDirectory();
   tf3.setText(dir);
  }
  else if(e.getSource()==bt8){
     dlg.setVisible(false);
  }
  else if(e.getSource()==bt7){
   title=tf1.getText();
   author=tf2.getText();
   file=tf3.getName();
   li.add(number+1+" : "+title+" ("+author+")");
   li1.add(lb4.getText()+title+"\n\n"+lb5.getText()+author+"\n\n"+lb6.getText()+file+"\n\n"+"내용 : \n\n"+ta2.getText());
   String str=(String)li1.get(number);
   ta1.setText(str);
   
   
   
   //글쓰기창 공백으로 초기화
   tf1.setText("");
   tf2.setText("");
   tf3.setText("");
   ta2.setText("");
   
   number++;
   dlg.setVisible(false);
  }
  else if(e.getSource()==bt4){

   int del=li.getSelectedIndex();
   li.remove(del);
   li1.remove(del);
   number--;
  }
  else if(e.getSource()==bt2){
   List t_li =new List();
   ArrayList t_li1=new ArrayList();
   for(int i=0;i<number;++i){
   
    t_li.add(i+1+li.getItem(i));
    t_li1.add(li1.get(i));
   }
   li.removeAll(); //리스트 항목 모두 삭제
   li1.clear(); //어레이 리스트 항목 모두 삭제
   
   for(int i=0;i<number;++i){
    li.add(t_li.getItem(i));
    li1.add(t_li1.get(i));
   }
   System.out.println(number);
  }
 }
 @Override
 public void mouseClicked(MouseEvent e) {
  // TODO Auto-generated method stub
  if(e.getSource()==bt3){
   if(bt3.getLabel()=="수정"){
    bt3.setLabel("수정중");

    ta1.setBackground(new Color(255,255,255));
    ta1.setEditable(true);
   }
   else if(bt3.getLabel()=="수정중"){
    bt3.setLabel("수정"); 

    ta1.setBackground(new Color(144,122,44));
    int index=li.getSelectedIndex();
    System.out.println(index);
    li1.set(index,ta1.getText());
    ta1.setEditable(false);
   }
  }
  if(e.getSource()==li){
   int index=li.getSelectedIndex();
   String str =(String)li1.get(index);
   ta1.setText(str);
  }
 }
 @Override
 public void mousePressed(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mouseReleased(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mouseEntered(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mouseExited(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 
}


public class Ex_01 {
 public static void main(String [] ar){
  F ex=new F();
 }
}


 

'JAVA > 소스' 카테고리의 다른 글

열혈강의 자바 24장 과제 1 [미완성]  (2) 2012.02.03
열혈강의 자바 22장 과제 2  (3) 2012.01.11
자바 21장 과제 2  (0) 2011.12.15
자바 문자열 숫자로 바꾸는 방법  (0) 2011.12.12
열혈강의 자바 14장 과제1  (0) 2011.10.30