gpt4 book ai didi

java - 我的 JPanel 程序无法成功运行

转载 作者:行者123 更新时间:2023-11-29 05:07:21 25 4
gpt4 key购买 nike

该程序在运行时在位置 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);
}

}
}

最佳答案

Swing 是一个单线程框架,这意味着您不能在事件调度线程的上下文中执行长时间运行的操作,否则您将无法处理事件队列并且您的程序将挂起。

先看看 Concurrency in SwingHow to use Swing Timers

您还违反了油漆链契约(Contract)。看看Painting in AWT and SwingPerforming Custom Painting有关如何在 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);
}

}
}

关于java - 我的 JPanel 程序无法成功运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29913648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com