import java.awt.*; 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 Frame implements MouseListener, MouseMotionListener { TextArea tx1, tx2; /******************/ /* コンストラクタ */ /******************/ Win (String name, String data) { // Frameクラスのコンストラクタ(Windowのタイトルを引き渡す) super(name); // レイアウトの変更(行,列,水平ギャップ,垂直ギャップ) setLayout(new GridLayout(2, 1, 5, 10)); Font f = new Font("MS 明朝", Font.PLAIN, 20); // テキストエリアの追加 tx1 = new TextArea(4, 23); tx1.setFont(f); tx1.addMouseListener(this); tx1.addMouseMotionListener(this); add(tx1); tx2 = new TextArea(4, 23); tx2.setFont(f); add(tx2); // Windowの大きさ setSize(300, 400); // ウィンドウを表示 setVisible(true); // イベントアダプタ addWindowListener(new WinEnd()); } /******************************/ /* 上,左,下,右の余白の設定 */ /******************************/ public Insets getInsets() { return new Insets(35, 10, 10, 10); } /***************************/ /* MouseListenerによる処理 */ /***************************/ public void mouseClicked(MouseEvent e) { tx1.append("クリック(回数:" + e.getClickCount() + ")\n"); tx1.append(" 位置 (" + e.getX() + "," + e.getY() + ")\n"); } public void mousePressed(MouseEvent e) { if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0) tx1.append("押した\n"); } public void mouseReleased(MouseEvent e) { if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0) tx1.append("離した\n"); } public void mouseEntered(MouseEvent e) { tx1.append("入った\n"); } public void mouseExited(MouseEvent e) { tx1.append("出た\n"); } /*********************************/ /* MouseMotionListenerによる処理 */ /*********************************/ public void mouseDragged(MouseEvent e) { if ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0) tx2.append("ドラッグ(" + e.getX() + ", " + e.getY() + ")\n"); } public void mouseMoved(MouseEvent e) { if ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0) tx2.append("移動(" + e.getX() + ", " + e.getY() + ")\n"); } /************/ /* 終了処理 */ /************/ class WinEnd extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } }