Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFrame仅在调整大小时更新_Java_Swing_Jframe - Fatal编程技术网

Java JFrame仅在调整大小时更新

Java JFrame仅在调整大小时更新,java,swing,jframe,Java,Swing,Jframe,我正在为高尔夫模拟器写一个程序。到目前为止,它基本上按预期工作,但只有当我拖动窗口以调整其大小时,窗口才会更新。为什么会这样?我已附上以下相关课程(还有其他课程)。非常感谢您的帮助。更新为MWE。程序应该更改背景颜色,但只有在调整窗口大小时才这样做。接下来是FieldComponent类,然后是main类 import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphic

我正在为高尔夫模拟器写一个程序。到目前为止,它基本上按预期工作,但只有当我拖动窗口以调整其大小时,窗口才会更新。为什么会这样?我已附上以下相关课程(还有其他课程)。非常感谢您的帮助。更新为MWE。程序应该更改背景颜色,但只有在调整窗口大小时才这样做。接下来是FieldComponent类,然后是main类

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class FieldComponent extends Canvas {

    private BufferedImage back;
    private int time;

public FieldComponent() {
    setVisible(true);
}

public void update(Graphics window) {
    paint(window);
}


public void paint(Graphics window) {
    Graphics2D twoDGraph = (Graphics2D) window;
    if (back == null) {
        back = (BufferedImage) createImage(getWidth(), getHeight());
    }
    Graphics graphToBack = back.getGraphics(); 
    if(time%2==0){
        graphToBack.setColor(Color.RED);
    }
    else{
        graphToBack.setColor(Color.GREEN);
    }
    graphToBack.fillOval(200, 300, 600, 600);
    time++; 
    twoDGraph.drawImage(back, null, 0, 0);
}

}
主要课程包括:

import javax.swing.JFrame;
import java.awt.Component;

public class GolfRunner extends JFrame {

    private static final int width = 1000;
    private static final int height = 800;

    public GolfRunner() {
        super("Golf Simulator");
        setSize(width,height); 
        FieldComponent golfgame = new FieldComponent();
        ((Component)golfgame).setFocusable(true); 
        getContentPane().add(golfgame);
        setVisible(true);
    }

    public static void main(String args[]) {
        GolfRunner run = new GolfRunner();
    }

}

我已经修改了您的代码,以符合当代Swing标准

  • 您必须首先调用SwingUtilities invokeLater方法,将Swing组件放在(EDT)上

  • 您可以使用Swing组件,比如JFrame。扩展Swing组件的唯一原因是要重写其中一个组件方法

  • 不要使用AWT组件,如Canvas。使用JPanel作为画布

  • Swing会自动双缓冲图形。您不必在单独的BuffereImage上绘制

  • 您可以在JPanel中设置首选大小,而不是JFrame。设置图形的大小,JFrame pack方法将处理JFrame的大小

  • 我把这些类放在一起,以便于粘贴。你应该分开上课

    package com.ggl.testing;
    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class GolfRunner implements Runnable {
    
        private static final int    width   = 1000;
        private static final int    height  = 800;
    
        private JFrame frame;
    
        @Override
        public void run() {
            frame = new JFrame();
            frame.setTitle("Golf Simulator");
            FieldComponent golfgame = new FieldComponent();
            ((Component) golfgame).setFocusable(true);
            frame.getContentPane().add(golfgame);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String args[]) {
            SwingUtilities.invokeLater(new GolfRunner());
        }
    
        public class FieldComponent extends JPanel {
    
            private static final long   serialVersionUID    = 
                    -6481773088613540357L;
    
            private int             time;
    
            public FieldComponent() {
                this.time = 1200;
                this.setPreferredSize(new Dimension(width, height));
            }
    
            @Override
            protected void paintComponent(Graphics window) {
                super.paintComponent(window);
    
                Graphics2D twoDGraph = (Graphics2D) window;
    
                if (time % 2 == 0) {
                    twoDGraph.setColor(Color.RED);
                } else {
                    twoDGraph.setColor(Color.GREEN);
                }
                twoDGraph.fillOval(100, 100, 600, 600);
    
                time++;
            }
    
        }
    
    }
    

    您能否为其余依赖项提供MWE?或者干脆把它们全部删除。你要把它们转换成一个子类?这看起来不是个好主意<代码>返回=(BuffereImage)(createImage(getWidth(),getHeight())基本上,将重型和轻型组件混合在一起可以使组件适合可见性,并相互覆盖。Z顺序描述组件在可见层次结构中的深度,但重量级组件没有Z顺序的概念。除非您以演示性的、立即有用的方式消除噪音并隔离问题,否则我们无法帮助您。请你解释一下时间好吗?这是一个神奇的数字。@Simon Kuang:我必须定义时间,以便Java能够编译。1200是第一个想到的数字。从user17902的代码来看,每次重新绘制JPanel时,他都会在红色圆圈和绿色圆圈之间交替切换。