【问题标题】:java swing won't displayjava swing不会显示
【发布时间】:2013-06-21 08:00:59
【问题描述】:

我目前正在尝试将动画添加到具有各种对象在 2D 空间中四处移动和交互的程序中。我终于到了计时器工作的地方,程序让对象按照我想要的方式移动和交互......但它打开的显示面板仍然令人沮丧地空白。我一直在尝试使用网上找到的示例中的代码,但我一定还是遗漏了一些东西......

    public class Populus2 extends JPanel 
{
    /**
     * @param args
     */

        static float[] xCoordinates;
        static float[] yCoordinates;
        static int duration;
        static int iteration = 1;
        static int graphSize = 500;
        ..............

        static JFrame frame;
        static JLabel lbl;  
        JPanel panel;
        static Timer timer;

        public static void main(String[] args) throws IOException {
            final Populus2 pop = new Populus2();

            frame = new JFrame("Animation Frame");
            lbl = new JLabel();
            Panel panel = new Panel();
            panel.add(lbl);
            frame.add(panel, BorderLayout.CENTER);
            frame.setSize(graphSize, graphSize);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            ActionListener timeStep = new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.out.println("at time " + iteration);
                    spendTime();
                    pop.repaint();
                    if(iteration>duration)
                        timer.stop();
                }
            };

        timer = new Timer(100, timeStep);
        timer.setInitialDelay(0);
        timer.start();      
    }

    @Override
    public void paint(Graphics g) 
    {
        super.paint(g);
        System.out.println("******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n");
        Graphics2D g2d = (Graphics2D) g;
        paintCell(g, 1);  //eventually, I want to call this method for every "cell" in the program
        g.drawLine(30, 30, 80, 80);  //this line's basically a test to see if I can display anything at all
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }


      public void actionPerformed(ActionEvent e) {
        repaint();
    }

    public void paintCell(Graphics graphics, int cellNumber)
    {
        graphics.setColor(Color.black);
        graphics.fillOval((int)(graphSize*xCoordinates[cellNumber]/size), (int)(graphSize*yCoordinates[cellNumber]/size), 5, 5);
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        for(int i = 0; i < types.length; i++)
            paintCell(g, i);
    }


    public static void spendTime()
    {
        //advances the iteration counter and recalculates the coordinates of all the cells
    }

}

最终,我希望程序在计时器的每次迭代中,为模拟中的每个单元调用paintCell(),每个单元代表一个移动对象。不过,目前,屏幕仍然是空白的。为了记录,我通过 System.out.println() 调用 paint() 的消息没有显示。 想法?

【问题讨论】:

  • 你会在定时器内部调用 repaint() 吗?
  • 好吧,我在 timeStep ActonListener 内调用 pop.repaint()。

标签: java swing animation graphics awt


【解决方案1】:

我一直在尝试使用网上找到的示例代码,但我一定还是遗漏了一些东西......

不要在 Swing 程序中使用 AWT 建议。 Swing 与 AWT 不同。不要使用面板。在 Swing 中,您使用 JPanel。在 Swing 中,您覆盖 paintComponent() 方法,而不是 paint() 方法。现在您的代码尝试覆盖这两种方法。

阅读 Custom Painting 上的 Swing 教程以获取更多信息和适当的示例。

frame.setSize(graphSize, graphSize);

不要使用 frame.setSize() 框架大小包括边框和标题栏,因此您的图表将不是您想要的大小。相反,覆盖 JPanel 的 getPreferredSize(),您可以在其中进行自定义绘画以返回面板的大小。然后你会做frame.pack()

但面板不显示的原因是您没有将面板添加到框架中。

//frame.add(panel, BorderLayout.CENTER);
frame.add(pop, BorderLayout.CENTER);

下面的代码什么都不做,因为标签没有价值。您应该使用 JPanel,但不需要面板,因为您可以直接将标签添加到框架的内容窗格。

lbl = new JLabel();
Panel panel = new Panel();
panel.add(lbl);

【讨论】:

    猜你喜欢
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2013-12-08
    相关资源
    最近更新 更多