タイトル
 メニューにないコーナーはTopからいけます
TOPJavaSwingJFormattedTextField → This Page

JFormattedTextField@Swing サンプル02

概要

Java - Swing - JFormattedTextField のサンプルです。
・数値系[DecimalFormat]

解説

入力した数値を整形して表示します。
NumberFormat と似ていますが、こちらのほうが細かい設定をすることができます。

パターン

フォーマットには以下のパターンを組み合わせて設定します。
パターン説明
0 数字
# 数字(ゼロの場合は非表示)
. 数値桁区切り、または通貨桁区切り
- マイナス記号
, グループ区切り
E 仮数と指数の区切り
; 正と負のサブパターン
% 100倍してパーセント
\u2030 1000倍してパーミル値
\u00A4 通貨符号


サンプルイメージ

サンプル画像


サンプルソース

import java.awt.Dimension;
import java.awt.Font;
import java.text.DecimalFormat;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * JFormattedTextField サンプル02
 * ・数値系[DecimalFormat]
 * 
 * @author みっちー
 */
public class JFormattedTextField02 extends JFrame {
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * 開始メソッド
	 * 
	 * @param args	パラメータ
	 */
	public static void main(String[] args) {
		JFormattedTextField02 frame = new JFormattedTextField02();
		
		// 閉じるボタンをクリックされた場合の動作を設定
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// ウインドウのタイトルを設定
		frame.setTitle("JFormattedTextField サンプル02");
		
		// フレームの X座標、Y座標、幅、高さを設定
		frame.setBounds(100, 200, 250, 220);
		
		// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
		frame.setVisible(true);
	}
	
	/**
	 * コンストラクタ
	 */
	public JFormattedTextField02() {
		// パネルを作成
		JPanel panelBase = new JPanel();
		
		// 初期値
		double d = 123456789.12345d;
		
		// テキストを作成
		JFormattedTextField text1 = new JFormattedTextField(new DecimalFormat("##########"));
		JFormattedTextField text2 = new JFormattedTextField(new DecimalFormat("#,###,###,###"));
		JFormattedTextField text3 = new JFormattedTextField(new DecimalFormat("0,000,000,000"));
		JFormattedTextField text4 = new JFormattedTextField(new DecimalFormat("#,###,###,###.000000"));
		JFormattedTextField text5 = new JFormattedTextField(new DecimalFormat("#,###,###,###.######"));
		JFormattedTextField text6 = new JFormattedTextField(new DecimalFormat("\u00A4#,###,###,###.######"));
		JFormattedTextField text7 = new JFormattedTextField(new DecimalFormat("##0.00%"));
		
		// 初期値を設定
		// setText() だと初期表示時にフォーマットが評価されないので setValue() を使う
		text1.setValue(Double.valueOf(d));
		text2.setValue(Double.valueOf(d));
		text3.setValue(Double.valueOf(d));
		text4.setValue(Double.valueOf(d));
		text5.setValue(Double.valueOf(d));
		text6.setValue(Double.valueOf(123456789d));
		text7.setValue(Double.valueOf(0.12345d));
		
		// サイズを設定
		text1.setPreferredSize(new Dimension(180, 20));
		text2.setPreferredSize(new Dimension(180, 20));
		text3.setPreferredSize(new Dimension(180, 20));
		text4.setPreferredSize(new Dimension(180, 20));
		text5.setPreferredSize(new Dimension(180, 20));
		text6.setPreferredSize(new Dimension(180, 20));
		text7.setPreferredSize(new Dimension(180, 20));
		
		// フォントの設定
		text1.setFont(new Font("MS ゴシック", Font.PLAIN, 16));
		text2.setFont(new Font("MS ゴシック", Font.PLAIN, 16));
		text3.setFont(new Font("MS ゴシック", Font.PLAIN, 16));
		text4.setFont(new Font("MS ゴシック", Font.PLAIN, 16));
		text5.setFont(new Font("MS ゴシック", Font.PLAIN, 16));
		text6.setFont(new Font("MS ゴシック", Font.PLAIN, 16));
		text7.setFont(new Font("MS ゴシック", Font.PLAIN, 16));
		
		// テキストを追加
		panelBase.add(text1);
		panelBase.add(text2);
		panelBase.add(text3);
		panelBase.add(text4);
		panelBase.add(text5);
		panelBase.add(text6);
		panelBase.add(text7);
		
		// パネルを追加
		getContentPane().add(panelBase);
	}
}

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

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

更新履歴

2016/05/13 Windows 8.1 + Java 7 環境で全体を見直し
2007/12/27 新規作成


TOPJavaSwingJFormattedTextField → This Page
Valid CSS!