TOP →
Java →
Swing →
JTextField → This Page
JTextField@Swing サンプル06
概要
Java -
Swing -
JTextField のサンプルです。
・切り取り
・コピー
・貼り付け
解説
テキスト編集における基本「切り取り」「コピー」「貼り付け」です。
サンプルイメージ
サンプルソース
import java.awt.Font;
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.JTextField;
/**
* JTextField サンプル06
* ・切り取り
* ・コピー
* ・貼り付け
*
* @author みっちー
*/
public class JTextField06 extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JTextField text = null;
JButton button1 = null;
JButton button2 = null;
JButton button3 = null;
/**
* 開始メソッド
*
* @param args パラメータ
*/
public static void main(String[] args) {
JTextField06 frame = new JTextField06();
// 閉じるボタンをクリックされた場合の動作を設定
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ウインドウのタイトルを設定
frame.setTitle("JTextField サンプル06");
// フレームの X座標、Y座標、幅、高さを設定
frame.setBounds(100, 200, 400, 150);
// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
frame.setVisible(true);
}
/**
* コンストラクタ
*/
public JTextField06() {
// パネルを作成
JPanel panelBase = new JPanel();
// テキストを作成
text = new JTextField("テキスト", 30);
// ボタンを作成
button1 = new JButton("切り取り");
button2 = new JButton("コピー");
button3 = new JButton("貼り付け");
// 見やすくなるようにフォントを設定
text.setFont(new Font("MS ゴシック", Font.ITALIC, 20));
// アクションリスナー追加
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
// テキスト、ボタンを追加
panelBase.add(text);
panelBase.add(button1);
panelBase.add(button2);
panelBase.add(button3);
// パネルを追加
getContentPane().add(panelBase);
}
/**
* アクション
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(button1)) {
text.cut();
} else if (e.getSource().equals(button2)) {
text.copy();
} else if (e.getSource().equals(button3)) {
text.paste();
}
}
}
サンプルソースのダウンロード
ソースのダウンロード(Eclipse用のプロジェクトファイルも同梱)
更新履歴
2016/05/13 新規作成
TOP →
Java →
Swing →
JTextField → This Page