我想用最简单的方法制作pacman开/关嘴动画.这是我最近的代码:问题是,什么都没发生?
package ordner;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PacMan implements ActionListener {
private JFrame frame;
private DrawPanel panel;
private void initGui() {
frame = new JFrame("Pacman");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new DrawPanel();
frame.add(panel);
panel.setBackground(Color.BLACK);
frame.setSize(300, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
PacMan pm = new PacMan();
pm.initGui();
}
@Override
public void actionPerformed(ActionEvent e) {
panel.repaint();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的画板:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawPanel extends JPanel …Run Code Online (Sandbox Code Playgroud) 我需要在我的Java GUI中运行后台线程,该线程仅在我单击按钮时运行,并在我再次单击该按钮时暂停.我不确定如何设置它,但我已经在我的构造函数中放置了一个线程,并且当我将特定布尔值设置为TRUE时,while循环设置为通过.一个按钮从设置此布尔值TRUE或FALSE切换.
我在这个GUI中的其他所有工作都很好.当我尝试调试线程时,它实际上是在我逐步完成线程时工作但当我尝试完全运行GUI时没有任何内容.GUI相当大,所以我要设置一部分构造函数和按钮的动作监听器.其余的代码是不必要的,因为它工作得很好.我需要知道我在做错了什么:
public BasketballGUI() {
// certain labels and buttons
Thread runningSim = new Thread() {
public void run() {
while(simRun) {
// do stuff here
}
}
};
runningSim.start();
}
// other GUI stuff
// actionListener that should run the thread.
class SimButtonListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
if(!simRun) {
simRun = true;
sim.setText("Pause Simulator");
}
else if(simRun) {
simRun = false;
sim.setText("Run Simulator");
}
// other stuff in this actionListener
}
}
Run Code Online (Sandbox Code Playgroud)