java 鼠标单击按钮控制图形移动

在窗口画个圆,下排有四个按钮分别控制圆的上下左右移动帮改下我的代码,改好加分~~importjava.awt.*;importjavax.swing.*;importja... 在窗口画个圆,下排有四个按钮分别控制圆的上下左右移动
帮改下我的代码,改好加分~~
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Exercise_8 extends JFrame implements ActionListener{

protected BallPanel panel1 = new BallPanel();

private JButton jbtLeft = new JButton("Left");
private JButton jbtRight = new JButton("Right");
private JButton jbtUp = new JButton("Up");
private JButton jbtDown = new JButton("Down");

public static void main (String[] args){

Exercise_8 frame = new Exercise_8();
frame.setTitle("Exercise 8");
frame.setLayout (new BorderLayout());
frame.setSize (600, 300);
frame.setVisible (true);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}

public Exercise_8(){

JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.add(jbtLeft);
panel2.add(jbtRight);
panel2.add(jbtUp);
panel2.add(jbtDown);

getContentPane().setLayout(new BorderLayout());
getContentPane().add(panel1, BorderLayout.CENTER);
getContentPane().add(panel2, BorderLayout.SOUTH);

jbtLeft.addActionListener(this);
jbtRight.addActionListener(this);
jbtUp.addActionListener(this);
jbtDown.addActionListener(this);
}

public void actionPerformed(ActionEvent e){
if (e.getSource() == jbtLeft){
panel1.moveLeft();
}else if (e.getSource() == jbtRight){
panel1.moveRight();
}else if (e.getSource() == jbtUp){
panel1.moveUp();
}else{
panel1.moveDown();
}
}
}

***********************************************************************

import java.awt.*;
import javax.swing.*;

public class BallPanel extends JPanel{

private int x = 100;
private int y = 100;
private int interval = 10;

public BallPanel(){
}

public int getX(){
return x;
}

public void setX(int x){
this.x = x;
repaint();
}

public int getY(){
return y;
}

public void setY(int y){
this.y = y;
repaint();
}

protected void paintComponent(Graphics g){
super.paintComponent(g);
g.fillOval(x, y, 10, 10);
}

public void moveLeft(){
x -= interval;
repaint();
}

public void moveRight(){
x += interval;
repaint();
}

public void moveUp(){
y -= interval;
repaint();
}

public void moveDown(){
y += interval;
repaint();
}
}
展开
 我来答
ceaning
2010-04-05 · TA获得超过183个赞
知道小有建树答主
回答量:131
采纳率:0%
帮助的人:91.8万
展开全部
这样写:
<html>
<head>
<style>.mystyle{
position:absolute;
left:50;
top:50;
}
</style>
<script language="JavaScript">
function move(x, y){
mypic.style.left = x;
mypic.style.top = y;
}
</script>
</head>
<body onMousemove="move(event.x, event.y)" >

<div class="mystyle" id="mypic">
<img src="fish.gif">
</div>
</body>
</html>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式