[JavaSwing] JTextArea Trong Java - Hãy Sống Theo Cách Của Bạn

Bỏ qua nội dung
Tìm kiếm cho: Tìm
Date: 13/07/2014Author: nguyenvanquan7826 0 Bình luận

JTextArea là một thành phần cho phép hiển thị nhiều dòng văn bản đồng thời người dùng có thể chỉnh sửa văn bản.

JTextArea is a component allowing displaying multiple line and the user can edit the text.

Tạo một JTextArea đơn giản

Bây giờ chúng ta sẽ thực hành ngay một ví dụ cho phép bạn gõ và chỉnh sửa văn bản. Trong code và hình ảnh mình đã giải thích khá rõ ràng các lệnh thực hiện với JTextArea.

Create a simple JTextArea

Now we will write an example allowed typing and editing text. In the code and image, I have clearly explained.

create JTextArea  in Java
package nguyenvanquan7826.JTextArea; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; /** * ----------------- @author nguyenvanquan7826 ----------------- * ---------------nguyenvanquan7826.wordpress.com -------------- */ public class DemoJTextArea extends JFrame { private JTextArea ta; public DemoJTextArea() { add(createMainPanel()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Demo JTextArea"); pack(); setLocationRelativeTo(null); setVisible(true); } private JPanel createMainPanel() { JPanel panel = new JPanel(new BorderLayout()); // JScrollPane create a scroll when row of text larger than row of // JTextArea JScrollPane scroll = new JScrollPane(ta = createTextArea(10, 40)); panel.add(scroll, BorderLayout.CENTER); JButton btnClear = new JButton("Clear"); btnClear.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { clear(); } }); JPanel panelBottom = new JPanel(); panelBottom.add(btnClear); panel.add(panelBottom, BorderLayout.PAGE_END); return panel; } /** * create a JTextArea with rows and columns, two method setWrapStyleWord and * setLineWrap make text can down line when text too long */ private JTextArea createTextArea(int row, int col) { JTextArea ta = new JTextArea(row, col); ta.setWrapStyleWord(true); ta.setLineWrap(true); return ta; } /** * clear text of JTextArea */ private void clear() { ta.setText(""); } public static void main(String[] args) { new DemoJTextArea(); } }

Đọc thêm (read more): TUT Java swing, class JTextArea, use JTextArea

Chia sẻ bài viết:

  • Facebook
  • X
  • LinkedIn
  • Email
  • Pinterest
  • Tumblr
  • Túi
  • Reddit
  • In
Thích Đang tải...

Điều hướng bài viết

Bài viết trước: [Java – C] Gọi hàm C trong Java – Call C function in Java Bài đăng tiếp theo: [Tricks] Set featured image from image in article

Bình luận về bài viết này Hủy trả lời

Δ

  • Bình luận
  • Đăng lại
  • Theo dõi Đã theo dõi
    • Hãy sống theo cách của bạn
    • Đã có 45 người theo dõi Theo dõi ngay
    • Đã có tài khoản WordPress.com? Đăng nhập.
    • Hãy sống theo cách của bạn
    • Theo dõi Đã theo dõi
    • Đăng ký
    • Đăng nhập
    • URL rút gọn
    • Báo cáo nội dung
    • Xem toàn bộ bài viết
    • Quản lý theo dõi
    • Ẩn menu
%d

Từ khóa » Cách Dùng Jtextarea Trong Java