如何使用带有 class 扩展 Canvas 的 JButton?

How to use JButton with a class extending Canvas?

我正在使用扩展 Canvas 的 class 来尝试制作 Conway 的生命游戏副本。我不是 Java 的新手,但我是 Swing 和 canvas 的新手。我已经尝试了很多方法来将 JButton 对象添加到我的 canvas 但没有成功。我已经包含了我的代码,如果有人对我如何实现按钮有任何建议,将不胜感激。

import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JFrame;

public class ConwaysGameOfLife extends Canvas implements Runnable{
    private static final long serialVersionUID = 1L;

    public static final int SIZE = 960;

    public static final String TITLE = "Conway's Game of Life";

    private boolean running = false;
    private Thread thread;

    private static JButton but;

    public static void main(String[] args)
    {
        ConwaysGameOfLife game = new ConwaysGameOfLife();

        game.setPreferredSize(new Dimension(SIZE-10, SIZE-10));
        game.setMaximumSize(new Dimension(SIZE-10, SIZE-10));
        game.setMinimumSize(new Dimension(SIZE-10, SIZE-10));

        JFrame frame = new JFrame(TITLE);

        frame.add(game);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        but = new JButton("Button");
        frame.add(but);

        game.start();
    }

    private void start()
    {
        if(running)
            return;

        running = true;
        thread = new Thread(this);
        thread.start();
    }

    private void stop()
    {
        if(!running)
            return;

        try{thread.join();} 
        catch(InterruptedException e){}
        System.exit(1);
    }

    public void run() 
    {
        while(running)
        {
            System.out.println("RUNNING");
        }
        stop();
    }

    public void paint(Graphics g)
    {
        g.setColor(Color.BLACK);
        g.fillRect(0,0,SIZE,64);

        for(int i = 16; i < SIZE; i += 16)
        {
            g.drawLine(i,0,i,SIZE);
            g.drawLine(0,i,SIZE,i);
        }
    }
}

您遇到的直接问题是您已将组件添加到 BorderLayout...

中的相同位置
frame.add(game); // Look and me, I'm in the CENTER
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

but = new JButton("Button");
frame.add(but);  // Look and me, I'm in the CENTER

现在,因为您在添加按钮后没有使框架失效,所以它还没有更新,所以它没有被显示,但即​​使您这样做了,您也可能会发现一些奇怪的问题,因为 AWT 组件不'有 z 深度的概念,这意味着可能会或可能不会覆盖按钮...有趣的东西。

相反,将按钮添加到框架内的不同位置/BorderLayout

frame.add(game); // Look and me, I'm in the CENTER
but = new JButton("Button");
frame.add(but, BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

查看 How to Use BorderLayout 了解更多详情。

在不了解绘画是如何完成的情况下,我也会小心打破 Canvas 的绘画链。你应该打电话给 super.paint.

事实上,在这种情况下,使用 Canvas 几乎没有什么好处(还有很多问题),相反,您应该使用 JPanel 并覆盖它的 paintComponent 方法,请确保在进行任何自定义绘画之前调用 super.paintComponent

有关更多详细信息,请参阅 Painting in AWT and Swing and Performing Custom Painting