[英]Why isn't the paint() method being called at any point in this program?
这是我正在开发的益智游戏的代码。 我正在尝试使其在屏幕中间显示“已暂停”,但无法使其正常工作。 事实证明,即使使用各种repaints(),也没有任何反应。 为什么? 是因为类本身实际上并未扩展JPanel或JFrame吗?
package main;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PuzzleSquares implements ActionListener, KeyListener, Runnable {
boolean running = false;
boolean inGame = false;
boolean paused = false;
JFrame frame = new JFrame();
JPanel menu = new JPanel();
JPanel game = new JPanel();
Thread thread;
public PuzzleSquares() {
frame.requestFocus();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Just another puzzle game");
addMenu();
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}
public void addMenu() {
menu.setLayout(new BoxLayout(menu, BoxLayout.Y_AXIS));
menu.setFocusable(true);
menu.setBackground(Color.GRAY);
createButton("START", menu);
createButton("HIGH SCORES", menu);
createButton("EXIT", menu);
frame.revalidate();
frame.add(menu);
}
public void addGame() {
game.setBackground(Color.GREEN);
game.addKeyListener(this);
game.setFocusable(true);
game.setPreferredSize(frame.getMaximumSize());
frame.add(game);
frame.revalidate();
game.requestFocus();
}
public void createButton(String title, Container c) {
JButton button = new JButton(title) {
{
setSize(200, 30);
setMaximumSize(getSize());
}
};
button.setAlignmentX(JPanel.CENTER_ALIGNMENT);
button.addActionListener(this);
button.setActionCommand(title);
c.add(button);
}
public synchronized void start() {
if (running) { return; }
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop() {
if (!running) { return; }
running = false;
try { thread.join(); } catch (Exception e) {}
}
@Override
public void run() {
while (running) {
}
}
public void paint(Graphics g) {
System.out.println("Hi");
if (paused) {
g.setColor(Color.BLACK);
for (int i=0;i<100;i++) {
g.drawLine(i, i+1, i, i+1);
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("START")) {
System.out.println("Start");
frame.remove(menu);
addGame();
}
if (e.getActionCommand().equals("HIGH SCORES")) {
System.out.println("High Scores");
}
if (e.getActionCommand().equals("EXIT")) {
System.out.println("Exit");
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
if (!paused) {
System.out.println("Pause");
paused = true;
} else {
paused = false;
}
game.repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
public static void main(String[] args) {
PuzzleSquares ps = new PuzzleSquares();
ps.start();
}
}
基本上,您的类是不可绘制的,它不会扩展框架可以绘制的任何内容(例如JComponent
或JPanel
)
如果您尝试做类似...
@Override
public void paint(Graphics g) {
super.paint(g);
System.out.println("Hi");
if (paused) {
g.setColor(Color.BLACK);
for (int i=0;i<100;i++) {
g.drawLine(i, i+1, i, i+1);
}
}
}
它不会编译,因为该方法不会覆盖其父类或接口中的任何方法。
有关更多详细信息,请参见“ AWT中的绘画”和“摇摆和执行自定义绘画 ”
我也建议不要使用KeyListener
,而建议使用键绑定API,请参阅如何使用键绑定 。 这将有助于解决您的重点问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.