TOP →
Java →
Swing →
JTextArea → This Page
JTextArea@Swing サンプル04
概要
Java -
Swing -
JTextArea のサンプルです。
・余白を設定
・サイズを設定
・編集不可
・無効化
・タブサイズを設定
解説
余白設定をしていても、枠を設定すると枠の余白設定が優先されるため思ったとおりになりません。
テキストエリア1と2を見比べて下さい。
テキスト6はタブのサイズを大きくしてあります。
通常、ウインドウズだとテキストエリアでタブキーを押すと
半角8文字分のタブスペースが挿入されますが、サンプルでは半角12文字分にしてあります。
サンプル画像2がテキストエリア6でタブキーを一回押した直後の画像です。
サンプルイメージ
サンプルソース
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;
/**
* JTextArea サンプル04
* ・余白を設定
* ・サイズを設定
* ・編集不可
* ・無効化
* ・タブサイズを設定
*
* @author みっちー
*/
public class JTextArea04 extends JFrame {
private static final long serialVersionUID = 1L;
/**
* 開始メソッド
*
* @param args パラメータ
*/
public static void main(String[] args) {
JTextArea04 frame = new JTextArea04();
// 閉じるボタンをクリックされた場合の動作を設定
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ウインドウのタイトルを設定
frame.setTitle("JTextArea サンプル04");
// フレームの X座標、Y座標、幅、高さを設定
frame.setBounds(100, 200, 500, 300);
// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
frame.setVisible(true);
}
/**
* コンストラクタ
*/
public JTextArea04() {
// パネルを作成
JPanel panelBase = new JPanel();
// テキストを作成
JTextArea text1 = new JTextArea("テキスト1", 5, 20);
JTextArea text2 = new JTextArea("テキスト2", 5, 20);
JTextArea text3 = new JTextArea("テキスト3");
JTextArea text4 = new JTextArea("テキスト4", 5, 10);
JTextArea text5 = new JTextArea("テキスト5", 5, 10);
JTextArea text6 = new JTextArea("テキスト6", 3, 30);
// 余白を設定
text1.setMargin(new Insets(10, 10, 10, 10));
text2.setMargin(new Insets(10, 10, 10, 10));
// サイズを設定
text3.setPreferredSize(new Dimension(100, 50));
// text1 だけ枠線設定
text1.setBorder(new BevelBorder(BevelBorder.RAISED));
// 編集不可
text4.setEditable(false);
// 無効化
text5.setEnabled(false);
// タブサイズを設定
text6.setTabSize(12);
// 分かりやすいように背景色を設定
text1.setBackground(Color.LIGHT_GRAY);
text2.setBackground(Color.LIGHT_GRAY);
text3.setBackground(Color.LIGHT_GRAY);
text4.setBackground(Color.LIGHT_GRAY);
text5.setBackground(Color.LIGHT_GRAY);
text6.setBackground(Color.LIGHT_GRAY);
// テキストを追加
panelBase.add(text1);
panelBase.add(text2);
panelBase.add(text3);
panelBase.add(text4);
panelBase.add(text5);
panelBase.add(text6);
// パネルを追加
getContentPane().add(panelBase);
}
}
サンプルソースのダウンロード
ソースのダウンロード(Eclipse用のプロジェクトファイルも同梱)
更新履歴
2016/05/13 Windows 8.1 + Java 7 環境で全体を見直し
2008/01/17 新規作成
TOP →
Java →
Swing →
JTextArea → This Page