在 JButton click 上移动一个空心圆圈

Move a hollow circle on JButton click

我必须创建一个使用按钮移动圆圈的程序,它不会移动。我已经尝试了很多东西,但恐怕我应该以不同的方式组织 classes,否则我会遗漏一些东西。这是在没有重叠条件的情况下移动圆圈的代码。

DrawCircle class:

public class DrawCircle extends JComponent {
Graphics e;

 @Override
 public void paintComponent(Graphics g) {
        g.drawOval(30,30,50,50);
        e=g;
    }

int moveSpeed = 10;
public void moveUp() {
    if(e.getClipBounds().getMinY() - moveSpeed >= 0){  // make sure the future location of the circle is within bounds of visual frame
        e.setClip( (int) Math. round(e.getClipBounds().getCenterX()),(int) Math. round( e.getClipBounds().getCenterY() - moveSpeed),(int) Math. round( e.getClipBounds().getX()),
            (int) Math. round(e.getClipBounds().getY() - moveSpeed));
    }
    repaint();
}

public void moveDown()
{
    if(e.getClipBounds().getMaxY() - moveSpeed <= 140){  // make sure the future location of the circle is within bounds of visual frame
        e.setClip((int) Math. round(e.getClipBounds().getCenterX()),(int) Math. round( e.getClipBounds().getCenterY() + moveSpeed) ,(int) Math. round( e.getClipBounds().getX()),
            (int) Math. round(e.getClipBounds().getY() + moveSpeed));
    }
    repaint();
}

public void moveRight ()
{
    e.setClip((int) Math. round(e.getClipBounds().getCenterX()-10) ,(int) Math. round( e.getClipBounds().getCenterY()), 
               (int) Math. round(e.getClipBounds().getX()-10), (int) Math. round(e.getClipBounds().getY())  ) ;
    repaint();
}

public void moveLeft()
{
    e.setClip(  (int) Math. round(e.getClipBounds().getCenterX()+10) ,(int) Math. round( e.getClipBounds().getCenterY()),
                (int) Math. round(e.getClipBounds().getX()+10 ),(int) Math. round( e.getClipBounds().getY()));
    repaint();
}
}

Main Class:

public class Main{

public static void main(String[] args) {
    final JButton button1;
    final JButton button2;
    final JButton button3;
    final JButton button4;
    JPanel panel;
    final JFrame frame=new JFrame();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,200);
    final DrawCircle c=new DrawCircle();
    frame.add(c);


     button1=new JButton(" Right ");
     button2=new JButton(" Left ");
     button3=new JButton(" Up ");
     button4=new JButton(" Down ");
     panel=new JPanel();
     panel.setLayout(new BorderLayout());
     panel.add(button1,BorderLayout.WEST);
     panel.add(button2,BorderLayout.EAST);
     panel.add(button3,BorderLayout.NORTH);
     panel.add(button4,BorderLayout.SOUTH);
     frame.add(panel,BorderLayout.EAST);


    class ListenerClass implements ActionListener {
     public void actionPerformed (ActionEvent e)
     {
         if (e.getSource()==button3)
         {
             c.moveUp();
         }
         else if (e.getSource()==button4)
         {
             c.moveDown();
         }
         else if (e.getSource()==button1)
         {
             c.moveRight();
         }
         else if (e.getSource()==button2)
         {
             c.moveLeft();
         }
    }
}

ListenerClass listen=new ListenerClass();
button1.addActionListener (listen);
button2.addActionListener (listen);
button3.addActionListener (listen);
button4.addActionListener (listen);
}

    }

这是我的一个旧示例程序。你只需要稍微调整一下。也许它会对你有所帮助

public class MainPanel extends JFrame {

    int x = 0;
    int y = 0;

    ContentPanel drawPanel = new ContentPanel();

    public static void main(String[] args) {
        MainPanel mainPanel = new MainPanel();       
    }
    
    public MainPanel() {
        
        setLayout(new BorderLayout());
        
        JButton button1 = new JButton();
            button1.setText("/\");
            button1.setPreferredSize(new Dimension(800, 50));
            button1.addActionListener((ActionEvent e) -> {
                y -= 10;
                drawPanel.repaint();
            });
        add(button1, BorderLayout.NORTH);
        JButton button2 = new JButton();
            button2.setText("<");
            button2.setPreferredSize(new Dimension(50, 800));
            button2.addActionListener((ActionEvent e) -> {
                x -= 10;
                drawPanel.repaint();
            });
        add(button2, BorderLayout.WEST);
        JButton button3 = new JButton();
            button3.setText(">");
            button3.setPreferredSize(new Dimension(50, 800));
            button3.addActionListener((ActionEvent e) -> {
                x += 10;
                drawPanel.repaint();
            });
        add(button3, BorderLayout.EAST);
        JButton button4 = new JButton();
            button4.setText("\/");
            button4.setPreferredSize(new Dimension(800, 50));
            button4.addActionListener((ActionEvent e) -> {
                y += 10;
                drawPanel.repaint();
            });
        add(button4, BorderLayout.SOUTH);
        
        add(drawPanel, BorderLayout.CENTER);

        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private class ContentPanel extends JPanel {

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.fillOval(x, y, 50, 50);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(800, 800);
        }
    }    
}