为什么不在这个程序中的任何一点调用paint()方法?

时间:2015-01-14 04:11:36

标签: java swing

这是我开始研究的益智游戏的代码。我试图让它显示"暂停"在屏幕中间,但无法设法让它工作。事实证明,即使有各种重绘(),也没有任何事情发生。为什么?是因为课程本身并没有实际扩展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();
}
}

1 个答案:

答案 0 :(得分:2)

基本上,您的课程不具有可绘画性,它不会受到框架可能绘制的任何内容的影响(如JComponentJPanel

如果您尝试过像......那样的事情。

@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);
        }
    }
}

它不会编译,因为该方法不会覆盖其父类或接口的任何方法。

有关详细信息,请参阅Painting in AWT and SwingPerforming Custom Painting

我也不鼓励使用KeyListener,而是建议使用密钥绑定API,请参阅How to Use Key Bindings。它将有助于解决您的焦点问题