TOP →
Java →
Swing →
JFormattedTextField → This Page
JFormattedTextField@Swing サンプル04
概要
Java -
Swing -
JFormattedTextField のサンプルです。
・日時系[SimpleDateFormat]
解説
入力した日付文字列を整形して表示します。
DateFormat と似ていますが、こちらのほうが細かい設定をすることができます。
パターン
フォーマットには以下のパターンを組み合わせて設定します。
パターン | 説明 |
G |
紀元 |
y |
年 |
M |
月 |
w |
年における週 |
W |
月における週 |
d |
月における日 |
D |
年における日 |
F |
月における曜日 |
E |
曜日 |
a |
AM/PM |
H |
1日における時(0~23) |
k |
1日における時(1~24) |
K |
AM/PMにおける時(0~11) |
h |
AM/PMにおける時(1~12) |
m |
分 |
s |
秒 |
S |
ミリ秒 |
z |
一般タイムゾーン |
Z |
RFC 822 タイムゾーン |
月を表示する際、M を指定すると8月は 8 と表示されますが、
MM を指定すると 08 と表示されます。
(d なども同様です)
なので、
2007/1/2 としたい場合は yyyy/M/d
2007/01/02 としたい場合は yyyy/MM/dd
です。
サンプルイメージ
サンプルソース
import java.awt.Dimension;
import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* JFormattedTextField サンプル04
* ・日時系[SimpleDateFormat]
*
* @author みっちー
*/
public class JFormattedTextField04 extends JFrame {
private static final long serialVersionUID = 1L;
/**
* 開始メソッド
*
* @param args パラメータ
*/
public static void main(String[] args) {
JFormattedTextField04 frame = new JFormattedTextField04();
// 閉じるボタンをクリックされた場合の動作を設定
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ウインドウのタイトルを設定
frame.setTitle("JFormattedTextField サンプル04");
// フレームの X座標、Y座標、幅、高さを設定
frame.setBounds(100, 200, 250, 170);
// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
frame.setVisible(true);
}
/**
* コンストラクタ
*/
public JFormattedTextField04() {
// パネルを作成
JPanel panelBase = new JPanel();
// フォーマットを設定
SimpleDateFormat sdf1 = new SimpleDateFormat("yy/MM/dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSSSS");
SimpleDateFormat sdf4 = new SimpleDateFormat("ahh:mm:ss");
SimpleDateFormat sdf5 = new SimpleDateFormat("yyyy年MM月dd日(E)");
// テキストを作成
JFormattedTextField text1 = new JFormattedTextField(sdf1);
JFormattedTextField text2 = new JFormattedTextField(sdf2);
JFormattedTextField text3 = new JFormattedTextField(sdf3);
JFormattedTextField text4 = new JFormattedTextField(sdf4);
JFormattedTextField text5 = new JFormattedTextField(sdf5);
// 初期値
Date dt = new Date();
// 初期値を設定
// setText() だと初期表示時にフォーマットが評価されないので setValue() を使う
text1.setValue(dt);
text2.setValue(dt);
text3.setValue(dt);
text4.setValue(dt);
text5.setValue(dt);
// サイズを設定
text1.setPreferredSize(new Dimension(220, 20));
text2.setPreferredSize(new Dimension(220, 20));
text3.setPreferredSize(new Dimension(220, 20));
text4.setPreferredSize(new Dimension(220, 20));
text5.setPreferredSize(new Dimension(220, 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));
// テキストを追加
panelBase.add(text1);
panelBase.add(text2);
panelBase.add(text3);
panelBase.add(text4);
panelBase.add(text5);
// パネルを追加
getContentPane().add(panelBase);
}
}
サンプルソースのダウンロード
ソースのダウンロード(Eclipse用のプロジェクトファイルも同梱)
更新履歴
2016/05/13 Windows 8.1 + Java 7 環境で全体を見直し
2007/12/27 新規作成
TOP →
Java →
Swing →
JFormattedTextField → This Page