显示鼠标坐标的标签

label that shows Coordinates of mouse

在大学里,我们开始 Java 编程并得到了编写一个程序的任务,该程序在鼠标当前所在的位置绘制一条垂直线和一条水平线。我们还应该添加一个显示鼠标坐标的标签。我可以使用绘图功能,但是当我尝试添加标签时,它不会显示吗?我从测试标签开始,但框架内也没有显示。有人能帮我吗?

public class Coordinates extends JPanel implements MouseListener, MouseMotionListener {

private Point currentPoint = new Point(-50, -50);


public Coordinates(){
    addMouseListener(this);
    addMouseMotionListener(this);


}

public void paint(Graphics g) {
    super.paint(g);
    g.setColor(Color.blue);
    g.drawLine(currentPoint.x, currentPoint.y+1000, currentPoint.x, currentPoint.y-1000);
    g.drawLine(currentPoint.x+1000, currentPoint.y, currentPoint.x-1000, currentPoint.y);

}

public void mousePressed(MouseEvent e){

    currentPoint = e.getPoint();
    repaint();
};

static JLabel label = new JLabel();

public static void main(String[] args) {

    JFrame frame = new JFrame("Koordinaten");
    frame.setSize(600, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.add(label);

    JComponent newContentPane = new Coordinaten();
    newContentPane.setOpaque(true);

    frame.setContentPane(newContentPane);

}


public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub

}





public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}


public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}


public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}


public void mouseDragged(MouseEvent e) {
    // TODO Auto-generated method stub

}


public void mouseMoved(MouseEvent e) {
    // TODO Auto-generated method stub
    label.setText(currentPoint.toString());

    currentPoint = e.getPoint();
    repaint();
}

}

  1. 在 JPanel 的 paintComponent 方法而不是 paint 方法中完成所有绘制,并确保在覆盖方法中调用超级 paintComponent 方法,通常是在覆盖方法的开头。
  2. 您需要将 JLabel 添加到 JPanel 才能显示任何内容。您的代码不会这样做。然后在 MouseMotionListener 中,使用鼠标坐标设置 JLabel 的文本。

例如:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class DrawPanel extends JPanel {
    private static final int PREF_W = 600;
    private static final int PREF_H = PREF_W;
    private JLabel locationLabel = new JLabel();

    public DrawPanel() {
        add(locationLabel);
        addMouseMotionListener(new MyMouseAdapter());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);  // this allows JPanel to do housekeeping painting first

        // do drawing here!

    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private class MyMouseAdapter extends MouseAdapter {
        @Override
        public void mouseMoved(MouseEvent e) {
            // get Point location and turn into a String
            String location = String.format("[%d, %d]", e.getX(), e.getY());

            // set the label's text with this String
            locationLabel.setText(location);
        }
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("DrawPanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new DrawPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

有了十字准线:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class DrawPanel extends JPanel {
    private static final int PREF_W = 600;
    private static final int PREF_H = PREF_W;
    private JLabel locationLabel = new JLabel();
    private int mouseX = 0;
    private int mouseY = 0;

    public DrawPanel() {
        add(locationLabel);
        addMouseMotionListener(new MyMouseAdapter());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); // this allows JPanel to do housekeeping
                                 // painting first

            // do drawing here!
            g.drawLine(0, mouseY, getWidth(), mouseY);
            g.drawLine(mouseX, 0, mouseX, getHeight());
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private class MyMouseAdapter extends MouseAdapter {
        @Override
        public void mouseMoved(MouseEvent e) {
            mouseX = e.getX();
            mouseY = e.getY();

            // get Point location and turn into a String
            String location = String.format("[%d, %d]", mouseX, mouseY);

            // set the label's text with this String
            locationLabel.setText(location);

            repaint();
        }
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("DrawPanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new DrawPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

基本上,JLabel 是这样制作的,在 main 方法之外定义:

static JLabel label = new JLabel();

在你的主要方法中

frame.add(label);

并且在您的 mouseMoved 方法中,您可以这样写:

label.setText(currentPoint.toString());