マウスイベントの捕捉およびサブウィンドウの表示

戻る


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

/**
$Id: mc.html,v 1.1 2009/06/22 16:12:17 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class MouseEventTest2 extends JFrame implements MouseListener {

    private JLabel eventLabel;
    private JLabel yetAnotherLabel;
    private JTextArea textArea;
    /** 左クリックされたときに生成されるウィンドウ */
    private MyWindow mw = null;

    public static void main( String[] args ) {
        /* 自分自身を作成 */
        MouseEventTest2 test = new MouseEventTest2();

        /* 終了処理を追加 */
        test.addWindowListener( new WindowAdapter() {
                                    public void windowClosing( WindowEvent e ) {
                                        System.exit( 0 );
                                    }
                                }
                              );

        /* サイズと位置を指定 */
        test.setBounds( 10, 10, 300, 200 );

        /* 実際に表示する */
        test.setVisible( true );
    }

    public MouseEventTest2() {
        eventLabel = new JLabel( "マウスを適当に動かしてください!" );
        eventLabel.addMouseListener( this );
        eventLabel.setHorizontalAlignment( JLabel.CENTER );

        yetAnotherLabel = new JLabel( "This is Yet Another Label." );
        yetAnotherLabel.addMouseListener( this );
        yetAnotherLabel.setHorizontalAlignment( JLabel.CENTER );

        textArea = new JTextArea( "" );
        textArea.setLineWrap( true );
        textArea.setWrapStyleWord( true );

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.getViewport().setView( textArea );

        getContentPane().add( eventLabel, BorderLayout.NORTH );
        getContentPane().add( yetAnotherLabel, BorderLayout.SOUTH );
        getContentPane().add( scrollPane, BorderLayout.CENTER );
    }

    /* マウスがクリックされた時の処理 */
    public void mouseClicked( MouseEvent e ) {
        int modifier = e.getModifiers();

        // どのラベルでクリックされたのか判別する
        String text = ( ( JLabel ) e.getComponent() ).getText();
        System.out.println( "JLabelの文字列 = " + text );

        // SHIFT,ALT,CTRLキーの押下の判別
        String s = getShiftAltCtrl( modifier );

        if ( ( modifier & InputEvent.BUTTON1_MASK ) != 0 ) {
            /** 左クリック **/
            textArea.append( "Button1" );

            //新しいウィンドウを表示する
            mw = new MyWindow( e.getComponent() );


        } else if ( ( modifier & InputEvent.BUTTON2_MASK ) != 0 ) {
            textArea.append( "Button2" );
        } else if ( ( modifier & InputEvent.BUTTON3_MASK ) != 0 ) {
            /** 右クリック **/
            textArea.append( "Button3" );

        }

        if ( !( s.equals( "" ) ) ) {
            textArea.append( "(" + s + ")" );
        }
        textArea.append( " / " );

        textArea.setCaretPosition( textArea.getText().length() );
    }

    private String getShiftAltCtrl( int modifier ) {
        StringBuffer sb = new StringBuffer();

        if ( ( modifier & InputEvent.SHIFT_MASK ) != 0 ) {
            sb.append( "SHIFT " );
        }

        if ( ( modifier & InputEvent.ALT_MASK ) != 0 ) {
            sb.append( "ALT " );
        }

        if ( ( modifier & InputEvent.CTRL_MASK ) != 0 ) {
            sb.append( "CTRL " );
        }

        return ( new String( sb ) );
    }

    public void mouseEntered( MouseEvent e ) {}
    public void mouseExited( MouseEvent e ) {}
    public void mousePressed( MouseEvent e ) {}
    public void mouseReleased( MouseEvent e ) {}
}


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

/**
$Id: mc.html,v 1.1 2009/06/22 16:12:17 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class MyWindow extends JFrame {

    public MyWindow( Component component ) {

        String text = ( ( JLabel ) component ).getText();

        super.setTitle( "サブウィンドウでやんす♪ -- " + text );

        if ( "マウスを適当に動かしてください!".equals( text ) ) {
            /* サイズと位置を指定 */
            setBounds( 500, 10, 500, 200 );
        } else if ( "This is Yet Another Label.".equals( text ) ) {
            setBounds( 400, 300, 600, 200 );

        }

        /* 実際に表示する */
        setVisible( true );

    }

}

戻る inserted by FC2 system