java映像介面開發簡單一實例-JTextArea、JScrollPane、JPanel、JButton應用執行個體

來源:互聯網
上載者:User

java映像介面開發簡單一實例

JTextArea、JScrollPane、JPanel、JButton應用執行個體,通過‘插入文本’按鈕功能,寫入測試文本,換行功能可以進行換行和不換行,代碼如下:  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;


/**
 * JTextArea、JScrollPane、JPanel、JButton應用執行個體
 *  @author  左傑  jdk 5.0
  */
public   class  Example8Frame  extends  JFrame {

     private   static   final   long  serialVersionUID  =   1L ;
     private  JTextArea textArea; // 文本域
     private  JScrollPane scrollPane; // 帶捲軸面板
     private  JPanel buttonPanel; // 存放按鈕面板
     private  JButton wrapButton; // 換行按鈕
     public  Example8Frame() {
        setTitle( " 文本編輯 " ); // 設定表單標題
        setSize( 300 ,  300 ); // 設定表單大小
         // 建立JTextArea組件
        textArea  =   new  JTextArea();
         // 建立JScrollPane面板,將JTextArea組件放入帶捲軸面板中
        scrollPane  =   new  JScrollPane(textArea);
         //// 在表單中添加帶捲軸面板
        add(scrollPane, BorderLayout.CENTER);
        
        buttonPanel  =   new  JPanel(); // 建立按鈕面板
         // 建立“插入文本”按鈕,並添加點擊事件,添加測試文本
        JButton insertButton  =   new  JButton( " 插入文本 " ); // 建立“插入文本”按鈕
        buttonPanel.add(insertButton); // 在面板中添加該按鈕
         // 為該按鈕添加事件監聽
        insertButton.addActionListener( new  ActionListener() {
             public   void  actionPerformed(ActionEvent event) {
                textArea.append( " 這是一個textArea組件的簡單應用執行個體,這為測試文本。 " ); // JTextArea組件添加文本資訊
            }
        });
         // 添加“換行”按鈕,並添加點擊事件,來控制換行和不換行
        wrapButton  =   new  JButton( " 換行 " ); // 建立“換行文本”按鈕
        buttonPanel.add(wrapButton); // 在面板中添加該按鈕
         // 為該按鈕添加事件監聽
        wrapButton.addActionListener( new  ActionListener() {
             public   void  actionPerformed(ActionEvent event) {
                 boolean  wrap  =   ! textArea.getLineWrap(); // 擷取JTextArea組件的是否換行狀態,預設為false,取反
                textArea.setLineWrap(wrap); // 重新設定是否換行屬性
                wrapButton.setText(wrap  ?   " 不換行 "  :  " 換行 " ); // 根據屬性設定按鈕顯示文本
            }
        });
         // 在表單中添加按鈕面板
        add(buttonPanel, BorderLayout.SOUTH);
    }
     public   static   void  main(String[] args) {
        Example8Frame frame  =   new  Example8Frame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible( true );
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.