⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adapter.java

📁 java经典的源代码 我非常喜欢这个源代码 对于编程很有好处
💻 JAVA
字号:
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
public class adapter extends JFrame{
  private JPanel contentPane;
  private int[] x=new int[40];
  private int[] y=new int[40];
  private String s[]=new String[40];
  JButton b[]=new JButton[40];
  JLabel label=new JLabel("点击选中,然后用←、↑、→、↓移动");
  public adapter(){
    super("事件适配器");
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(null);
    this.setSize(new Dimension(450, 200));
    contentPane.add(label);
    label.setBounds(0,0,250,30);
    int k1,k2,k3,k4;
    k1=k2=k3=k4=70;
    for(int i=0;i<40;i++){
      b[i]=new JButton(String.valueOf(i));
      if(i%3==0)
        b[i].setBackground(Color.blue);
      if(i%3==1)
        b[i].setBackground(Color.red);
      if(i%3==2)
        b[i].setBackground(Color.yellow);
      b[i].addKeyListener(new demo());
      b[i].addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        for(int i=0;i<40;i++){
          if(e.getSource()==b[i]){
            for(int j=0;j<40;j++)
              s[j]=b[i].getLabel();
            }
        }
      }
    });
      contentPane.add(b[i]);
      if(i%4==0){
        b[i].setBounds(k1,40,30,30);
        k1=k1+31;
      }
      else if(i%4==1){
        b[i].setBounds(k2,71,30,30);
        k2=k2+31;
      }
      else if(i%4==2){
        b[i].setBounds(k3,102,30,30);
        k3=k3+31;
      }
      else if(i%4==3){
        b[i].setBounds(k4,133,30,30);
        k4=k4+31;
      }
    }
    for(int i=0;i<40;i++){
      x[i]=b[i].getBounds().x;
      y[i]=b[i].getBounds().y;
    }
    show();
  }
  public static void main(String args[]){
    adapter app=new adapter();
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
  }
class demo extends KeyAdapter{
  public void keyPressed(KeyEvent e){
    if(e.getKeyCode()==KeyEvent.VK_UP){
      for(int i=0;i<40;i++){
        if(s[i].equals(String.valueOf(i))){
          y[i]=y[i]-2;
          if(y[i]<=0)
            y[i]=0;
          b[i].setLocation(x[i],y[i]);
        }
      }
    }
    else if(e.getKeyCode()==KeyEvent.VK_DOWN){
      for(int i=0;i<40;i++){
        if(s[i].equals(String.valueOf(i))){
          y[i]=y[i]+2;
          if(y[i]>=300)
            y[i]=300;
          b[i].setLocation(x[i],y[i]);
        }
      }
    }
    else if(e.getKeyCode()==KeyEvent.VK_LEFT){
      for(int i=0;i<40;i++){
        if(s[i].equals(String.valueOf(i))){
          x[i]=x[i]-2;
          if(x[i]<=0)
            x[i]=0;
          b[i].setLocation(x[i],y[i]);
        }
      }
    }
    else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
      for(int i=0;i<40;i++){
        if(s[i].equals(String.valueOf(i))){
          x[i]=x[i]+2;
          if(x[i]>=300)
            x[i]=300;
          b[i].setLocation(x[i],y[i]);
        }
      }
    }
   }
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -