タイトル
TOPJavaSwingJTextArea → This Page

JTextArea@Swing サンプル03

概要

Java - Swing - JTextArea のサンプルです。
・折り返し有無を設定

解説

テキストエリア1は折り返しあり、
テキストエリア2は折り返しあり&単語単位で折り返し、
テキストエリア3は折り返しなしにしています。
折り返しなしの設定だと、改行が入るまでテキストエリアの幅が延びてしまいます。

サンプルイメージ

サンプル画像


サンプルソース

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;

/**
 * JTextArea サンプル03
 * ・折り返し有無を設定
 * 
 * @author みっちー
 */
public class JTextArea03 extends JFrame {
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * 開始メソッド
	 * 
	 * @param args	パラメータ
	 */
	public static void main(String[] args) {
		JTextArea03 frame = new JTextArea03();
		
		// 閉じるボタンをクリックされた場合の動作を設定
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// ウインドウのタイトルを設定
		frame.setTitle("JTextArea サンプル03");
		
		// フレームの X座標、Y座標、幅、高さを設定
		frame.setBounds(100, 200, 450, 150);
		
		// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
		frame.setVisible(true);
	}
	
	/**
	 * コンストラクタ
	 */
	public JTextArea03() {
		// パネルを作成
		JPanel panelBase = new JPanel();
		
		// テキストを作成
		JTextArea text1 = new JTextArea("テキスト1 1234567890ABCDE", 5, 10);
		JTextArea text2 = new JTextArea("テキスト2 1234567890ABCDE", 5, 10);
		JTextArea text3 = new JTextArea("テキスト3 1234567890ABCDE", 5, 10);
		
		// 折り返し有無を設定
		text1.setLineWrap(true);
		text2.setLineWrap(true);
		text3.setLineWrap(false);
		
		// 単語単位で折り返し
		text2.setWrapStyleWord(true);
		
		// 分かりやすいように枠線設定
		text1.setBorder(new BevelBorder(BevelBorder.RAISED));
		text2.setBorder(new BevelBorder(BevelBorder.RAISED));
		text3.setBorder(new BevelBorder(BevelBorder.RAISED));
		
		// テキストを追加
		panelBase.add(text1);
		panelBase.add(text2);
		panelBase.add(text3);
		
		// パネルを追加
		getContentPane().add(panelBase);
	}
}

サンプルソースのダウンロード

ソースのダウンロード(Eclipse用のプロジェクトファイルも同梱)

更新履歴

2016/05/13 Windows 8.1 + Java 7 環境で全体を見直し
2008/01/17 新規作成


TOPJavaSwingJTextArea → This Page