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();
}
} 展开
帮改下我的代码,改好加分~~
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();
}
} 展开
1个回答
展开全部
这样写:
<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>
<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>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询