import java.awt.*; import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; public class Test { public static void main (String[] args) { Win win = new Win("Test Window"); } } /*******************/ /* クラスWinの定義 */ /*******************/ class Win extends JFrame implements AdjustmentListener { JScrollBar s_h; JTextField tx_h, tx_v; /******************/ /* コンストラクタ */ /******************/ Win (String name) { // 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); // テキストフィールドとスクロールバーの追加 tx_h = new JTextField("abcdefghijklmnopqrstuvwxyz"); tx_h.setFont(f); pn1.add(tx_h); s_h = new JScrollBar(JScrollBar.HORIZONTAL, 0, 10, 0, 40); s_h.addAdjustmentListener(this); pn1.add(s_h); // 下のパネル // テキストフィールドとスクロールペインの追加 tx_v = new JTextField("abcdefghijklmnopqrstuvwxyz"); tx_v.setFont(f); JScrollPane sp = new JScrollPane(tx_v); cp.add(sp); // Windowの大きさ setSize(200, 200); // ウィンドウを表示 setVisible(true); // イベントアダプタ addWindowListener(new WinEnd()); } /******************************/ /* 上,左,下,右の余白の設定 */ /******************************/ public Insets getInsets() { return new Insets(35, 10, 10, 10); } /********************************************/ /* スクロールバーに従ってカーソル位置を調整 */ /********************************************/ public void adjustmentValueChanged(AdjustmentEvent e) { if (e.getAdjustable() == s_h) { int k = s_h.getValue(); if (k < tx_h.getText().length()) tx_h.setCaretPosition(k); } } /************/ /* 終了処理 */ /************/ class WinEnd extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } }