当前位置: 代码迷 >> Java相关 >> 求大家帮助修改下
  详细解决方案

求大家帮助修改下

热度:109   发布时间:2012-12-26 19:33:34.0
求大家帮助修改下
修改例W14E6,使得当鼠标在窗口中点击任何位置时,都会将按钮放在以点击位置为中心的位置,并在文本域中显示中心位置的坐标。
import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;

public class W14E6{

    public static void main(String[] args) {
        MyWindow myWin = new MyWindow("鼠标运动事件测试");
        
    }   
   
}

class MyWindow extends Frame implements MouseMotionListener{
    Button b;
    TextArea ta;
   
    MyWindow(String s){
        super(s);
        setLayout(new FlowLayout());
        setBounds(200, 100, 600, 300);
        b = new Button("按钮");
        add(b);
        ta = new TextArea();
        add(ta);
        b.addMouseMotionListener(this);
        ta.addMouseMotionListener(this);
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        setVisible(true);
    }
   
    public void mouseDragged(MouseEvent e){
        int x,y,w,h;
        Component c = (Component)e.getSource();
        e = SwingUtilities.convertMouseEvent(c,e,this);
        x = e.getX();
        y = e.getY();
        w=c.getSize().width;
        h=c.getSize().height;
        c.setLocation(x-w/2,y-h/2);   
    }
   
    public void mouseMoved(MouseEvent e){}
        
}
搜索更多相关的解决方案: 鼠标  public  import  中心  

----------------解决方案--------------------------------------------------------
没分啊 〉???

----------------解决方案--------------------------------------------------------
程序代码:
package com.guigu.guo.main;

import java.awt.*;
import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class W14E6
{

    public static void main(String[] args)
    {
        MyWindow myWin = new MyWindow("鼠标运动事件测试");

    }

}

class MyWindow extends JFrame implements MouseListener
{
    JButton b;
    JTextArea ta;
    JPanel panel;

    MyWindow(String s)
    {
        super(s);
        this.setLayout(new BorderLayout());
        this.setSize(600, 500);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        panel=new JPanel();
        panel.setLayout(null);
        
        b = new JButton("按钮");
        b.setBounds(20, 20, 60, 30);
        
        ta = new JTextArea();
        ta.setRows(5);
        
        this.getContentPane().add(ta,BorderLayout.NORTH);
        this.getContentPane().add(panel,BorderLayout.CENTER);
        
        panel.add(b);
        panel.addMouseListener(this);
        setVisible(true);
    }


    @Override
    public void mouseClicked(MouseEvent arg0)
    {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseEntered(MouseEvent arg0)
    {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseExited(MouseEvent arg0)
    {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
        int x=e.getPoint().x;
        int y=e.getPoint().y;
        ta.setText("中心位置的坐标是:("+x+","+y+")");
        int h=b.getSize().height;
        int w=b.getSize().width;
        b.setLocation(x-w/2,y-h/2);

    }

    @Override
    public void mouseReleased(MouseEvent arg0)
    {
        // TODO Auto-generated method stub
        
    }

}

楼主看看对不对
----------------解决方案--------------------------------------------------------
  相关解决方案