4

嗨,我试图在游戏循环中每次都调用paint方法。此时,一旦按下按钮,屏幕就会弹出一个标签和一个按钮,我想要的标签和按钮去,但我无法得到开始的绘画方法我尝试了 j.repaint() 和 j.validate() 都没有访问绘画方法。任何帮助将不胜感激。

package sgame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class SGame extends JPanel
{

    public static void main(String[] args)
    {

        final JFrame window = new JFrame("hello");
        final JPanel window1 = new JPanel();
        Timer loop = new Timer(1000, new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                GameLoop(window1);

            }
        });
        Window(window, loop);

    }

    public static void Window(final JFrame window, final Timer loop)
    {
        final JButton b1 = new JButton("GO!");
        b1.setLocation(210, 300);
        b1.setSize(70, 50);
        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(500, 500);
        window.setLocation(420, 170);
        window.setBackground(Color.BLACK);
        final JLabel Title = new JLabel("Snake", JLabel.CENTER);
        Title.setFont(new Font("Times New Roman", Font.ITALIC, 60));
        Title.setVerticalAlignment(JLabel.CENTER);
        Title.setForeground(Color.WHITE);
        window.add(b1);
        window.add(Title);
        b1.addActionListener(new ActionListener() // runs when buttons pressed
            {

                @Override
                public void actionPerformed(ActionEvent e) // clears header,button and starts timer
                {
                    b1.invalidate();
                    b1.setVisible(false);
                    Title.setVisible(false);
                    Title.invalidate();
                    loop.start();
                }

            });

    }

    public static void GameLoop(JPanel j)
    {
        // Call paint method
    }

    public void paintComponent(Graphics g)
    {
        g.setColor(Color.yellow);
        g.drawRect(30, 30, 30, 30);
    }
}
4

1 回答 1

1

paintComponent()永远不会调用in中的方法,SGame因为您没有实例化任何 SGame 对象。并且 window1 尚未添加到框架中。

像这样的东西应该更好:

public class SGame extends JPanel {

    private Timer loop;

    public SGame(){
        loop = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameLoop();
            }
        });
    }

    public static void main(String[] args) {
        final JFrame window = new JFrame("hello");
        final JButton b1 = new JButton("GO!");
        b1.setLocation(210, 300);
        b1.setSize(70, 50);
        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(500, 500);
        window.setLocation(420, 170);
        window.setBackground(Color.BLACK);
        final JLabel Title = new JLabel("Snake", JLabel.CENTER);
        Title.setFont(new Font("Times New Roman", Font.ITALIC, 60));
        Title.setVerticalAlignment(JLabel.CENTER);
        Title.setForeground(Color.WHITE);
        window.add(b1);
        window.add(Title);
        b1.addActionListener(new ActionListener() { // runs when buttons pressed
            @Override
            public void actionPerformed(ActionEvent e) // clears header,button
            {
                b1.invalidate();
                b1.setVisible(false);
                Title.setVisible(false);
                Title.invalidate();
                SGame sg = new SGame();
                window.add(sg);
                sg.start();
            }
        });;
    }

    public void start(){
        loop.start();
    }

    public void gameLoop() {
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.yellow);
        g.drawRect(30, 30, 30, 30);
    }
}
于 2012-09-02T17:25:28.203 回答