显示鼠标坐标的标签

时间:2016-02-10 22:09:05

标签: java swing label coordinates mouse

在大学时,我们开始进行Java编程,并完成编写程序的任务,该程序在鼠标当前所在的位置绘制垂直和水平线。我们还应该添加一个显示鼠标坐标的Label。我得到的绘图工作,但当我尝试添加标签时,它不会显示?我开始使用测试标签,但即使这样也没有显示在框架内。有人能帮我吗?

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();
}

}

2 个答案:

答案 0 :(得分:2)

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

    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();
                }
            });
        }
    }
    

答案 1 :(得分:0)

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

static JLabel label = new JLabel();

在你的主要方法

frame.add(label);

在你的mouseMoved方法中,你会把它放在:

label.setText(currentPoint.toString());