该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
哪位能帮忙详细解答下吗,谢谢啦!import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestApp extends JFrame implements KeyListener {
public static JButton jbtn1;
public static JButton jbtn2;
int x, y;
public TestApp() {
x = y = 0;
this.setTitle("窗口");
this.setSize(400, 400);
this.setLayout(new FlowLayout());
jbtn1 = new JButton("按钮1");
jbtn2 = new JButton("按钮2");
jbtn1.addKeyListener(this);
this.getContentPane().add(jbtn1);
this.getContentPane().add(jbtn2);
this.setVisible(true);}
public static void main(String[] args)
{
TestApp ta = new TestApp();
}
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == 37)// 左向箭头
{
if (x <= 0)x = 0;
elsex -= 5;
jbtn1.setLocation(x, y);
}
if (e.getKeyCode() == 38)// 上向箭头
{
if (y <= 0)y = 0;
elsey -= 5;
jbtn1.setLocation(x, y);
}
if (e.getKeyCode() == 39)// 右向箭头
{
if (x >= 400)x = 400;
elsex += 5;
jbtn1.setLocation(x, y);
}
if (e.getKeyCode() == 40)// 下向箭头
{
if (y >= 400)y = 400;
elsey += 5;
jbtn1.setLocation(x, y);
}
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}