我的代码应该显示与enitre窗口相关的鼠标坐标,但我使用的方法只给出每个单独组件的坐标。如何获得整个窗口和所有组件的坐标?
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();
}
}
}
}
答案 0 :(得分:2)
您可以使用SwingUtilities.convertPoint。在mouseMoved()方法中尝试这个:
Point p = SwingUtilities.convertPoint(e.getComponent(),e.getPoint(), 的getContentPane());
根据需要更新目标组件,我使用getContentPane()转换为frame的contentPane的坐标。
答案 1 :(得分:2)
我希望这段代码片段返回JFrame的坐标,有沮丧得到JFrame,因为是扩展
public void mouseMoved(MouseEvent e) {
Container container = this.getContentPane();
Point p = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), container);
System.out.println(p);
System.out.println(p.x);
System.out.println(p.y);
/*public static Point convertPoint(Component source, int x, int y, Component destination)
Convert the point (x,y) in source coordinate system to destination coordinate system.
If source is null, (x,y) is assumed to be in destination's root component coordinate
system. If destination is null, (x,y) will be converted to source's root component
coordinate system. If both source and destination are null, return (x,y) without any conversion.*/
xpos = e.getX();
ypos = e.getY();
xf.setText("" + xpos);
yf.setText("" + ypos);
}