学swing写代码遇到的问题, 大家可以帮忙看看吗,谢啦
用UtralEdit写的 编译通过了, 但是运行不出效果
 
预想的效果是画一个小球, 受到四个Button的控制, 进行上下左右的移动
可是.....
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyExercise16_3 extends JFrame {
	private JButton jbt1, jbt2, jbt3, jbt4;
	
	public MyExercise16_3 () {
		setLayout(new BorderLayout());
		
	
		JPanel p1 = new JPanel(new FlowLayout());
		BallDisplay p2 = new BallDisplay();
		ButtonListener listener = new ButtonListener();
		
		JButton jbt1 = new JButton("Left");
		JButton jbt2 = new JButton("Right");
		JButton jbt3 = new JButton("Up");
		JButton jbt4 = new JButton("Down");
		
		p1.add(jbt1);
		p1.add(jbt2);
		p1.add(jbt3);
		p1.add(jbt4);
		
		add(p1, BorderLayout.CENTER);
		add(p2, BorderLayout.SOUTH);
		
		jbt1.addActionListener(listener);
		jbt2.addActionListener(listener);
		jbt3.addActionListener(listener);
		jbt4.addActionListener(listener);
		
		
	}
	
	public static void main(String[] args) {
		MyExercise16_3 frame = new MyExercise16_3();
		
		frame.setTitle("MyExercise16_3");
		frame.setSize(200, 300);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	class BallDisplay extends JPanel {
		private int radius = 5; 
		private int xCoordinate = 50;
		private int yCoordinate = 50;
		
		public BallDisplay (){
			Timer timer = new Timer(1000, new ButtonListener());
			
			timer.start();
		}
		
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.drawOval(xCoordinate, yCoordinate, 2 * radius, 2 * radius );
		}
		
		public void left () {
			xCoordinate -= 5;
			
		}
		
		public void right () {
			xCoordinate += 5;
			
		}
		
		public void up () {
			yCoordinate -= 5;
			
		}
		
		public void down () {
			yCoordinate += 5;
			
		}
	}
	
	class ButtonListener implements ActionListener {
		
		BallDisplay ball = new BallDisplay();
		
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == jbt1) {
				ball.left();
				repaint();
			}
				
			else if (e.getSource() == jbt2) {
				ball.right();
				repaint();
			}
				
			else if (e.getSource() == jbt3) {
				ball.up();
				repaint();
			}
				
			else if (e.getSource() == jbt4) {
				ball.down();
				repaint();
			}
		} 
	}
}
------解决方案--------------------可是什么?遇到什么问题了?
------解决方案--------------------你的BallDisplay的构造函数中new了一个ButtonListener实例,
而在ButtonListener实例中,又创建了一个BallDisplay实例,
在创建这个BallDisplay实例时(参见构造函数),又new了一个ButtonListener实例,
在new了ButtonListener实例中,又创建了一个BallDisplay实例,
......
这样不断循环,最终导致堆栈溢出了。
具体逻辑我没看,所以你自己想修改方法吧。