动态面板



import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Fu extends JFrame{
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JButton hardware;
int x1,y1;
int x2,y2;
boolean row;
 public Fu(){
  y2=300;
     properties();
     discription(x1,y1,p1);
     discription(x2,y2,p2);
     setTitle("性能道具");
     setSize(450,329);
     setResizable(false);
     setVisible(true);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 private void discription(int x,int y,JPanel p){
  add(p);
  p.setBounds(x,y,300,300);
     p.setBorder(BorderFactory.createLineBorder(Color.blue,5));
 }
 private void properties(){
     setLayout(null);
     hardware=new JButton("硬件五金");
     add(hardware);
     hardware.setBounds(310,200,120,30);
     hardware.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             new Thread(new Runnable(){//定义一个线程匿名内部类
                 public void run(){
                    while(!row){      
                       if(y1==-300){
                           y1=300;
                       }
                       if(y2==-300){
                           y2=300;
                       }
                       y1--;
                       y2--;
                       p1.setLocation(x1,y1);//重新定位
                       p2.setLocation(x2,y2);//重新定位
                    try {
                       Thread.sleep(10);//移动延迟时间,毫秒数
                       }
                    catch (Exception font){
                    font.printStackTrace();
                    } 
                 }
             }
          }).start();
       } 
     });
  }
 public static void main(String[] args){
     new Fu();
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值