мой код должен отображать координаты мыши, так как она относится к окну ввода, но метод, который я использовал, будет давать координаты только для каждого отдельного компонента.Как я могу получить координаты для всего окна и всех компонентов?
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();
}
}
}
}