/**
*这个程序中加的事件监听没什么没有用!它监听第一次,好像是闪了一下,但只要TextArea出现之后,就不起作用了!是不是因为TextArea占满了整个JWindow,就不行了呢?如果我要实现把鼠标移上去,就显示大窗口,移走就显示小窗口,应该怎么做呢?
*/
package everyday;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyWindow extends JWindow implements MouseListener{
    public MyWindow(Frame owner) {
super(owner);
init();
    addMouseListener(this);
    }
private void addThings() {
        nowDayText = new javax.swing.JTextPane();
        jScrollPane1 = new javax.swing.JScrollPane(nowDayText);
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-415)/2, (screenSize.height-333)/2, 415, 333);
setVisible(true);
    }
    public static void main(String args[])
    {
MyWindow me=new MyWindow(new JFrame());
me.setSize(120, 50);
me.setLocation(500,50);
me.show();
    }
    public MyWindow(Window owner){
        super(owner);
        init();
        addMouseListener(this);}
    public MyWindow(Window owner, GraphicsConfiguration gc){
        super(owner,gc);
        init();
        addMouseListener(this);}
    public void init()
    {
        setSize(410,315);
        setLocation(500,50);
        addThings();
    }
    public void mouseClicked(MouseEvent e)
    {}
    public void mouseEntered(MouseEvent e)
    {
        setSize(410,315);
        setLocation(500,50);
repaint();
    }
    public void mouseExited(MouseEvent e)
    {
        setSize(120, 50);
        setLocation(500,50);
getContentPane().add(jScrollPane1);
repaint();
    }
    public void mousePressed(MouseEvent e) 
    {}
    public void mouseReleased(MouseEvent e)
    {}
// 变量声明 - 不进行修改//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane nowDayText;
    // 变量声明结束//GEN-END:variables
}

解决方案 »

  1.   

    你的鼠标监听事件已经起作用了,用如下方法可以看到你的输出:
    public void mouseEntered(MouseEvent e) {
        setSize(410, 315);
        setLocation(500, 50);
        getContentPane().add(jScrollPane1);
        System.out .println("mouseEntered") ;
        repaint();
      }  public void mouseExited(MouseEvent e) {
        setSize(120, 50);
        setLocation(500, 50);
        getContentPane().add(jScrollPane1);
        System.out .println("mouseExited") ;
        repaint();
      }
      

  2.   

    不过没看见你的JScrollPane出现
      

  3.   

    package test;import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;public class MyNewWindow
        extends JWindow implements MouseListener
    {
        public MyNewWindow(Frame owner)
        {
            super(owner);
            init();
            //addMouseListener(this);
        }    private void addThings()
        {
            nowDayText = new javax.swing.JTextPane();
            nowDayText.addMouseListener(this);
            jScrollPane1 = new javax.swing.JScrollPane(nowDayText);
            getContentPane().add(jScrollPane1);
            java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
            setBounds( (screenSize.width - 415) / 2, (screenSize.height - 333) / 2, 415, 333);
            setVisible(true);
        }    public static void main(String args[])
        {
            MyNewWindow me = new MyNewWindow(new JFrame());
            me.setSize(120, 50);
            me.setLocation(500, 50);
            me.show();
        }    public void init()
        {
            setSize(410, 315);
            setLocation(500, 50);
            addThings();
        }    public void mouseClicked(MouseEvent e)
        {}    public void mouseEntered(MouseEvent e)
        {
            System.out.println("hello,enter");
            setSize(410, 315);
            setLocation(500, 50);
            repaint();
            this.validate();
        }    public void mouseExited(MouseEvent e)
        {
            System.out.println("hello,exit");
            setSize(120, 50);
            setLocation(500, 50);
            
            repaint();
            this.validate();
        }    public void mousePressed(MouseEvent e)
        {}    public void mouseReleased(MouseEvent e)
        {}// 变量声明 - 不进行修改//GEN-BEGIN:variables
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextPane nowDayText;
        // 变量声明结束//GEN-END:variables
    }