import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.event.*;
public class Test {
public static void main (String[] args)
{
Win win = new Win("Test Window", "Test Data");
}
}
/*******************/
/* クラスWinの定義 */
/*******************/
class Win extends JFrame implements ActionListener, DocumentListener {
JTextField tx1, tx2;
JTextArea ta;
Document dc;
/******************/
/* コンストラクタ */
/******************/
Win (String name, String data)
{
// Frameクラスのコンストラクタ(Windowのタイトルを引き渡す)
super(name);
// レイアウトの変更(行,列,水平ギャップ,垂直ギャップ)
Container cp = getContentPane();
cp.setLayout(new GridLayout(2, 1, 5, 10));
Font f = new Font("MS 明朝", Font.PLAIN, 20);
// 上のパネル
// パネルの追加
JPanel pn1 = new JPanel();
pn1.setLayout(new GridLayout(2, 1, 5, 10));
cp.add(pn1);
// テキストフィールドの追加
tx1 = new JTextField();
tx1.setFont(f);
tx1.addActionListener(this);
pn1.add(tx1);
tx2 = new JTextField();
tx2.setFont(f);
pn1.add(tx2);
// 下のパネル
// パネルの追加
JPanel pn2 = new JPanel();
cp.add(pn2);
// テキストエリアの追加
ta = new JTextArea(4, 25);
ta.setFont(f);
dc = ta.getDocument();
dc.addDocumentListener(this);
JScrollPane scroll = new JScrollPane(ta);
pn2.add(scroll);
// Windowの大きさ
setSize(350, 300);
// ウィンドウを表示
setVisible(true);
// イベントアダプタ
addWindowListener(new WinEnd());
}
/******************************/
/* 上,左,下,右の余白の設定 */
/******************************/
public Insets getInsets()
{
return new Insets(35, 10, 10, 10);
}
/********************************/
/* 改行キーが押されたときの処理 */
/********************************/
public void actionPerformed(ActionEvent e)
{
String str;
if (e.getSource() == tx1) {
str = tx1.getText();
ta.append(str + "\n");
}
}
/****************************************/
/* TextAreaの内容が変更されたときの処理 */
/****************************************/
// 文字の挿入
public void insertUpdate(DocumentEvent e)
{
Document d = e.getDocument();
if (d == dc) {
tx2.setText("文字が挿入されました");
}
}
// 文字の削除
public void removeUpdate(DocumentEvent e)
{
Document d = e.getDocument();
if (d == dc) {
tx2.setText("文字が削除されました");
}
}
// 文字以外の変更(フォーマットの変更等)
public void changedUpdate(DocumentEvent e) {}
/************/
/* 終了処理 */
/************/
class WinEnd extends WindowAdapter
{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}