Java 如何在JPanel中显示鼠标坐标?
我试图让下面的代码显示信息面板中的坐标,但它不工作。如何从SimpleFrameViewWidget的mouseListener获取要显示在信息面板中的坐标?我现在正在使用setText。。。谢谢Java 如何在JPanel中显示鼠标坐标?,java,swing,jframe,jpanel,mouseevent,Java,Swing,Jframe,Jpanel,Mouseevent,我试图让下面的代码显示信息面板中的坐标,但它不工作。如何从SimpleFrameViewWidget的mouseListener获取要显示在信息面板中的坐标?我现在正在使用setText。。。谢谢 public static void main(String[] args) throws IOException { Frame f = A7Helper.readFromURL("http://www.cs.unc.edu/~kmp/kmp.jpg"); f.setTitle(""
public static void main(String[] args) throws IOException {
Frame f = A7Helper.readFromURL("http://www.cs.unc.edu/~kmp/kmp.jpg");
f.setTitle("");
SimpleFrameViewWidget simple_widget = new SimpleFrameViewWidget(f);
JFrame main_frame = new JFrame();
main_frame.setTitle("Assignment 7 Pixel Inspector");
main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel top_panel = new JPanel();
top_panel.setLayout(new BorderLayout());
top_panel.add(simple_widget, BorderLayout.CENTER);
main_frame.setContentPane(top_panel);
JPanel info_panel = new JPanel();
info_panel.setLayout(new BorderLayout());
top_panel.add(info_panel, BorderLayout.SOUTH);
JLabel x_label = new JLabel("");
x_label.setHorizontalAlignment(SwingConstants.CENTER);
x_label.addMouseListener(simple_widget);
x_label.setText(simple_widget.getStringInfo());
info_panel.add(x_label);
main_frame.setContentPane(top_panel);
main_frame.pack();
main_frame.setVisible(true);
}
这是包含mouseStener和mouseClick的代码。提前谢谢
public class SimpleFrameViewWidget extends JPanel implements MouseListener {
private FrameView frame_view;
private String stringInfo;
public SimpleFrameViewWidget(Frame f) {
setLayout(new BorderLayout());
frame_view = new FrameView(f);
frame_view.addMouseListener(this);
add(frame_view, BorderLayout.CENTER);
JLabel title_label = new JLabel(f.getTitle());
add(title_label, BorderLayout.SOUTH);
}
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
stringInfo = "X: " + x + "Y: " + y;
}
public String getStringInfo(){
return stringInfo;
}
你可以。。。
将另一个鼠标侦听器注册到SimpleFrameViewWidget,该Widget只需将结果直接提供给info_panel或让info_panel为您完成所有操作….您还没有指定希望它的显示方式?为了更快地获得更好的帮助,请发布一个最小的完整且可验证的示例。Frame f=A7Helper.readFromURLhttp://www.cs.unc.edu/~kmp/kmp.jpg;这是一个奇怪的命名方法,用于返回帧!