Java用swing窗体写2个可控制移动的按钮,鼠标点击哪个键盘上下左右移动哪个,在线等,谢谢了
2个回答
展开全部
代码还可以优化,你自己修改吧!代码有点小问题,运行后,按钮2会全屏,你按键盘方向键就会好了!这个问题你自己想办法修改吧。不过可以实现你要的功能。
public class PaintovalFrame extends JFrame {
private static JButton objT1;
private static JButton objT2;
public PaintovalFrame() {
setSize(500,500);
objT1 = new JButton("1");
objT1.setBounds(120, 40, 50, 40);
add(objT1);
objT2 = new JButton("2");
objT2.setBounds(20, 40, 50, 40);
add(objT2);
objT1.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_UP){
objT1.setBounds(objT1.getX(), objT1.getY() - 10, 50, 40);
} else if(e.getKeyCode()==e.VK_DOWN){
objT1.setBounds(objT1.getX(), objT1.getY() + 10, 50, 40);
} else if(e.getKeyCode()==e.VK_LEFT){
objT1.setBounds(objT1.getX() - 10, objT1.getY(), 50, 40);
} else if(e.getKeyCode()==e.VK_RIGHT){
objT1.setBounds(objT1.getX() + 10, objT1.getY(), 50, 40);
}
}
});
objT2.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_UP){
objT2.setBounds(objT2.getX(), objT2.getY() - 10, 50, 40);
} else if(e.getKeyCode()==e.VK_DOWN){
objT2.setBounds(objT2.getX(), objT2.getY() + 10, 50, 40);
} else if(e.getKeyCode()==e.VK_LEFT){
objT2.setBounds(objT2.getX() - 10, objT2.getY(), 50, 40);
} else if(e.getKeyCode()==e.VK_RIGHT){
objT2.setBounds(objT2.getX() + 10, objT2.getY(), 50, 40);
}
}
});
}
public static void main(String[] args) {
PaintovalFrame objPane = new PaintovalFrame();
objPane.setVisible(true);
}
}
public class PaintovalFrame extends JFrame {
private static JButton objT1;
private static JButton objT2;
public PaintovalFrame() {
setSize(500,500);
objT1 = new JButton("1");
objT1.setBounds(120, 40, 50, 40);
add(objT1);
objT2 = new JButton("2");
objT2.setBounds(20, 40, 50, 40);
add(objT2);
objT1.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_UP){
objT1.setBounds(objT1.getX(), objT1.getY() - 10, 50, 40);
} else if(e.getKeyCode()==e.VK_DOWN){
objT1.setBounds(objT1.getX(), objT1.getY() + 10, 50, 40);
} else if(e.getKeyCode()==e.VK_LEFT){
objT1.setBounds(objT1.getX() - 10, objT1.getY(), 50, 40);
} else if(e.getKeyCode()==e.VK_RIGHT){
objT1.setBounds(objT1.getX() + 10, objT1.getY(), 50, 40);
}
}
});
objT2.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_UP){
objT2.setBounds(objT2.getX(), objT2.getY() - 10, 50, 40);
} else if(e.getKeyCode()==e.VK_DOWN){
objT2.setBounds(objT2.getX(), objT2.getY() + 10, 50, 40);
} else if(e.getKeyCode()==e.VK_LEFT){
objT2.setBounds(objT2.getX() - 10, objT2.getY(), 50, 40);
} else if(e.getKeyCode()==e.VK_RIGHT){
objT2.setBounds(objT2.getX() + 10, objT2.getY(), 50, 40);
}
}
});
}
public static void main(String[] args) {
PaintovalFrame objPane = new PaintovalFrame();
objPane.setVisible(true);
}
}
2013-12-31
展开全部
点击就是获得焦点。
只要两个按钮都监听一个键盘事件就可以了。
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class JButtonMoveFrame extends JFrame {
private JButton objT1;
private JButton objT2;
public JButtonMoveFrame() {
super("JButtonMoveFrame");
this.setSize(500,500);
this.setLayout(null);
objT1 = new JButton("1");
objT1.setBounds(120, 40, 50, 40);
objT2 = new JButton("2");
objT2.setBounds(20, 40, 50, 40);
this.add(objT1);
this.add(objT2);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
KeyAdapter kl = new KeyAdapter(){
public void keyPressed(KeyEvent e){
JButton btn=(JButton)e.getSource();
if(e.getKeyCode()==e.VK_UP){
btn.setBounds(btn.getX(), btn.getY() - 10, 50, 40);
} else if(e.getKeyCode()==e.VK_DOWN){
btn.setBounds(btn.getX(), btn.getY() + 10, 50, 40);
} else if(e.getKeyCode()==e.VK_LEFT){
btn.setBounds(btn.getX() - 10, btn.getY(), 50, 40);
} else if(e.getKeyCode()==e.VK_RIGHT){
btn.setBounds(btn.getX() + 10, btn.getY(), 50, 40);
}
}
};
objT1.addKeyListener(kl);
objT2.addKeyListener(kl);
}
public static void main(String[] args) {
new JButtonMoveFrame();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询