动态面板

来源:互联网 发布:.sit域名 备案 编辑:程序博客网 时间:2024/04/30 06:42


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();
 }
}

原创粉丝点击