In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Swing how to use the arrow keys to move pictures, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
You can choose the direction of up, down, left, right, top left, bottom left, top right, bottom right, etc. When the picture moves to the edge of the window, it no longer moves.
KEY enumeration: the direction of movement based on the result of the keystroke
HORIZION enumeration: left and right arrow keys pressed on the keyboard (left, right)
VERTICAL enumeration: up and down arrow keys pressed on the keyboard (up and down)
STEP: the unit in which the picture moves in response to the button
How do I respond to two keys?
If a key does not pop up after it is pressed (the press event is issued) and the other key is pressed (the press event is issued), both keys are pressed at the same time.
Because the keystroke is recorded by the enumeration quantity of the program, when pressing the opposite direction key (not pressing one key at the same time, that is, pressing one key and then popping up and then pressing another), it is necessary to clear the direction record of the previous press (for example, press left first, then press right, and then clear the left record).
Package com.cnblogs.hyjiacan; import java.awt.Color; import java.awt.Point; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel Enum KEY {UP, RIGHT, DOWN, LEFT, RIGHT_UP, RIGHT_DOWN, LEFT_UP, LEFT_DOWN} enum HORIZION {LEFT, RIGHT} enum VERTICAL {UP, DOWN} public class MoveImage {JFrame win; Icon img; JLabel lb; static final int STEP = 2; KEY key; HORIZION hor; VERTICAL ver Public MoveImage () {hor = null; ver = null; win = new JFrame ("MoveImage"); win.setLayout (null); / / load picture img = new ImageIcon (getClass (). GetResource ("img.gif")); lb = new JLabel (); lb.setSize (img.getIconWidth (), img.getIconHeight ()) / / load picture lb.setIcon (img); win.setBounds (200,0400,300); / / press the event win.addKeyListener (new KeyAdapter () {/ / key value temporary quantity int k) in response to the key on the keyboard @ Override public void keyPressed (KeyEvent e) {k = e.getKeyCode (); switch (k) {case KeyEvent.VK_UP: ver = VERTICAL.UP; break Case KeyEvent.VK_DOWN: ver = VERTICAL.DOWN; break; case KeyEvent.VK_RIGHT: hor = HORIZION.RIGHT; break; case KeyEvent.VK_LEFT: hor = HORIZION.LEFT Break;} if (ver = = null) {if (hor = = HORIZION.RIGHT) {key = KEY.RIGHT;} else {key = KEY.LEFT }} else if (hor = = null) {if (ver = = VERTICAL.UP) {key = KEY.UP;} else {key = KEY.DOWN }} else {if (hor = = HORIZION.RIGHT) {if (ver = = VERTICAL.UP) {key = KEY.RIGHT_UP } else {key = KEY.RIGHT_DOWN;}} else {if (ver = = VERTICAL.UP) {key = KEY.LEFT_UP } else {key = KEY.LEFT_DOWN;} move (key) } @ Override public void keyReleased (KeyEvent e) {k = e.getKeyCode (); switch (k) {case KeyEvent.VK_UP: ver = null; break Case KeyEvent.VK_DOWN: ver = null; break; case KeyEvent.VK_RIGHT: hor = null; break; case KeyEvent.VK_LEFT: hor = null; break }); win.add (lb); win.getContentPane (). SetBackground (Color.WHITE); win.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); win.setVisible (true); win.validate () } / * move the picture, only deal with the up and down four direction keys, the rest do not deal with * * @ param keyCode * key value on the keyboard pressed * / void move (KEY key) {/ / write down the current position Point pos = lb.getLocation () Switch (key) {case UP:// up pos.y-= STEP; break; case RIGHT:// right pos.x + = STEP; break; case DOWN:// Down pos.y + = STEP; break Case LEFT:// left direction pos.x-= STEP; break; case RIGHT_UP:// upper right direction pos.x + = STEP; pos.y-= STEP; break; case RIGHT_DOWN:// lower right direction pos.x + = STEP Pos.y + = STEP; break; case LEFT_DOWN:// lower left direction pos.x-= STEP; pos.y + = STEP; break; case LEFT_UP:// upper left direction pos.x-= STEP; pos.y-= STEP; break } / / whether the if has been moved to the edge (pos.x > = win.getWidth ()-lb.getWidth () | pos.x
< 0 || pos.y >= win.getHeight ()-lb.getHeight () | | pos.y < 0) return; / / set the new location lb.setLocation (pos); lb.validate ();} public static void main (String [] args) {MoveImage m = new MoveImage ();}}
Remaining questions:
When pressing up and down or left and right at the same time, the picture will move in the direction specified by the direction key pressed behind (in theory, it should not move at this time).
When you press and hold the two arrow keys at the same time, the picture will move first, then pause, and then continue to move.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.