JAVA 按钮移动问题
use JAVA Netbeans :) 展开
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()); //动画场地
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<TO_X)
{
x = (int)(FROM_X + (TO_X - FROM_X) * getNewValue(time / FULL_TIME));
}
}
if(visSelected)
{
if(y<TO_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;
}
}
怎么加线程呢?我是自学新手,代码什么的都会就是不知道怎么添加。能讲讲实际怎么操作吗?
多线程这一块内容挺多的,挺复杂的,最好找本书系统的看一看。
简单的来说在java中实现多线程有两种方式,一种是继承Thread类然后重写run方法,然后在需要运行的时候调用start()方法。另一种是实现Runnable接口,然后实现run方法,这种方式更灵活,我也比较推荐。
代码(实现Runnable接口的方式):
//首先让一个类实现Runnable接口
public class Test implements Runnable{
//实现run方法
public void run(){
//你需要这个线程做的事情
}
}
//然后通过实现了Runnable接口的类创建一个新的线程
Thread thread = new Thread(new Test());
//最后启动线程
thread.start();
然后系统就会分配CPU资源给这个线程来执行你run方法里面的代码。
至于挂起线程的话stop和resume容易导致死锁之类的情况现在已经不推荐使用了。
现在调度线程一般是用wait和notify方法,如果想让线程休眠一定的时间用sleep和interrupt方法也可以,关于进程调度这一块的内容就不是几句话能说的清了,自己去看看书吧。
JPanel中,设置JPanel 的位置,setBounds(50,50,100,100);试一下,我没有试过。