今天分享一个题吧,上课老师强调的会考的题
大致就是用到了swing可视化界面,两个btn.两个lab
两个线程 一个按下往右,一个按下往左
package java复习;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Process extends Thread{
JLabel ll;
JButton bb;
public Process(JLabel ll, JButton bb) {
super();
this.ll = ll;
this.bb = bb;
}
public void run() {
while(true) {
try {
if(Thread.currentThread().getName()=="向下") {
ll.setLocation(ll.getX(),ll.getY()+10);//向下
Thread.sleep(1000);
}
else {
ll.setLocation(ll.getX()+10, ll.getY());//向右
Thread.sleep(1000);
}
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String [] args) {
JFrame jFrame=new JFrame();
JLabel jLabel1=new JLabel("向下移标签");
JLabel jLabel2=new JLabel("向右移标签");
JButton jb1=new JButton("向下移按钮");
JButton jb2=new JButton("向右移标签");
jFrame.setSize(800, 1000);
jFrame.setLayout(new FlowLayout());
jFrame.setVisible(true);
jFrame.add(jb2);
jFrame.add(jb1);
jFrame.add(jLabel1);
jFrame.add(jLabel2);
Process t1=new Process(jLabel1, jb1);
t1.setName("向下");
Process t2=new Process(jLabel2, jb2);
t2.setName("向右");
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t1.start();
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
t2.start();
}
} );
}
}
我感觉老师上课讲的太麻烦了,我就用构造函数将两个串起来了
其实最好的是用javabean封装一下吧
希望能帮到你们
ps:马上要考试了,软工实践是真的难。。。头疼