홍익대 Java/수업
텍스트 필드와 버튼
Yi Junho
2009. 7. 29. 15:15
반응형
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); } } } }
반응형