如何在扩展Canvas的类中使用JButton?

时间:2015-06-15 03:47:29

标签: java swing canvas awt jbutton

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

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

1 个答案:

答案 0 :(得分:2)

您遇到的直接问题是,您已将组件添加到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并覆盖它{{1}方法,确保在进行任何自定义绘画之前调用paintComponent

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