JAVA JTextComponent

스윙에서 텍스트와 관련된 컴포넌트들은 JTextComponent를 상속받는다.
텍스트 관련 클래스들은 용도에 따라 아래와 같이 세가지로 분류 된다.

  • 텍스트 컨트롤 : 한줄을 입력할 수 있는 텍스트클래스
    •  JTextField :한줄의 텍스트를 다룬다.
    •  JPasswardField : 패스워드 형태의 텍스트를 다룬다. 
    •  JFomattedTextFeild : 규격화된 형태의 텍스트를 다룬다.
  • 단순 텍스트: 여러줄을 입력할 수 있지만, 한가지의 폰트로 동일한 스타일의 문자열만 사용
    • JTextArea
  • 스타일: 하나이상의 폰트를 이용해서 화면에 보여줄 수 있다.
    • JEditorPane
    • JTextPane 

간단한 Demo
SwingText.java
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. import javax.swing.*;
  5. import java.text.*;
  6. import javax.swing.text.*;
  7.  
  8. public class SwingText extends JFrame implements ActionListener {
  9.     protected JTextField field;
  10.     protected JPasswordField passwd;
  11.     protected JFormattedTextField phone;
  12.     protected JTextArea area;
  13.    
  14.     public SwingText() throws ParseException{
  15.         super("Swing Text");
  16.         getContentPane().setLayout(new BorderLayout(10,10));
  17.        
  18.         area = new JTextArea();
  19.         field = new JTextField(10);
  20.         field.addActionListener((ActionListener) this);
  21.         passwd = new JPasswordField(10);
  22.         passwd.addActionListener((ActionListener) this);
  23.        
  24.         MaskFormatter mf = new MaskFormatter("(0##) ###-####");
  25.         mf.setPlaceholderCharacter('_');
  26.         phone = new JFormattedTextField(mf);
  27.         phone.addActionListener((ActionListener) this);
  28.        
  29.         JPanel bottom = new JPanel(new GridLayout(3,3,10,2));
  30.         bottom.add(new JLabel("Login"));
  31.         bottom.add(field);
  32.         bottom.add(new JLabel("Password"));
  33.         bottom.add(passwd);
  34.         bottom.add(new JLabel("Phone Num."));
  35.         bottom.add(phone);
  36.        
  37.         getContentPane().add("Center",new JScrollPane(area));
  38.         getContentPane().add("South",bottom);
  39.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40.         setSize(300,200);
  41.         setVisible(true);
  42.     }
  43.     public void actionPerformed(ActionEvent e){
  44.         Object o =e.getSource();
  45.         if(o == field ||o==passwd||o==phone){
  46.             System.out.println("ID-"+field.getText());
  47.             char[] data = passwd.getPassword();
  48.             System.out.println("Password-"+new String(data));
  49.             System.out.println("Phone-"+phone.getValue());
  50.            
  51.         }
  52.     }
  53.     public static void main(String argsp[]){
  54.         try{
  55.             SwingText st =new SwingText();
  56.         }catch(Exception e){
  57.             System.out.println(e);
  58.         }
  59.     }
  60. }


댓글

이 블로그의 인기 게시물

[Win32 API] WINAPI - 함수호출규약

JAVA Frame Icon setting

JAVA Spinner