精华内容
下载资源
问答
  • importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclasstextextendsJFrame{publictext(){text1p=newtext1();setLayout(newBorderLayout());add(p);}classtext1ext...import java.awt.*;import ja...

    importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclasstextextendsJFrame{publictext(){text1p=newtext1();setLayout(newBorderLayout());add(p);}classtext1ext...

    import java.awt.*;

    import javax.swing.*;

    import java.awt.event.*;

    public class text extends JFrame

    { public text(){

    text1 p = new text1();

    setLayout(new BorderLayout());

    add(p);

    }

    class text1 extends JPanel{

    int x=50,y=50;

    public text1(){

    addMouseMotionListener(new MouseMotionAdapter(){

    public void MouseMoved(MouseEvent e){

    x=e.getX();

    y=e.getY();

    repaint();

    }

    });

    }

    protected void paintComponent(Graphics g){

    super.paintComponent(g);

    g.setColor(Color.yellow);

    g.fillOval(x, y, 120, 30);

    g.setColor(Color.red);

    g.drawString("拖动我", x+30, y+20);

    }

    }

    public static void main(String args[])

    {

    text frame =new text();

    frame.setSize(300,300);

    frame.setLocationRelativeTo(null);

    frame.setVisible(true);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    }

    动不了为什么。

    然后为什么不用接接口就可以用鼠标事件

    展开

    展开全文
  • 如果用户拖动鼠标,则小程序上的矩形应根据当前鼠标坐标增加或减少.我有以下代码.在下面的代码中,我使用[b] SelectionArea [/ b]类扩展了在其上执行绘制操作的画布.我在此类中使用[b] image [/ b]变量进行双缓冲,以...

    我正在使用Java.

    我想根据mousedrag事件绘制矩形.如果用户拖动鼠标,则小程序上的矩形应根据当前鼠标坐标增加或减少.

    我有以下代码.

    在下面的代码中,我使用[b] SelectionArea [/ b]类扩展了在其上执行绘制操作的画布.我在此类中使用[b] image [/ b]变量进行双缓冲,以减少闪烁并保存小程序的先前状态(即,绘制小程序的内容)

    但如果我画第一个矩形,代码工作正常.如果我开始绘制第二个矩形,则先前绘制的矩形将消失.我希望先前绘制的矩形出现在屏幕上

    谁能告诉我如何解决这个问题.

    import java.awt.*;

    import java.applet.Applet;

    import java.awt.event.*;

    /*

    * This displays a framed area. When the user drags within

    * the area, this program displays a rectangle extending from

    * where the user first pressed the mouse button to the current

    * cursor location.

    */

    public class RectangleDemo extends Applet {

    SelectionArea drawingPanel;

    Label label;

    public void init() {

    GridBagLayout gridBag = new GridBagLayout();

    GridBagConstraints c = new GridBagConstraints();

    setLayout(gridBag);

    drawingPanel = new SelectionArea(this);

    c.fill = GridBagConstraints.BOTH;

    c.weighty = 1.0;

    c.gridwidth = GridBagConstraints.REMAINDER; //end row

    gridBag.setConstraints(drawingPanel, c);

    add(drawingPanel);

    label = new Label("Drag within the framed area.");

    c.fill = GridBagConstraints.HORIZONTAL;

    c.weightx = 1.0;

    c.weighty = 0.0;

    gridBag.setConstraints(label, c);

    add(label);

    drawingPanel.setVisible(true);

    validate();

    }

    public void paint(Graphics g){

    drawingPanel.repaint();

    }

    public void update(Graphics g){

    paint(g);

    }

    }

    class SelectionArea extends Canvas implements ActionListener, MouseListener, MouseMotionListener{

    Rectangle currentRect;

    RectangleDemo controller;

    //for double buffering

    Image image;

    Graphics offscreen;

    public SelectionArea(RectangleDemo controller) {

    super();

    this.controller = controller;

    addMouseListener(this);

    addMouseMotionListener(this);

    }

    public void actionPerformed(ActionEvent ae){

    repaintoffscreen();

    }

    public void repaintoffscreen(){

    image = createImage(this.getWidth(), this.getHeight());

    offscreen = image.getGraphics();

    Dimension d = getSize();

    if(currentRect != null){

    Rectangle box = getDrawableRect(currentRect, d);

    //Draw the box outline.

    offscreen.drawRect(box.x, box.y, box.width - 1, box.height - 1);

    //repaint();

    }

    }

    public void mouseEntered(MouseEvent me) {}

    public void mouseExited(MouseEvent me){ }

    public void mouseClicked(MouseEvent me){}

    public void mouseMoved(MouseEvent me){}

    public void mousePressed(MouseEvent me) {

    currentRect = new Rectangle(me.getX(), me.getY(), 0, 0);

    repaintoffscreen();

    }

    public void mouseDragged(MouseEvent me) {

    System.out.println("here in dragged()");

    currentRect.setSize(me.getX() - currentRect.x, me.getY() - currentRect.y);

    repaintoffscreen();

    repaint();

    }

    public void mouseReleased(MouseEvent me) {

    currentRect.setSize(me.getX() - currentRect.x, me.getY() - currentRect.y);

    repaintoffscreen();

    repaint();

    }

    public void update(Graphics g){

    paint(g);

    }

    public void paint(Graphics g) {

    g.drawImage(image, 0, 0, this);

    }

    Rectangle getDrawableRect(Rectangle originalRect, Dimension drawingArea) {

    int x = originalRect.x;

    int y = originalRect.y;

    int width = originalRect.width;

    int height = originalRect.height;

    //Make sure rectangle width and height are positive.

    if (width < 0) {

    width = 0 - width;

    x = x - width + 1;

    if (x < 0) {

    width += x;

    x = 0;

    }

    }

    if (height < 0) {

    height = 0 - height;

    y = y - height + 1;

    if (y < 0) {

    height += y;

    y = 0;

    }

    }

    //The rectangle shouldn't extend past the drawing area.

    if ((x + width) > drawingArea.width) {

    width = drawingArea.width - x;

    }

    if ((y + height) > drawingArea.height) {

    height = drawingArea.height - y;

    }

    return new Rectangle(x, y, width, height);

    }

    }

    另外,如果我在全屏模式下运行此代码,那么我会看到只有在释放鼠标后矩形才会出现在屏幕上.但是我希望在拖动鼠标时在屏幕上显示矩形,并且应该根据当前鼠标坐标更改矩形的尺寸.

    谁能帮助我.

    展开全文
  • 如果用户拖动鼠标,则小程序上的矩形应根据当前鼠标坐标增加或减少。我有以下代码。在下面的代码中,我使用[b] SelectionArea [/ b]类扩展了在其上执行绘制操作的画布。我在此类中使用[b] image [/b]变量进行双缓冲...

    我正在使用Java。我想根据mousedrag事件绘制矩形。如果用户拖动鼠标,则小程序上的矩形应根据当前鼠标坐标增加或减少。我有以下代码。

    在下面的代码中,我使用[b] SelectionArea [/ b]类扩展了在其上执行绘制操作的画布。我在此类中使用[b] image [/

    b]变量进行双缓冲,以减少闪烁并保存小程序的先前状态(即,绘制小程序的内容)

    但如果我画第一个矩形,代码工作正常。如果我开始绘制第二个矩形,则先前绘制的矩形将消失。我希望先前绘制的矩形出现在屏幕上

    谁能告诉我如何解决这个问题。

    import java.awt.*;

    import java.applet.Applet;

    import java.awt.event.*;

    /*

    * This displays a framed area. When the user drags within

    * the area, this program displays a rectangle extending from

    * where the user first pressed the mouse button to the current

    * cursor location.

    */

    public class RectangleDemo extends Applet {

    SelectionArea drawingPanel;

    Label label;

    public void init() {

    GridBagLayout gridBag = new GridBagLayout();

    GridBagConstraints c = new GridBagConstraints();

    setLayout(gridBag);

    drawingPanel = new SelectionArea(this);

    c.fill = GridBagConstraints.BOTH;

    c.weighty = 1.0;

    c.gridwidth = GridBagConstraints.REMAINDER; //end row

    gridBag.setConstraints(drawingPanel, c);

    add(drawingPanel);

    label = new Label("Drag within the framed area.");

    c.fill = GridBagConstraints.HORIZONTAL;

    c.weightx = 1.0;

    c.weighty = 0.0;

    gridBag.setConstraints(label, c);

    add(label);

    drawingPanel.setVisible(true);

    validate();

    }

    public void paint(Graphics g){

    drawingPanel.repaint();

    }

    public void update(Graphics g){

    paint(g);

    }

    }

    class SelectionArea extends Canvas implements ActionListener, MouseListener, MouseMotionListener{

    Rectangle currentRect;

    RectangleDemo controller;

    //for double buffering

    Image image;

    Graphics offscreen;

    public SelectionArea(RectangleDemo controller) {

    super();

    this.controller = controller;

    addMouseListener(this);

    addMouseMotionListener(this);

    }

    public void actionPerformed(ActionEvent ae){

    repaintoffscreen();

    }

    public void repaintoffscreen(){

    image = createImage(this.getWidth(), this.getHeight());

    offscreen = image.getGraphics();

    Dimension d = getSize();

    if(currentRect != null){

    Rectangle box = getDrawableRect(currentRect, d);

    //Draw the box outline.

    offscreen.drawRect(box.x, box.y, box.width - 1, box.height - 1);

    //repaint();

    }

    }

    public void mouseEntered(MouseEvent me) {}

    public void mouseExited(MouseEvent me){ }

    public void mouseClicked(MouseEvent me){}

    public void mouseMoved(MouseEvent me){}

    public void mousePressed(MouseEvent me) {

    currentRect = new Rectangle(me.getX(), me.getY(), 0, 0);

    repaintoffscreen();

    }

    public void mouseDragged(MouseEvent me) {

    System.out.println("here in dragged()");

    currentRect.setSize(me.getX() - currentRect.x, me.getY() - currentRect.y);

    repaintoffscreen();

    repaint();

    }

    public void mouseReleased(MouseEvent me) {

    currentRect.setSize(me.getX() - currentRect.x, me.getY() - currentRect.y);

    repaintoffscreen();

    repaint();

    }

    public void update(Graphics g){

    paint(g);

    }

    public void paint(Graphics g) {

    g.drawImage(image, 0, 0, this);

    }

    Rectangle getDrawableRect(Rectangle originalRect, Dimension drawingArea) {

    int x = originalRect.x;

    int y = originalRect.y;

    int width = originalRect.width;

    int height = originalRect.height;

    //Make sure rectangle width and height are positive.

    if (width < 0) {

    width = 0 - width;

    x = x - width + 1;

    if (x < 0) {

    width += x;

    x = 0;

    }

    }

    if (height < 0) {

    height = 0 - height;

    y = y - height + 1;

    if (y < 0) {

    height += y;

    y = 0;

    }

    }

    //The rectangle shouldn't extend past the drawing area.

    if ((x + width) > drawingArea.width) {

    width = drawingArea.width - x;

    }

    if ((y + height) > drawingArea.height) {

    height = drawingArea.height - y;

    }

    return new Rectangle(x, y, width, height);

    }

    }

    另外,如果我在全屏模式下运行此代码,那么我会看到只有在释放鼠标后矩形才会出现在屏幕上。但是我希望矩形在拖动鼠标时出现在屏幕上,并且应该根据当前鼠标坐标更改其尺寸。谁能帮助我。

    展开全文
  • 小编典典好吧,在重新阅读您的问题后,似乎您不必担心拥有多个矩形:)这是一次只能解决一个问题的解决方案(与您开始时的解决方案很接近):import java.awt.BorderLayout;import java.awt.Graphics;import java.awt....

    小编典典

    好吧,在重新阅读您的问题后,似乎您不必担心拥有多个矩形:)

    这是一次只能解决一个问题的解决方案(与您开始时的解决方案很接近):

    import java.awt.BorderLayout;

    import java.awt.Graphics;

    import java.awt.event.MouseEvent;

    import java.awt.event.MouseListener;

    import java.awt.event.MouseMotionListener;

    import javax.swing.JFrame;

    import javax.swing.JLabel;

    import javax.swing.SwingConstants;

    public class MouseTracker4July extends JFrame implements MouseListener, MouseMotionListener {

    private static final long serialVersionUID = 1L;

    private final JLabel mousePosition;

    int x1, x2, y1, y2;

    int x, y, w, h;

    private final JLabel recStart;

    private final JLabel recStop;

    private final JLabel cords; // set up GUI and register mouse event handlers

    boolean isNewRect = true;

    public MouseTracker4July() {

    super( "Rectangle Drawer" );

    this.mousePosition = new JLabel();

    this.mousePosition.setHorizontalAlignment( SwingConstants.CENTER );

    getContentPane().add( this.mousePosition, BorderLayout.CENTER );

    JLabel text1 = new JLabel();

    text1.setText( "At the center the mouse pointer's coordinates will be displayed." );

    getContentPane().add( text1, BorderLayout.SOUTH );

    this.recStart = new JLabel();

    getContentPane().add( this.recStart, BorderLayout.WEST );

    this.recStop = new JLabel();

    getContentPane().add( this.recStop, BorderLayout.EAST );

    this.cords = new JLabel();

    getContentPane().add( this.cords, BorderLayout.NORTH );

    addMouseListener( this ); // listens for own mouse and

    addMouseMotionListener( this ); // mouse-motion events

    setSize( 800, 600 );

    setVisible( true );

    }

    // MouseListener event handlers // handle event when mouse released immediately after press

    public void mouseClicked( final MouseEvent event ) {

    this.mousePosition.setText( "Clicked at [" + event.getX() + ", " + event.getY() + "]" );

    repaint();

    }

    // handle event when mouse pressed

    public void mousePressed( final MouseEvent event ) {

    this.mousePosition.setText( "Pressed at [" + ( this.x1 = event.getX() ) + ", " + ( this.y1 = event.getY() ) + "]" );

    this.recStart.setText( "Start: [" + this.x1 + ", " + this.y1 + "]" );

    this.isNewRect = true;

    repaint();

    }

    // handle event when mouse released after dragging

    public void mouseReleased( final MouseEvent event ) {

    this.mousePosition.setText( "Released at [" + ( this.x2 = event.getX() ) + ", " + ( this.y2 = event.getY() ) + "]" );

    this.recStop.setText( "End: [" + this.x2 + ", " + this.y2 + "]" );

    repaint();

    }

    // handle event when mouse enters area

    public void mouseEntered( final MouseEvent event ) {

    this.mousePosition.setText( "Mouse entered at [" + event.getX() + ", " + event.getY() + "]" );

    repaint();

    }

    // handle event when mouse exits area

    public void mouseExited( final MouseEvent event ) {

    this.mousePosition.setText( "Mouse outside window" );

    repaint();

    }

    // MouseMotionListener event handlers // handle event when user drags mouse with button pressed

    public void mouseDragged( final MouseEvent event ) {

    this.mousePosition.setText( "Dragged at [" + ( this.x2 = event.getX() ) + ", " + ( this.y2 = event.getY() ) + "]" ); // call repaint which calls paint repaint();

    this.isNewRect = false;

    repaint();

    }

    // handle event when user moves mouse

    public void mouseMoved( final MouseEvent event ) {

    this.mousePosition.setText( "Moved at [" + event.getX() + ", " + event.getY() + "]" );

    repaint();

    }

    @Override

    public void paint( final Graphics g ) {

    super.paint( g ); // clear the frame surface

    g.drawString( "Start Rec Here", this.x1, this.y1 );

    g.drawString( "End Rec Here", this.x2, this.y2 );

    int width = this.x1 - this.x2;

    int height = this.y1 - this.y2;

    this.w = Math.abs( width );

    this.h = Math.abs( height );

    this.x = width < 0 ? this.x1

    : this.x2;

    this.y = height < 0 ? this.y1

    : this.y2;

    if ( !this.isNewRect ) {

    g.drawRect( this.x, this.y, this.w, this.h );

    }

    this.cords.setText( "w = " + this.w + ", h = " + this.h );

    }

    public static void main( final String args[] ) {

    MouseTracker4July application = new MouseTracker4July();

    application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    }

    } // end class MouseTracker

    2020-11-16

    展开全文
  • nb-首先要注意的是,这是使用Java 7完成的,在Java 6中...然后我使用JPanel作为主要容器来捕获鼠标事件并执行绘制效果.面板是透明的.这允许面板(和框架)下方的任何东西保持可见.然后我用透明的颜色画了这个(我这样做只...
  • 鼠标坐标与小球坐标比较3.1 鼠标悬浮在小球上时,面板文字显示“please click”,同时将悬浮表示markM置为true;3.2 鼠标点击选中小球时,面板文字再显示”Bingo“,同时将选中标识markC置为true;3.3 鼠标离开晓...
  • java Jframe 我再NetBeans中创建两个窗口,一个登录一个主窗口。都设置undecorated为True。同事都对窗口进行鼠标拖动的设置,但是为什么只有登录可以,主窗口不行呢?求大神指点迷津
  • 相对于其它的鼠标拖动效果,这款拖动特效还是比较精简的,而且它还支持鼠标吸附,不按鼠标左键它也可以会跟随鼠标移动;定义时候也相对方便,只用指定被拖动的DIV ID就可以了,扩展性很好。/p>...
  • import java.awt.*; import java.awt.event.*; public class TestMouseDrag extends Frame{ TestMouseDrag(){ setBounds(100,100,500,500); addMouseListener(new MouseAction()); setVisible...
  • 鼠标操作是图形操作系统最常用操作,用户使用鼠标单击,双击,右击,拖动等操作实现与软件的交互。鼠标事件监听器鼠标事件监听器由MouseListener接口和MouseMotionListener接口定义,分别定义鼠标捕获不同的鼠标操作...
  • importjava.awt.Color;importjava.awt.Graphics;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjava.awt.event.MouseMotionListener;importjavax.swing.JFrame;importjavax.swing.JPan...
  • Java鼠标事件

    千次阅读 2018-02-06 16:49:41
    鼠标事件事件源往往与容器相关,当鼠标进入容器、离开容器,或者在容器中单击鼠标拖动鼠标时都会发生鼠标事件java语言为处理鼠标事件提供两个接口:MouseListener,MouseMotionListener接口。 ...
  • 任何组件上都可以发生鼠标事件,如:鼠标进进组件、退出组件、在组件上方单击鼠标拖动鼠标等都发生鼠标事件,也就是说,组件可以成为发生鼠标事件事件源。1 使用MouseListener接口处理鼠标事件使用MouseListener...
  • {相关信息}鼠标事件类(MouseEvent)指组件中发生的鼠标动作事件,例如按下鼠标、释放鼠标、单击鼠标鼠标光标进入或离开组件的几何图形、移动鼠标拖动鼠标。当鼠标移动到某个区域或鼠标单击某个组件时就会触发鼠标...
  • 【单选题】上颌中切牙的发育沟描述正确的是【多选题】调节器参数常用的工程整定方法有( )。【单选题】目前,最常用的网络广告形式是( )。【判断题】只要两个力大小相等、方向相反,该两力就组成一力偶。...
  • 鼠标事件事件源往往与容器相关,当鼠标进入容器、离开容器,或者在容器中单击鼠标拖动鼠标时都会发生鼠标事件java语言为处理鼠标事件提供两个接口:MouseListener,MouseMotionListener接口。MouseListener...
  • 鼠标事件事件源往往与容器相关,当鼠标进入容器、离开容器,或者在容器中单击鼠标拖动鼠标时都会发生鼠标事件java语言为处理鼠标事件提供两个接口:MouseListener,MouseMotionListener接口。MouseListener...
  • java GUI 鼠标事件

    千次阅读 2017-11-07 20:54:09
    鼠标事件可分为几种 1.鼠标运动事件 (可以监听鼠标在某个控件上移动或者拖动 并且获取一些详细的参数) but.addMouseMotionListener... //鼠标拖动事件 //可以获取例如坐标 等需求 public void mouseDragged(MouseE
  • Java鼠标事件MouseEvent

    2014-06-20 16:36:07
    当在一个组件上按下,释放,点击,移动或拖动鼠标时就会产生鼠标事件。MouseEvent对象捕获这个事件.
  • 当用户单击JFrame的角落来调整大小并拖动鼠标时,JFrame会根据用户拖动鼠标的当前位置重绘。你怎么听这些事件?下面是我目前尝试的:public final class TestFrame extends JFrame {public TestFrame() {this....
  • java鼠标事件监听

    千次阅读 2017-12-03 11:07:50
    addMouseListenerpublic void addMouseListener(MouseListener l)添加指定的鼠标侦听器,以接收发自此组件的鼠标事件。如果侦听器 l 为 null,则不会抛出异常并且不执行...(要跟踪鼠标移动和鼠标拖动,请使用 MouseMot
  • [Java教程]js拖拽——将鼠标事件响应范围扩大到整个系统桌面02014-05-09 18:00:15起因:最近在做一个可拖拽的拓扑图,遇到的这个问题:如果执行拖拽操作的时候鼠标拖动很快,可能会出现鼠标脱离页面乃至浏览器的范围...
  • 鼠标事件事件源往往与容器相关,当鼠标进入容器、离开容器,或者在容器中单击鼠标拖动鼠标时都会发生鼠标事件java语言为处理鼠标事件提供两个接口:MouseListener,MouseMotionListener接口。 ...
  • Java实现图片随鼠标拖动

    千次阅读 2016-10-14 19:55:22
    面向对象的实验要实现用鼠标拖动一张纸牌图片。经过相关查询可知要为图片注册事件MouseListener和MouseMotionListener中的mousePressed方法和mouseDragged方法。这里的MyMouseInputAdapter继承了MouseInputAdapter类...
  • import java.awt.*; import java.awt.event.*; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JButton; public class duozhongListener implements Mo
  • 最近在写自动化测试,在网上搜了很多拖动,发现都是拖动完之后不触发拖动完成事件,下面的代码是针对上面的这种滑块解锁登录的。 //输入用户名 driver.findElement(By.name("username")).sendKeys(...
  • 在我们取消窗口的边框以及标题栏以后,窗口就无法响应鼠标的点击事件了,也就无法被鼠标拖动而改变其在桌面上的位置了。那么我们需要如下做: 2、使取消了边框以及标题栏窗口能够响应鼠标事件: 创建如下一个类来...

空空如也

空空如也

1 2 3 4 5 ... 9
收藏数 178
精华内容 71
关键字:

java鼠标拖动事件

java 订阅