- package bbs.istak.org.tw;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import java.awt.Image;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JLabel;
- public class movecharetar extends JFrame {
- private JPanel contentPane;
- private static ImageIcon icon;
- private int x =97,y=10;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- movecharetar frame = new movecharetar();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public movecharetar() {
- icon = new ImageIcon(udicon.class.getResource("Actions-go-down-icon.png"));
- Image img = icon.getImage().getScaledInstance(75,102,java.awt.Image.SCALE_FAST );
- ImageIcon newicon = new ImageIcon(img);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
-
- JLabel label = new JLabel(newicon);
- label.setBounds(97, 10, 100, 100);
- contentPane.add(label);
- addKeyListener(new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent e) {
- if(e.getKeyCode()==e.VK_LEFT){
- x-=3;
- label.setIcon(turndir("Actions-go-previous-icon.png"));
- }
- if(e.getKeyCode()==e.VK_UP){
- y-=3;
- label.setIcon(turndir("Actions-go-up-icon.png"));
- }
- if(e.getKeyCode()==e.VK_RIGHT){
- x+=3;
- label.setIcon(turndir("Actions-go-next-icon.png"));
- }
- if(e.getKeyCode()==e.VK_DOWN){
- y+=3;
- label.setIcon(turndir("Actions-go-down-icon.png"));
- }
- label.setLocation(x, y);
- }
- });
-
- }
- public static ImageIcon turndir(String picname){
-
- icon = new ImageIcon(udicon.class.getResource(picname));
- Image img = icon.getImage().getScaledInstance(100,100,java.awt.Image.SCALE_FAST );
- ImageIcon diricon = new ImageIcon(img);
-
- return diricon;
- }
- }
複製代碼 |