java 我写的这个代码 圆为什么不能移动啊
packageUnit16;importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclass...
package Unit16;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Exercise16_3 extends JFrame{
private JButton jbtRight = new JButton("Right");
private JButton jbtLeft = new JButton("Left");
private JButton jbtUp = new JButton("Up");
private JButton jbDwon = new JButton("Dwon");
public Exercise16_3(){
setLayout(new BorderLayout());
JPanel p = new JPanel(new FlowLayout());
p.add(jbtUp);
p.add(jbDwon);
p.add(jbtLeft);
p.add(jbtRight);
add(BorderLayout.CENTER,new Ball());
add(BorderLayout.SOUTH,p);
Ball listener = new Ball();
jbtUp.addActionListener(listener);
jbDwon.addActionListener(listener);
jbtLeft.addActionListener(listener);
jbtRight.addActionListener(listener);
listener.y += 100;
}
public static void main(String[] args) {
Exercise16_3 frame = new Exercise16_3();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class Ball extends JPanel implements ActionListener{
private int radius = 5;
private int x = 100;
private int y = 100;
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawOval(x, y, 50, 50);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == jbtUp){
System.out.println("dwqdqdw");
y -= 20;
System.out.println(y);
repaint();
}else if(e.getSource() == jbDwon){
y += 5;
repaint();
}else if (e.getSource() == jbtLeft){
x -= 5;
repaint();
}else if(e.getSource() == jbtRight){
x += 5;
repaint();
}
}
}
} 展开
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Exercise16_3 extends JFrame{
private JButton jbtRight = new JButton("Right");
private JButton jbtLeft = new JButton("Left");
private JButton jbtUp = new JButton("Up");
private JButton jbDwon = new JButton("Dwon");
public Exercise16_3(){
setLayout(new BorderLayout());
JPanel p = new JPanel(new FlowLayout());
p.add(jbtUp);
p.add(jbDwon);
p.add(jbtLeft);
p.add(jbtRight);
add(BorderLayout.CENTER,new Ball());
add(BorderLayout.SOUTH,p);
Ball listener = new Ball();
jbtUp.addActionListener(listener);
jbDwon.addActionListener(listener);
jbtLeft.addActionListener(listener);
jbtRight.addActionListener(listener);
listener.y += 100;
}
public static void main(String[] args) {
Exercise16_3 frame = new Exercise16_3();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class Ball extends JPanel implements ActionListener{
private int radius = 5;
private int x = 100;
private int y = 100;
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawOval(x, y, 50, 50);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == jbtUp){
System.out.println("dwqdqdw");
y -= 20;
System.out.println(y);
repaint();
}else if(e.getSource() == jbDwon){
y += 5;
repaint();
}else if (e.getSource() == jbtLeft){
x -= 5;
repaint();
}else if(e.getSource() == jbtRight){
x += 5;
repaint();
}
}
}
} 展开
展开全部
//用了两个Ball 所以不行了 修改了一下有错误的地方,你看到就明白了为什么错了
public Exercise16_3() {
setLayout(new BorderLayout());
JPanel p = new JPanel(new FlowLayout());
p.add(jbtUp);
p.add(jbDwon);
p.add(jbtLeft);
p.add(jbtRight);
Ball listener = new Ball();
add(BorderLayout.CENTER, listener);
add(BorderLayout.SOUTH, p);
jbtUp.addActionListener(listener);
jbDwon.addActionListener(listener);
jbtLeft.addActionListener(listener);
jbtRight.addActionListener(listener);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询