JFrame 未出现在鼠标单击位置(包括 SSCCE)
我试图让 JFrame 出现在 mousePressed Location 上,但我一直失败,而且很烦人:( 有什么想法不起作用吗?
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SSCCE
{
@SuppressWarnings("static-access")
public static void getInputData()
{
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(frame.DISPOSE_ON_CLOSE);
JLabel emptyLabel = new JLabel("Test");
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
frame.setSize(new Dimension(375, 100));
MouseAdapter ml = new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent me)
{
frame.setLocation(me.getX(), me.getY());
}
@Override
public void mouseDragged(MouseEvent me)
{
frame.setLocation(me.getX(), me.getY());
}
};
frame.getContentPane().addMouseListener(ml);
frame.getContentPane().addMouseMotionListener(ml);
frame.setVisible(true);
}
public static void main(String args[])
{
JFrame test = new JFrame();
JButton but = new JButton("Click me");
but.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
getInputData();
}
});
test.getContentPane().add(but, BorderLayout.CENTER);
test.setSize(500, 500);
test.setVisible(true);
}
}
I am trying to make a JFrame appear on mousePressed Location but I keep failing and it get's annoying :( Any ideas what isn't working?
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SSCCE
{
@SuppressWarnings("static-access")
public static void getInputData()
{
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(frame.DISPOSE_ON_CLOSE);
JLabel emptyLabel = new JLabel("Test");
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
frame.setSize(new Dimension(375, 100));
MouseAdapter ml = new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent me)
{
frame.setLocation(me.getX(), me.getY());
}
@Override
public void mouseDragged(MouseEvent me)
{
frame.setLocation(me.getX(), me.getY());
}
};
frame.getContentPane().addMouseListener(ml);
frame.getContentPane().addMouseMotionListener(ml);
frame.setVisible(true);
}
public static void main(String args[])
{
JFrame test = new JFrame();
JButton but = new JButton("Click me");
but.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
getInputData();
}
});
test.getContentPane().add(but, BorderLayout.CENTER);
test.setSize(500, 500);
test.setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
SwingUtilities
方法convertPointToScreen()
和convertPointFromScreen()
用于转换MouseEvent
坐标。附录:或者,计算
getLocationOnScreen()
,即“组件在屏幕坐标空间中的左上角”。附录:要相对于原始鼠标单击定位新框架,请向父框架添加鼠标侦听器而不是按钮;使用坐标来定位新框架,如下所示。
之前,
Use the
SwingUtilities
methodsconvertPointToScreen()
andconvertPointFromScreen()
to transform theMouseEvent
coordinates.Addendum: Alternatively, calculate the offset from
getLocationOnScreen()
, which is "the component's top-left corner in the screen's coordinate space."Addendum: To position the new frame relative to the original mouse click, add a mouse listener to the parent frame instead of a button; use the coordinates to position the new frame, as shown below.
Previously,