在JFrame Java上移动按钮

时间:2017-12-06 15:47:01

标签: java button jframe mouselistener

我有一个任务要做一个应用程序,它将执行以下操作: 如果我移动鼠标,坐标应显示在状态栏上 如果单击鼠标,则JPanel上唯一的一个按钮应移动到click的坐标 所以问题是,当我点击鼠标时 - 它很好,按钮移动到coord的点击,但当我开始移动鼠标按钮回到原始位置

public class Window extends JFrame {
    private JLabel statusBar;
    private JPanel mainPanel, statusBarPanel;
    JButton button;
    public Window()
    {
        super("Window");
        setSize(400,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainPanel=new JPanel();
        statusBarPanel = new JPanel();
        statusBar=new JLabel("Coords: ");
        add(statusBarPanel, BorderLayout.SOUTH);
        add(mainPanel,BorderLayout.CENTER);
        mainPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
        statusBarPanel.add(statusBar,BorderLayout.CENTER);
        button = new JButton("Default text");
        mainPanel.add(button);
        MyMouseListener myMouseListener=new MyMouseListener();
        mainPanel.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                super.mouseMoved(e);
                statusBar.setText("Coords: ("+e.getX()+":"+e.getY()+")");
            }
        });
        mainPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                button.setLocation(e.getX()-button.getWidth()/2,e.getY()-button.getHeight()/2);
            }
        });
        mainPanel.setFocusable(true);
        setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:1)

这是您不希望面板拥有布局管理器的极少数情况之一,因为您需要绝对定位。

JPanel有一个默认的布局管理器FlowLayout,您对setLocation的调用只会产生临时效果,直到面板重新验证其内容并将其放置在应有的位置最初。

请参阅以下带注释的示例,它应该为您提供一般概念:

public class Window extends JFrame {
    private final JLabel statusBar;
    private final JPanel mainPanel, statusBarPanel;
    JButton button;

    public Window() {
        super("Window");
        setSize(400, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainPanel = new JPanel();
        mainPanel.setLayout(null);// no layout for absolute positioning
        statusBarPanel = new JPanel();
        statusBar = new JLabel("Coords: ");
        add(statusBarPanel, BorderLayout.SOUTH);
        add(mainPanel, BorderLayout.CENTER);
        mainPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
        statusBarPanel.add(statusBar, BorderLayout.CENTER);
        button = new JButton("Default text");

        // place the button "manually"
        button.setBounds((int) (400 - button.getPreferredSize().getWidth()) / 2, 0,
                (int) button.getPreferredSize().getWidth(),
                (int) button.getPreferredSize().getHeight());
        mainPanel.add(button);

        mainPanel.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(final MouseEvent e) {
                super.mouseMoved(e);
                statusBar.setText("Coords: (" + e.getX() + ":" + e.getY() + ")");
            }
        });
        mainPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(final MouseEvent e) {
                super.mouseClicked(e);
                button.setLocation((int) (e.getX() - button.getPreferredSize().getWidth() / 2),
                        (int) (e.getY() - button.getPreferredSize().getHeight() / 2));


            }
        });
        mainPanel.setFocusable(true);
        setVisible(true);
    }

}