Đang tải... (xem toàn văn)
Đặc biệt, bắt đầu phiên bản JDK 1.3 thì bạn có thể sử dụng cả văn bản đơn giản và cả HTML text trong button, label, menu items. Nhưng đối với button thì rất cản trở, còn với Label thì nó rất thuận tiện: JTextField(int cols): khởi tạo JtextField với số cột quy định. JTextField(String text, int cols): khởi tạo Jtextfield với dòng text và số cột quy định.
Trường ĐH Công Nghệ Thông Tin Đại Học Quốc Gia Hồ Chí Minh Thuyết trình giữa kỳ môn Java Nhóm thực hiện: Phạm Hải Triều 08520422 Nguyễn Hoàng Đức 08520099 Giảng Viên: Ths.Huỳnh Ngọc Tín Text INPUT 8/20/141 API: javax.swing.JTextField Constructor: JTextField(int cols): khởi tạo JtextField với số cột quy định. JTextField(String text, int cols): khởi tạo Jtextfield với dòng text và số cột quy định. Thông thường, ta sẽ tạo 1 constructor như sau: JTextField textField = new JTextField(20); Và sau đó setText(): textField.setText("Hello!"); 1.JtextField 8/20/142 Methods: -void setText(String t) -String getText() -void setEditable(boolean b): chỉnh sửa nội dung. -setColumns() -Chú ý: Sau khi dùng setColums, ta gọi lại hàm revalidate để hợp lệ lại component trong 1 container. textField.setColumns(10); panel.revalidate(); 1.JtextField(tt) 8/20/143 Để bỏ đi khoảng trắng đầu và cuối chuỗi, bạn có thể sử dụng hàm strim() String text = textField.getText().trim(); Để chọn Font chữ cho textField thì sử dụng hàm setFont. textField.setFont(new Font("Arial", Font.BOLD, 12)); 2.Label and labeling 8/20/144 API: javax.swing.JLabel JLabel(String text) JLabel(Icon icon) JLabel(String text, int align) JLabel(String text, Icon icon, int align) Align : SwingConstants (LEFT, CENTER, or RIGHT) 2.Label and labeling(tt) 8/20/145 - Đặc biệt, bắt đầu phiên bản JDK 1.3 thì bạn có thể sử dụng cả văn bản đơn giản và cả HTML text trong button, label, menu items. - Nhưng đối với button thì rất cản trở, còn với Label thì nó rất thuận tiện: label = new JLabel("<html><b>Required</b>entry:</html>" ); 2.Label and labeling(tt) 8/20/146 Methods: setText(String text): quy định chuỗi văn bản. setIcon(Icon icon): quy định Icon. getLength(): đưa ra chiều dài của chuỗi text. 2.Label and labeling(tt) 8/20/147 - Để theo dõi sự thay đổi của textField : textField.getDocument().addDocumentListener(listene r); - Khi text thay đổi thì 1 trong các phương thức DocumentListener sẽ được gọi: void insertUpdate(DocumentEvent event) void removeUpdate(DocumentEvent event) void changedUpdate(DocumentEvent event) 3.Change tracking in textfieLd 8/20/148 API:javax.swing.JPasswordField Contructor: JPasswordField(String text, int columns) Methods: void setEchoChar(char echo) char[] getPassword() 4.PassWord Field 8/20/149 Để chứa các trường văn bản. Có diện tích lớn hơn, chứa được nhiều dòng. Contructor: textArea = new JTextArea(8, 40); // 8 lines of 40 columns each 5.TextField Areas 8/20/1410 [...]...5.TextField Areas(tt) Methods: textArea.setLineWrap(true); // long lines are wrapped - Kết hợp với thanh cuộn: textArea = new JTextArea(8, 40); JScrollPane scrollPane = new JScrollPane(textArea); Các Methods khác: void setColumns(int cols) void setRows(int rows) void append(String newText) //thêm vào text sau chuỗi text có sẵn void setLineWrap(boolean wrap)//ngắt... hợp lệ” và Focus lại TextInput đó nếu người dùng Focus vào 1 component khác Và yêu cầu người dùng nhập lại Một verifiers được extends từ InputVerifiers Phương thức isEditValid của JFormaterTextField được trả về True nếu định dạng đúng class FormattedTextFieldVerifier extends InputVerifier { public boolean verify(JComponent component) { JFormattedTextField field = (JFormattedTextField) component;... field.isEditValid(); } } - Gắn vào TextInput : intField.setInputVerifier(new FormattedTextFieldVerifier()); 6.Format Input( tt) Others Standard Formatters NumberFormat có 1 số static methods: getNumberInstance: định dạng số getCurrencyInstance: định dạng tiền tệ getPercentInstance: định dạng % JFormattedTextField currencyField = new JFormattedTextField(NumberFormat getCurrencyInstance()); 6.Format Input( tt) Date: Static... text có sẵn void setLineWrap(boolean wrap)//ngắt dòng - Set tab: void setTabSize(int c) 6.Format Input Integer Input JFormattedTextField intField = new JFormattedTextField(NumberFormat.getIntegerInst ance()); - Bạn có thể đặt default value với setValue method intField.setValue(new Integer(100)); 6.Format Input( tt) Filter: - Cho phép người dùng nhập 1 loại ký tự nhất định - Bạn cần phải cài đặt tài... để định dạng JformatedTextField Các văn bản số nguyên sử dụng InternationalFormatter được khởi tạo với NumberFormat.getIntegerInstance() JFormattedTextField intField = new JFormattedTextField(new InternationalFormatter (NumberFormat.getIntegerInstance()) { protected DocumentFilter getDocumentFilter() { return filter; } private DocumentFilter filter = new IntFilter(); }); 6.Format Input( tt) Verifiers... JFormattedTextField dateField = new JFormattedTextField(DateFormat.getDateInstance()); Chuyển từ định dạng medium sang sort DateFormat.getDateInstance(DateFormat.SHORT) Feb 24, 2002 2/24/02 6.Format Input( tt) MaskFormatter: Định dạng những dạng nhập dữ liệu bằng các ký tự được quy định: Vd: Như muốn định dạng kiểu số (192-168-1-30) new MaskFormatter(###-###-#-##) MaskFormatter Symbols 6.Format Input( tt) . Hồ Chí Minh Thuyết trình giữa kỳ môn Java Nhóm thực hiện: Phạm Hải Triều 08520422 Nguyễn Hoàng Đức 08520099 Giảng Viên: Ths.Huỳnh Ngọc Tín Text INPUT 8/20/141 API: javax.swing.JTextField Constructor: JTextField(int. như sau: JTextField textField = new JTextField(20); Và sau đó setText(): textField.setText("Hello!"); 1.JtextField 8/20/142 Methods: -void setText(String t) -String getText() -void. JFormattedTextField field = (JFormattedTextField) component; return field.isEditValid(); } } - Gắn vào TextInput : intField.setInputVerifier(new FormattedTextFieldVerifier()); 6.Format Input( tt) 8/20/1414