简体   繁体   English

为什么每次最小化窗口时我的 Java 程序都会刷新?

[英]Why does my Java program refresh every time I minimize the window?

I am making a palace game to understand and get better at programming in Java.我正在制作一个宫殿游戏来理解和更好地使用 Java 编程。 Though I have run into an issue where every time I minimize the screen the variable that is selecting the card image constantly refreshes.虽然我遇到了一个问题,每次我最小化屏幕时,选择卡片图像的变量都会不断刷新。 For example the Randomizing function I have makes the variable pick a card depending on a digit and then it displays it, but it changes every time I minimize the page and open it again.例如,我的随机化函数使变量根据数字选择一张卡片,然后显示它,但是每次我最小化页面并再次打开它时它都会改变。 I tried to look for the answer to my problem but I couldn't find it.我试图寻找我的问题的答案,但我找不到。 My code for what I am doing is in the previous question I asked.我正在做的事情的代码在我问的上一个问题中。 My question is how do I stop my variables from refreshing every time minimize the page?我的问题是如何在每次最小化页面时阻止我的变量刷新?

Edit: I just realized it also changes every time I move the window with my cursor if this helps.编辑:我刚刚意识到每次我用光标移动窗口时它也会改变,如果这有帮助的话。

This is my randomize function:这是我的随机函数:

public void randomizer() {
        Random rand = new Random();
        int rand_int1 = rand.nextInt(15-1)+1;
        System.out.print(rand_int1);
        if (rand_int1 == 1) {
            setVariables(aceClover);

        }else if (rand_int1 == 2) {
            setVariables(twoClover);
        }else if (rand_int1 == 3) {
            setVariables(threeClover);
        }else if (rand_int1 == 4) {
            setVariables(fourClover);
        }else if (rand_int1 == 5) {
            setVariables(fiveClover);   
        }else if (rand_int1 == 6) {
            setVariables(sixClover);
        }else if (rand_int1 == 7) {
            setVariables(sevenClover);
        }else if (rand_int1 == 8) {
            setVariables(eightClover);
        }else if (rand_int1 == 9) {
            setVariables(nineClover);
        }else if (rand_int1 == 10) {
            setVariables(tenClover);
        }else if (rand_int1 == 11) {
            setVariables(jackClover);
        }else if (rand_int1 == 12) {
            setVariables(queenClover);
        }else if (rand_int1 == 13) {
            setVariables(kingClover);
        }else {
            System.out.println("Couldn't return any Cards!");
        }
    }
package MainClasses;
import java.awt.*;
import java.io.*;
import java.util.Random;

import javax.imageio.ImageIO;

public class DrawBoard extends Component{
    AllCards accessor = new AllCards();
    public void paint(Graphics g) { // function to draw onto the window
    Graphics2D g1 = (Graphics2D)g; // the component being used to access and write to the window
    g1.clearRect(0, 0, getWidth(), getHeight()); // clears rectangle every frame
    g1.setBackground(Color.green); // sets background color
    accessor.randomizer();
    g1.drawImage(accessor.imageExtract, 100, 100, null);
    }   
}

If you need the rest of the code its in this question:如果您需要此问题中的其余代码:

The full code 完整代码

It does have some minor changes because I fixed my previous problem all I did was make a new class and move the creation of those variables over to that class and removed them from my previous one.它确实有一些细微的变化,因为我解决了之前的问题,我所做的只是创建一个新类并将这些变量的创建移到该类中,然后将它们从我以前的类中删除。

Because you're calling the randomizer from within the paint method, and that method is called every time the gui gets minimized and restored, or any time it is resized.因为您是从 Paint 方法中调用随机化器,并且每次 gui 最​​小化和恢复时都会调用该方法,或者每次调整大小时都会调用该方法。

solution: Don't do that, don't have any program logic within a painting method but instead have only painting code there.解决方案:不要那样做,绘画方法中没有任何程序逻辑,而只有绘画代码。

Instead:反而:

  • Make the call from the class constructor which is only called once从只调用一次的类构造函数进行调用

Also:还:

  • Don't extend Component but rather JPanel (don't mix AWT and Swing components)不要扩展组件,而是扩展 JPanel(不要混合 AWT 和 Swing 组件)
  • Paint within the JPanel's paintComponent method在 JPanel 的paintComponent 方法中绘制
  • Call the super's painting method within the override super.paint(g) if you are overriding paint or super.paintComponent(g) if you override paintComponent.如果要覆盖paint,则在覆盖super.paint(g)调用super 的绘制方法,如果覆盖paintComponent,则super.paintComponent(g) This allows "housekeeping" painting to occur, including the removal of "dirty" pixels.这允许发生“内务”绘画,包括去除“脏”像素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次我最小化Java中的窗口时,如何放置JTextField而不使其生成更多字段? - How to place a JTextField without it generating more fields every time I minimize the window in Java? 为什么每次运行我的程序时它都会添加到最后一个 - Why does it add on to the last one every time I run my program 为什么我的Java程序每次都打印零? - Why is my java program printing zero every time? 为什么当我执行我的 Java 程序时它不转换第一个用户输入,但每个用户输入之后? - Why when I execute my Java program does it not convert the first user input, but does every user input after? 为什么每次运行我的应用程序都会崩溃? 没有错误 - Why does my app crash every time I run it? There are no errors 为什么Windows XP会在我的第二个屏幕上最小化我的摇摆全屏窗口? - Why does windows XP minimize my swing full screen window on my second screen? 为什么每次我连接到此套接字程序时,它都要占用不同的端口 - why does this socket program take different ports every time i connect to it 每次使用 Log4j 运行 Java 程序时,如何重新创建日志文件? - How to create log file freshly every time I run my Java Program using Log4j? 在运行时刷新java程序 - refresh java program at running time 为什么我的Java程序崩溃? - Why does my java program crashes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM