返回列表 發帖

文字編輯器 (一)

本帖最後由 tonyh 於 2020-2-1 15:05 編輯

運用 JTextArea 搭配 JScrollPane 作出如下圖之文字窗格,JTextArea 允許使用者輸入多行文字,有別於之前運用過的 JTextField (只能輸入一列),而 JScrollPane 則能使內容超過視窗範圍時,自動出現「滾動拉條」。另外,若要讓文字內容在超出視窗寬度時能自動換行,則將 JTextArea 物件設定 setLineWrap(true)。





  1. import java.awt.Font;
  2. import javax.swing.JFrame;
  3. import javax.swing.JScrollPane;
  4. import javax.swing.JTextArea;

  5. public class Ch139 {
  6.        
  7.         JFrame fm;
  8.         JScrollPane sp;
  9.         JTextArea ta;
  10.        
  11.         Ch139()
  12.         {
  13.                 ta=new JTextArea();
  14.                 ta.setFont(new Font("新細明體",Font.PLAIN,18));
  15.                 ta.setLineWrap(true);
  16.                
  17.                 sp=new JScrollPane(ta);

  18.                 fm=new JFrame("My Editor");
  19.                 fm.setBounds(100, 100, 500, 350);
  20.                 fm.setVisible(true);
  21.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.                 fm.add(sp);
  23.         }

  24.         public static void main(String[] args) {
  25.                 new Ch139();
  26.         }

  27. }
複製代碼

  1. import java.awt.Font;
  2. import javax.swing.JFrame;
  3. import javax.swing.JScrollPane;
  4. import javax.swing.JTextArea;

  5. public class Ch139 {
  6.         
  7.         JFrame fm;
  8.         JScrollPane sp;
  9.         JTextArea ta;
  10.         
  11.         Ch139()
  12.         {
  13.             ta=new JTextArea();
  14.             ta.setFont(new Font("新細明體",Font.PLAIN,18));
  15.             ta.setLineWrap(true);
  16.                
  17.             sp=new JScrollPane(ta);

  18.             fm=new JFrame("My Editor");
  19.                         fm.setBounds(100, 100, 500, 350);
  20.                 fm.setVisible(true);
  21.                         fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.                         fm.add(sp);
  23.         }

  24.         public static void main(String[] args) {
  25.                 new Ch139();
  26.         }

  27. }
複製代碼

TOP

  1. import java.awt.Font;
  2. import javax.swing.JFrame;
  3. import javax.swing.JScrollPane;
  4. import javax.swing.JTextArea;

  5. public class Ch000 {
  6.         
  7.         JFrame fm;
  8.         JScrollPane sp;
  9.         JTextArea ta;
  10.         
  11.         Ch000()
  12.         {
  13.                 ta=new JTextArea();
  14.                 ta.setFont(new Font("新細明體",Font.PLAIN,18));
  15.                 ta.setLineWrap(true);
  16.                
  17.                 sp=new JScrollPane(ta);

  18.                 fm=new JFrame("My Editor");
  19.                 fm.setBounds(100, 100, 500, 350);
  20.                 fm.setVisible(true);
  21.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.                 fm.add(sp);
  23.         }

  24.         public static void main(String[] args) {
  25.                 new Ch000();
  26.         }

  27. }
複製代碼
EEEEEEEEEEEEELLLLLLLLLLLLLIIIIIIIIIIIII

TOP

  1. import java.awt.Font;
  2. import javax.swing.JFrame;
  3. import javax.swing.JScrollPane;
  4. import javax.swing.JTextArea;

  5. public class Ch101 {
  6.         
  7.         JFrame fm;
  8.         JScrollPane sp;
  9.         JTextArea ta;
  10.         
  11.         Ch101()
  12.         {
  13.                 ta=new JTextArea();
  14.                 ta.setFont(new Font("新細明體",Font.PLAIN,18));
  15.                 ta.setLineWrap(true);
  16.                
  17.                 sp=new JScrollPane(ta);

  18.                 fm=new JFrame("My Editor");
  19.                 fm.setBounds(100, 100, 500, 350);
  20.                 fm.setVisible(true);
  21.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.                 fm.add(sp);
  23.         }

  24.         public static void main(String[] args) {
  25.                 new Ch101();
  26.         }

  27. }
複製代碼

TOP

  1. import java.awt.Font;
  2. import javax.swing.JFrame;
  3. import javax.swing.JScrollPane;
  4. import javax.swing.JTextArea;

  5. public class Ch139 {
  6.         
  7.         JFrame fm;
  8.         JScrollPane sp;
  9.         JTextArea ta;
  10.         
  11.         Ch139()
  12.         {
  13.                 ta=new JTextArea();
  14.                 ta.setFont(new Font("新細明體",Font.PLAIN,18));
  15.                 ta.setLineWrap(true);
  16.                
  17.                 sp=new JScrollPane(ta);

  18.                 fm=new JFrame("My Editor");
  19.                 fm.setBounds(100, 100, 500, 350);
  20.                 fm.setVisible(true);
  21.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.                 fm.add(sp);
  23.         }

  24.         public static void main(String[] args) {
  25.                 new Ch139();
  26.         }

  27. }
複製代碼

TOP

返回列表