我的jpanel程序无法成功运行

fcy6dtqo  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(332)

这个程序运行时显示一个椭圆形的位置70,70和有一个开始按钮。在我点击开始按钮后,程序停止一段时间,椭圆向东南移动一个位置。它应该滚到另一个角落。
这是程序。。。。

package javaapplication1.pkg161;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Player {

    int x = 70;
    int y = 70;
    static JFrame f;

    public static void main(String args[]) {
        Player p = new Player();
        p.go();
    }

    public void go() {
        f = new JFrame("title");
        f.setSize(200, 200);
        f.setVisible(true);
        Window win = new Window();
        f.add(BorderLayout.CENTER, win);
        JButton b = new JButton("Start");
        f.add(BorderLayout.SOUTH, b);
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < 130; i++) {
                    win.repaint();
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e1) {
                        System.out.println("error");
                    }
                }
            }
        });
    }

    class Window extends JPanel {

        public void paintComponent(Graphics g) {
            x++;
            y++;
            g.fillOval(x, y, 100, 100);
        }

    }
}
798qvoo8

798qvoo81#

swing是一个单线程框架,这意味着您不能在事件调度线程的上下文中执行长时间运行的操作,否则您将阻止处理事件队列,并且您的程序将挂起。
首先看一下swing中的并发性以及如何使用swing计时器
你还违反了油漆连锁合同。看一下awt和swing中的绘画,以及如何在swing中完成绘画的更多细节

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Player {

    int x = 70;
    int y = 70;

    private Timer timer;
    Window win = new Window();

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                Player p = new Player();
                p.go();
            }
        });
    }

    public void go() {
        JFrame f = new JFrame("title");
        f.add(BorderLayout.CENTER, win);
        JButton b = new JButton("Start");
        f.add(BorderLayout.SOUTH, b);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

        timer = new Timer(40, new ActionListener() {
            private int counter = 0;
            @Override
            public void actionPerformed(ActionEvent e) {
                if (counter < 130) {
                    counter++;
                    win.updateLocation();
                    win.repaint();
                } else {
                    timer.stop();
                    counter = 0;
                }
            }
        });

        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!timer.isRunning()) {
                    timer.start();
                }
            }
        });
    }

    class Window extends JPanel {

        private int x = 70;
        private int y = 70;

        public void updateLocation() {
            x++;
            y++;
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(170+130, 170+130);
        }

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

    }
}

相关问题