简体   繁体   English

如何使JButton更改JLabel中文本的位置

[英]how to make JButton changes the location of the text inside JLabel

how can i set JButton that changes the JLabel location on the JPanel when it pressed? 如何设置按下JPanel时更改JLabel上JLabel位置的JButton? how to implement the actionlistener for this on the JButton? 如何在JButton上为此实现actionlistener?

You want to add the action listener to the button first 您想先将动作监听器添加到按钮

button.addActionListener(new ButtonListener());

Next you want to create the custom class 接下来,您要创建自定义类

class ButtonListener implements ActionListener {
     public void actionPerformed(ActionEvent e) {
            if(e.getSource() == button) {
                 label.setLocation(xValue, yValue);
            }
     }
}

Change the xValue/yValue to the x and y values that you want the label to change to. 将xValue / yValue更改为您希望标签更改为的x和y值。

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM