如何获得JButton的(x,y)坐标

时间:2013-10-27 05:00:01

标签: java swing location jbutton point

是否有一种方法可以获得类似于pt.distance()方法的Jbutton的(x,y)格式坐标。 jbutton使用setLayout(null)和setBounds(x,y,0,0)。 我如何比较pt.distance()和Jbutton(x,y)的结果?

最后,如何计算(x,y)?

 Point pt = evt.getPoint();
    double u = pt.distance((double)x,(double)y);
    double k = st1.getAlignmentX();
    double p = st1.getAlignmentY();
    if(u > ){ // u has to be measured to (x,y) value of jbutton
    tim.setDelay(500);
    }
    if(u < ){
    tim.setDelay(100);
    }

3 个答案:

答案 0 :(得分:2)

Howabout getLocation,它返回其父组件上组件的坐标,或getLocationOnScreen,它返回显示器上组件的坐标?


关于如何计算x和y的第二个问题,我不确定'计算'是什么意思。坐标将相对于某些东西。通常,父组件(如JPanel所在的JButton)或屏幕上的某个位置(例如getLocation将返回JFrame)。< / p>

Point.distance这样的方法会减去两个坐标的x和y值并告诉你差异。这只是基本几何。

例如,这是一个方法,它将返回一个点距JButton中心的距离:

public static double getDistance(Point point, JComponent comp) {

    Point loc = comp.getLocation();

    loc.x += comp.getWidth() / 2;
    loc.y += comp.getHeight() / 2;

    double xdif = Math.abs(loc.x - point.x);
    double ydif = Math.abs(loc.y - point.y);

    return Math.sqrt((xdif * xdif) + (ydif * ydif));
}

这会将三角形的斜边作为度量以像素为单位返回,这意味着如果您给出的点(如光标坐标)位于对角线上,它将为您提供有用的距离。

Point.distance会做类似的事情。


我注意到我的这个旧答案得到了不少观点,所以这里有一个更好的方法来做上述事情(但并没有真正展示数学):

public static double distance(Point p, JComponent comp) {
    Point2D.Float center =
        // note: use (0, 0) instead of (getX(), getY())
        // if the Point 'p' is in the coordinates of 'comp'
        // instead of the parent of 'comp'
        new Point2D.Float(comp.getX(), comp.getY());

    center.x += comp.getWidth() / 2f;
    center.y += comp.getHeight() / 2f;

    return center.distance(p);
}

这是一个在Swing程序中显示这种几何的简单示例:

distance example

这会在鼠标光标所在的位置绘制一条线,并显示该线的长度(从JPanel的中心到光标的距离)。

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

class DistanceExample implements Runnable {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new DistanceExample());
    }

    @Override
    public void run() {
        JLabel distanceLabel = new JLabel("--");
        MousePanel clickPanel = new MousePanel();

        Listener listener =
            new Listener(distanceLabel, clickPanel);
        clickPanel.addMouseListener(listener);
        clickPanel.addMouseMotionListener(listener);

        JPanel content = new JPanel(new BorderLayout());
        content.setBackground(Color.white);
        content.add(distanceLabel, BorderLayout.NORTH);
        content.add(clickPanel, BorderLayout.CENTER);

        JFrame frame = new JFrame();
        frame.setContentPane(content);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    static class MousePanel extends JPanel {
        Point2D.Float mousePos;

        MousePanel() {
            setOpaque(false);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            if (mousePos != null) {
                g.setColor(Color.red);
                Point2D.Float center = centerOf(this);
                g.drawLine(Math.round(center.x),
                           Math.round(center.y),
                           Math.round(mousePos.x),
                           Math.round(mousePos.y));
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(100, 100);
        }
    }

    static class Listener extends MouseAdapter {
        JLabel distanceLabel;
        MousePanel mousePanel;

        Listener(JLabel distanceLabel, MousePanel mousePanel) {
            this.distanceLabel = distanceLabel;
            this.mousePanel = mousePanel;
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            Point2D.Float mousePos =
                new Point2D.Float(e.getX(), e.getY());

            mousePanel.mousePos = mousePos;
            mousePanel.repaint();

            double dist = distance(mousePos, mousePanel);

            distanceLabel.setText(String.format("%.2f", dist));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            mousePanel.mousePos = null;
            mousePanel.repaint();

            distanceLabel.setText("--");
        }
    }

    static Point2D.Float centerOf(JComponent comp) {
        Point2D.Float center =
            new Point2D.Float((comp.getWidth() / 2f),
                              (comp.getHeight() / 2f));
        return center;
    }

    static double distance(Point2D p, JComponent comp) {
        return centerOf(comp).distance(p);
    }
}

答案 1 :(得分:1)

这样的东西?:

JButton b = new JButton();
b.getAlignmentX();
b.getAlignmentY();

您也可以使用:

Rectangle r = b.getBounds(); // The bounds specify this component's width, height, 
                             // and location relative to its parent.

答案 2 :(得分:1)

如果按pt.distance(),您指的是Point2D.distance()方法,那么您可以这样继续:

Point location = button.getLocation(); // where button is your JButton object
double distance = pt.distance(location); // where pt is your Point2D object

或者:

double distance = pt.distance(button.getX(), button.getY());

然后Point将包含按钮的x和y坐标。如果您不使用布局,则这些值将是您设置的值。但是如果你使用的是布局,那么父级的LayoutManager负责计算值。

响应您的修改: 我不明白你想做什么。在setLayout(null)上拨打JButton将无法设置按钮的坐标,只能设置按钮的坐标。我认为这是你想要实现的目标:

Point pt = evt.getPoint();
double distance = pt.distance(button);
int someLength = 100; // the distance away from the button the point has to be to decide the length of the delay    

if (distance < someLength) {
    tim.setDelay(500);
} else {
    tim.setDelay(100);
}