Java添加图片(标签)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class First extends JFrame implements KeyListener//定义主类并添加键盘监听
{
    private ImageIcon marryi=new ImageIcon("marry.jpg");//贴图
 private JLabel label=new JLabel(marry);//将图片加到标签中
 private JPanel panel=new JPanel();  
    public First()//构造方法
    {
    panel.setLayout(null);
    this.add(panel);
    panel.add(label);
    this.setBounds(100,100,500,500);
    label.setBounds(200, 300,170, 170);
    this.addKeyListener(this);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);//添加关闭
    this.setVisible(true);
    }
    public void ss(KeyEvent e)
    {
  int x1=label.getX();
        int y1=label.getY();
     if(e.getKeyCode()==KeyEvent.VK_UP)
     {
      label.setLocation(x1, y1-5);
     }
     else if(e.getKeyCode()==KeyEvent.VK_DOWN)
     {
      label.setLocation(x1, y1+5);
     }
     else if(e.getKeyCode()==KeyEvent.VK_LEFT)
     {
      label.setLocation(x1-5, y1);
     }
     else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
     {
      label.setLocation(x1+5, y1);
     }
    }
 public void keyPressed(KeyEvent e) {ss(e);}
 public void keyReleased(KeyEvent e) {}
 public void keyTyped(KeyEvent e) {}
 public static void main(String[] args) //主方法
    {
     new First();
    }
}

你可能感兴趣的:(java,String,null,Class,UP,import)