java 移动按钮_JAVA 按钮移动问题

展开全部

package com;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JToggleButton;

import javax.swing.Timer;

import javax.swing.UIManager;

public class Test implements ActionListener

{

private static JButton obj;

private static JToggleButton switcher;

private static JCheckBox h, v, anim;

private static Timer timer = new Timer(10, new Test());

private static final int TO_X = 300, TO_Y = 300, FROM_X = 100, FROM_Y =100;

private static int x = FROM_X, y = FROM_Y; //记录位置变化

private static int time; //记录执行时间流逝

private static final float FULL_TIME = 3000; //动画执行3秒中

public static void main(String[] args) throws Exception

{

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //window外观

JFrame jf = new JFrame();

jf.add(createCanvas()); //动画场地e68a8462616964757a686964616f31333335313238

jf.add(createOptionBtns(),BorderLayout.SOUTH); //下方按钮区域

jf.setSize(800, 400);

jf.setLocationRelativeTo(null); //居中显示

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jf.setVisible(true);

}

private static final JComponent createCanvas()

{

JPanel jp = new JPanel(null);

jp.setBackground(Color.WHITE);

obj = new JButton("来抓我啊!");

obj.setBounds(100, 100, 100, 30);

jp.add(obj);

return jp;

}

private static final JComponent createOptionBtns()

{

JPanel jp = new JPanel();

h = new JCheckBox("水平滑动");

v = new JCheckBox("垂直滑动");

anim = new JCheckBox("咱来个动画效果咋样");

switcher = new JToggleButton("移动");

switcher.addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

if(!h.isSelected() && !v.isSelected())

{

JOptionPane.showMessageDialog(null, "水平滑动和垂直滑动至少选一个!");

switcher.setSelected(false);

return;

}

JToggleButton jtb = (JToggleButton)e.getSource();

if(jtb.isSelected())

{

jtb.setText("暂停");

timer.start();

}else{

jtb.setText("继续");

timer.stop();

}

}

});

jp.add(switcher);

jp.add(h);

jp.add(v);

jp.add(anim);

return jp;

}

@Override

public void actionPerformed(ActionEvent e)

{

time += 10;

boolean hisSelected = h.isSelected();

boolean visSelected = v.isSelected();

if(hisSelected)

{

if(x

{

x = (int)(FROM_X + (TO_X - FROM_X) * getNewValue(time / FULL_TIME));

}

}

if(visSelected)

{

if(y

y = (int)(FROM_Y + (TO_Y - FROM_Y) * getNewValue(time / FULL_TIME));

}

obj.setLocation(x, y);

if((!visSelected && hisSelected && x>= TO_X)

||(!hisSelected && visSelected && y>= TO_Y)

||(!hisSelected && !visSelected)

||(hisSelected && x>= TO_X && visSelected && y>= TO_Y))

{

timer.stop();

switcher.setText("移动");

switcher.setSelected(false);

try

{

Thread.sleep(500);

} catch (InterruptedException e1)

{

e1.printStackTrace();

}

x = FROM_X;

y = FROM_Y;

time = 0;

obj.setLocation(x, y);

}

}

private float getNewValue(float t)

{

if(anim.isSelected())

{

return getInterpolation(t);

}else{

return t;

}

}

private static float bounce(float t)

{

return t * t * 8.0f;

}

public float getInterpolation(float t)

{

t *= 1.1226f;

if (t < 0.3535f) return bounce(t);

else if (t < 0.7408f) return bounce(t - 0.54719f) + 0.7f;

else if (t < 0.9644f) return bounce(t - 0.8526f) + 0.9f;

else return bounce(t - 1.0435f) + 0.95f;

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值