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", "Test Data"); } } /*******************/ /* クラスWinの定義 */ /*******************/ class Win extends JFrame implements AdjustmentListener { JScrollBar s_h, s_v; JTextField tx_h, tx_v; /******************/ /* コンストラクタ */ /******************/ 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, 2, 5, 10)); cp.add(pn1); // テキストフィールドとスクロールバーの追加 JLabel lb1 = new JLabel("身長は?"); lb1.setFont(f); pn1.add(lb1); tx_h = new JTextField("100"); tx_h.setFont(f); pn1.add(tx_h); JLabel lb2 = new JLabel(); pn1.add(lb2); s_h = new JScrollBar(JScrollBar.HORIZONTAL, 100, 10, 100, 210); s_h.setUnitIncrement(10); s_h.setBlockIncrement(20); s_h.addAdjustmentListener(this); pn1.add(s_h); // 下のパネル // パネルの追加 JPanel pn2 = new JPanel(); pn2.setLayout(new GridLayout(1, 4, 5, 10)); cp.add(pn2); // テキストフィールドとスクロールバーの追加 JLabel lb3 = new JLabel("体重は?"); lb3.setFont(f); pn2.add(lb3); JLabel lb4 = new JLabel(); pn2.add(lb4); tx_v = new JTextField("30"); tx_v.setFont(f); pn2.add(tx_v); s_v = new JScrollBar(JScrollBar.VERTICAL, 100, 10, 30, 110); s_v.setUnitIncrement(5); s_v.setBlockIncrement(10); s_v.setBackground(Color.yellow); s_v.addAdjustmentListener(this); pn2.add(s_v); // Windowの大きさ setSize(360, 300); // ウィンドウを表示 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) tx_h.setText(Integer.toString(s_h.getValue())); if (e.getAdjustable() == s_v) tx_v.setText(Integer.toString(130 - s_v.getValue())); } /************/ /* 終了処理 */ /************/ class WinEnd extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } }