방치하기

텍스트 필드와 버튼 본문

홍익대 Java/수업

텍스트 필드와 버튼

Yi Junho 2009. 7. 29. 15:15
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class Add extends JFrame {
   private JButton addButton, printButton;
   Panel pan01,pan02;
   JTextField text01,text02;
   StringBuffer str=new StringBuffer();
   public Add()
   {
        super( "이름추가" );
     
        text01 = new JTextField( 5 );
        text02 = new JTextField( 10 );
        text02.setEditable(false);
        Container container = getContentPane();
        container.setLayout(new GridLayout(2,1));
 
        addButton = new JButton( "추가" );
        printButton = new JButton( "출력" );
         
        pan01=new Panel();
        pan02=new Panel();
        container.add( pan01 );
        container.add( pan02 );
         
        pan01.setLayout(new BorderLayout());
        pan01.add(BorderLayout.WEST,text01);
 
        pan01.add(BorderLayout.EAST,addButton);
 
 
 
        pan01.setLayout(new GridLayout(1,2));
        pan02.setLayout(new BorderLayout());
 
        pan02.add(BorderLayout.NORTH,text02);
        pan02.add(BorderLayout.SOUTH,printButton);
         
         
       
         
         
        ButtonHandler handler = new ButtonHandler();//액션이 발생하면 아래 내부 클래스로 넘김
        addButton.addActionListener( handler );
        printButton.addActionListener( handler );
        setSize( 275, 150 );
        setVisible( true );
   }
   public static void main( String args[] )
   {
      Add application = new Add();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }
   private class ButtonHandler implements ActionListener {
      public void actionPerformed( ActionEvent event )
      {
         if("추가".equals(event.getActionCommand()))
         {
            JOptionPane.showMessageDialog(Add.this,
            "You pressed: " + text01.getText()+"추가합니다" );
            str.append(text01.getText()+" ");
            System.out.println(text01.getText()+"추가합니다");
            text01.setText("");
         }
         else
          {
            String str02= str.substring(0);
            System.out.println(str02);
            text02.setText(str02);
          }
      }
   }
}
반응형
Comments