java - 整个窗口上的鼠标坐标

标签 java swing user-interface mouseevent

我的代码应该显示鼠标的坐标,因为它与整个窗口相关,但我使用的方法只会给出每个单独组件的坐标。如何获取整个窗口和所有组件的坐标?

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

import java.util.*;
import java.text.*;

public class Clock extends JFrame implements ActionListener, Runnable, MouseMotionListener {
    public static final int w = 500;
    public static final int l = 200;
    public JLabel label;
    String msg= null;
    public int color=0;
      int xpos; 
         int ypos;
         JTextField xf = new JTextField("b");
         JTextField yf = new JTextField("d");
         JTextField butf = new JTextField("f");
     Date date = new Date();

      String DATE_FORMAT = "hh:mm:ss";

      SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);

      public void mouseMoved(MouseEvent e)  
         {  

          xpos = e.getX();
          ypos = e.getY();
          xf.setText(""+xpos);
          yf.setText(""+ypos);

         }
      public void mouseDragged(MouseEvent me)  
         {  
              xpos = me.getX(); 
              ypos = me.getY(); 
         }
    public Clock() {
        super("CS 302 Lab 7");
        addMouseMotionListener(this); 
        JPanel main = new JPanel();
        JPanel top = new JPanel();
        main.setLayout(new GridLayout( 1,0));
        top.setLayout(new GridLayout( 1,0));
        setLayout(new BorderLayout());
        setSize(w, l);

        JLabel x = new JLabel("X");
        x.addMouseMotionListener(this);
        JLabel y = new JLabel("Y");
        y.addMouseMotionListener(this);
        JLabel but = new JLabel("Button");
        but.addMouseMotionListener(this);
        xf.addMouseMotionListener(this);
        yf.addMouseMotionListener(this);
        butf.addMouseMotionListener(this);

        top.add(x);
        top.add(xf);
        top.add(y);
        top.add(yf);
        top.add(but);
        top.add(butf);

        msg = (sdf.format(date));
        label = new JLabel(msg, SwingConstants.CENTER);
        label.setFont(new Font("Serif", Font.PLAIN, 40));

        add(BorderLayout.CENTER, label);

        JButton red = new JButton("Red");
        main.add(red);
        red.addActionListener(this);


        JButton green = new JButton("Green");
        main.add(green);
        green.addActionListener(this);

        JButton blue = new JButton("Blue");
        main.add(blue);
        blue.addActionListener(this);

        JButton orange = new JButton("Orange");
        main.add(orange);
        orange.addActionListener(this);

        red.addMouseMotionListener(this);
        blue.addMouseMotionListener(this);
        green.addMouseMotionListener(this);
        orange.addMouseMotionListener(this);

        SwingUtilities.convertPoint(red, x, y, destination)
        add(BorderLayout.SOUTH, main );
        add(BorderLayout.NORTH, top );
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
    //  Clock clock = new Clock();
        Runnable rn = new Clock();
        Thread th = new Thread(rn);
        th.start();

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("Red")){
            Date date = new Date();
            msg = (sdf.format(date));
            label.setText(msg);
            label.setForeground(Color.RED);
            color=1;
        }
        else if (e.getActionCommand().equals("Blue")){
            Date date = new Date();
            msg = (sdf.format(date));
            label.setText(msg);
            label.setForeground(Color.BLUE);
            color=2;
        }
        else if (e.getActionCommand().equals("Green")){
            Date date = new Date();
            msg = (sdf.format(date));
            label.setText(msg);
            label.setForeground(Color.GREEN);
            color=3;
        }
        else if (e.getActionCommand().equals("Orange")){
            Date date = new Date();
            msg = (sdf.format(date));
            label.setText(msg);
            label.setForeground(Color.ORANGE);
            color=4;
        }
    }

    @Override
    public void run() {
        while (true){
             Date date = new Date();
        msg = (sdf.format(date));
        label.setText(msg);
        if (color ==1)
            label.setForeground(Color.RED);
        else if (color ==2)
            label.setForeground(Color.BLUE);
        else if (color ==3)
            label.setForeground(Color.GREEN);
        else if (color ==4)
            label.setForeground(Color.ORANGE);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
    }

}

最佳答案

您可以使用SwingUtilities.convertPoint 。在 mouseMoved() 方法中尝试一下:

Point p = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), getContentPane());

根据需要更新目标组件,我使用 getContentPane() 转换为框架 contentPane 的坐标。

关于java - 整个窗口上的鼠标坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9657624/

相关文章:

java - 需要创建用户在 swing 应用程序中输入的信息并将其写入文件

java - 我需要一个基本的简单Java布局方法

java - 如何在java中构建缓冲区来合并图像

java - 我将如何将 boolean 字符串更改为 JTable 中的 JCheckBox?

Java 接口(interface)实现 - 自动初始化器

java - Tomcat 7 慢启动元数据完整 ="true"web.xml

java - 第一次和第二次 ACTION_DOWN 之间耗时

java - 理解java swing的困难

java - Knock Knock 带有服务器和 UI 的应用程序

Git GUI 类似于 MS Windows 中的 HG Workbench