Hello everyone, 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);
}
}