【问题标题】:label that shows Coordinates of mouse显示鼠标坐标的标签
【发布时间】:2016-02-11 06:41:21
【问题描述】:

在大学时,我们开始了 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();
}

}

【问题讨论】:

  • 抱歉,我在您发布的代码中的任何地方都看不到 JLabel。它在哪里?
  • 作为附带建议,在 JPanel 的 paintComponent 方法中进行所有绘制,并确保在您的覆盖中调用 super 的方法,通常在覆盖方法的开头。
  • 是的,很抱歉我没有将标签放在代码中,我想你们中的一个可以告诉我如何把它放在里面?
  • 如果你不向我们展示你是如何做到的,我们怎么知道你做错了什么?
  • How to create a GUI with Swing 应该向您展示如何创建和添加标签

标签: java swing label coordinates mouse


【解决方案1】:
  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();
            }
        });
    }
}

【讨论】:

  • 感谢您的回答,如果您能给我看一个示例代码就太好了,我真的是编程新手
  • @beginner_T: 示例代码已发布,但以后请不要索要代码,因为您应该始终先尝试解决它——包括将 JLabel 添加到 JPanel 和您的尝试设置其文本。我们最好使用您的代码,而不是我的。
  • 我编辑了我的代码以及我如何尝试它,但它仍然无法正常工作。我知道使用paintComponent 更好,但我们是这样学习的(idk 为什么),我猜我们应该按照我们所学的那样进行测试,即使这没有意义。
  • @beginner_T:您不想将所有垃圾添加到 JFrame 中,因为 JFrame 使用 BorderLayout,因此您正在使用最新内容覆盖先前添加的内容。像上面那样将 JLabel 添加到绘图 JPanel。 JPanel 使用 FlowLayout,因此标签将添加到中间的顶部。运行我的代码,看看我的意思。
【解决方案2】:

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

static JLabel label = new JLabel();

在你的主要方法中

frame.add(label);

在你的 mouseMoved 方法中你可以这样写:

label.setText(currentPoint.toString());

【讨论】:

  • 我试过了,但它在 frame.add(label); 处显示错误它说:无法对非静态字段标签进行静态引用
  • @beginner_T 这是你的下一个编程挑战,快速搜索 SO,这是初学者的常见错误,并且被问了很多。因此,当遇到问题时,请先尝试看看其他人是否也遇到过问题;)
  • 我已经更新了我的回复。此外,您应该在您的绘画方法开始时调用 super.paint(g)。现在会发生的是,面板在绘制之前不会被清除,并且您会得到一系列线条。
  • 非常感谢您的帮助,现在错误消失了......但标签仍然没有显示?! :(
  • 您想在调用 lframe.setContentPane 后添加标签。现在 JPanel 正在覆盖标签。
猜你喜欢
  • 1970-01-01
  • 2014-02-06
  • 2012-07-25
  • 2013-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多