用button和timer控制label右移

用button和timer控制label右移,第1张

1、在界面中添加一个button和一个label,将button的clicked信号与槽函数进行连接。

2、在槽函数中,使用timer控制label的移动。可以使用QPropertyAnimation类来实现动画效果。

3、在button的clicked信号触发时,启动timer,开始移动label。可以使用QTimer类来实现定时器功能。

java swing中JLabel中添加JButton只需要使用JLabel的add方法就可以添加,实例如下:

package components

 

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.awt.event.KeyEvent

import java.net.URL

 

import javax.swing.ImageIcon

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JPanel

import javax.swing.SwingUtilities

 

public class JButtonTest extends JPanel implements ActionListener {

 

   

    private static final long serialVersionUID = 1L

 

    JButton button1,button2,button3

    public JButtonTest() {

       //创建button上的图标

       ImageIcon imageIcon1 = createImage("images/right.gif")

       ImageIcon imageIcon2 = createImage("images/middle.gif")

       ImageIcon imageIcon3 = createImage("images/left.gif")

      

       //创建Button,并设置Button的图标

       button1 = new JButton("中间按钮不可用",imageIcon1)

       //设置Button的文本位置

       button1.setVerticalTextPosition(JButton.CENTER)

       button1.setHorizontalTextPosition(JButton.LEADING)

       //设置Button的快捷键

       button1.setMnemonic(KeyEvent.VK_D)

       //设置Button的反馈消息,消息处理者通过这个标记来辨别是哪个按钮被点击

       button1.setActionCommand("disable")

       //为Button添加监听者

       button1.addActionListener(this)

       //设置Button的提示信息

       button1.setToolTipText("点击此按键,此按键和中间按键变为不可用,右边按键变为可用")

       //将Button添加到panel中

       add(button1)

 }


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11439484.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存