如何“刷新"一个JFrame [英] How to "refresh" a JFrame

查看:89
本文介绍了如何“刷新"一个JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的程序存在这个问题;我根本无法弄清楚如何刷新" JFrame,以便它正确显示对变量vangle的更改.我当然已经导入了所有正确的文件.使用JPanel而不是JFrame有什么好处吗?请全力以赴!:)

I have this issue with my current program; I simply can't figure out how to "refresh" the JFrame so it correctly displays the changes to the variable vangle. I have of course imported all the correct files. Is there any advantage to using a JPanel instead of a JFrame? Please give it your best shot! :)

public class projekt {
    static int vangle = 20;
        public static void main(String args[]) {new FractalTree().setVisible(true);

        JFrame frame = new JFrame();
        JButton b1 = new JButton();
        frame.setSize(200, 100);
        b1.setSize(20, 20);
        b1.setVisible(true);
        b1.setText("vinklen + 10");
        frame.add(b1);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //stoppe når jframen bliver lukket lukker begge jframes
            b1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    vangle = vangle + 10;
                    System.out.println("angle:" + vangle);


                }
            });
    }



    public static class FractalTree extends JFrame {
        public FractalTree() {
            super("Fractal Tree");
            setBounds(100, 100, 800, 600);
            setResizable(true);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); /* stopper når den lukker, lukker begge Jframes */
        }

        public static void drawTree(Graphics g, int x1, int y1, double angle, int depth) {
            if (depth == 0) return;
            int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 10.0); /* skifter størelsen på den */
            int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 10.0);
            g.drawLine(x1, y1, x2, y2);
            drawTree(g, x2, y2, angle - vangle, depth - 1); /* skifter vinklen */
            drawTree(g, x2, y2, angle + vangle, depth - 1);


        }

        @Override
        public void paint(Graphics g) {
            g.setColor(Color.blue); /* skifter farven */
            drawTree(g, 400, 500, -90, 9);
        }


    }
}

推荐答案

您需要在FractalTree上调用 repaint(); .为此,您需要对其进行引用,例如

You need to call repaint(); on the FractalTree. In order to do that, you need a reference to it, e.g.

    final FractalTree tree = new FractalTree();
    tree.setVisible(true);

,然后您的方法变为:-

and then your method becomes:-

        public void actionPerformed(ActionEvent e) {
            vangle = vangle + 10;
            System.out.println("angle:" + vangle);
            tree.repaint();
        }

最后一点,您需要先清除树的先前图形,否则新树将在旧树的顶部绘制.

And one final point, you need to clear the previous drawing of the tree first, otherwise your new tree will draw over the top of the old one.

很明显,OP的代码存在很多问题,并且可能需要完全重写才能完成.但是我的理解(根据 https://meta.stackexchange.com/questions/266572/回答问题或改进代码)是,SO的主要目的是回答用户的特定问题,我的答案旨在做到这一点.

There are obviously a lot of problems with OP's code and it could do with a complete rewrite; however my understanding (as per https://meta.stackexchange.com/questions/266572/answer-question-or-improve-code ) is that the primary aim of SO is to answer a user's specific question, which my answer aims to do.

这篇关于如何“刷新"一个JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆