简体   繁体   English

每次最小化并重新打开Java窗口时,图形都会更改

[英]Each time I minimise and reopen my Java window, the graphics change

I got a bit of a problem which I am completely baffled about. 我遇到了一个问题,我完全感到困惑。 Not even sure where to start to debug the problem. 甚至不确定从哪里开始调试问题。

I have a JFrame which promotes the user to enter a number, lets say the number was 4. I sent this number to my paint component to create a vertical line of 4 x 50. SO the line should be 200 in length. 我有一个JFrame可以促进用户输入数字,可以说数字是4。我将此数字发送到我的绘画组件以创建4 x 50的垂直线。因此,该线的长度应为200。 Which it does. 是的。

But if I minimise and reopen the window, that 4 seems to be multiplied again by 50 giving 200. Then that again is multiplied by 50 making the line 10,000. 但是,如果我最小化并重新打开窗口,那4似乎又乘以50得出200。然后又乘以50得出了10,000行。

I placed my paint component code in hope that it would help as the issue must be there, but if more code is needed I'll be happy to post. 我放置了我的绘画组件代码,希望它能对问题有所帮助,但是如果需要更多代码,我将很乐意发布。

class mainPanel extends JPanel
{
    int processes;
    public mainPanel(int x) //the value x is passed from another class, this was the number the user chooses...i.e 4
    {
    processes = x;
    setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 5));
    }

    public Dimension getPreferredSize() {
        return new Dimension (1000, 1000);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);   
    int storedProcesses = processes;

        // Draw Title
    g.setFont(new Font("TimesRoman", Font.PLAIN, 28)); 
        g.drawString("We place the title here",380,50);
    processes = processes * 50;
    g.drawLine(100,100,100, processes+100); //Vertical (down) line

    //labels for vertical line
    g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
    int y = 125;
    for (int i=1; i<=storedProcesses; i++) //this loop keeps repeating and getting larger for some unknown reason...then its repainting my JFrame
    {
        g.drawString(String.valueOf(i), 70,y);
        y=y+50;
        System.out.println("Loop" + storedProcesses); //used for debugging
    }

    g.drawLine(100,processes+100,1000,processes+100); //Horizontal (across) line
    }  
}

I've attached some screen shots to illustrate what's going on with 1 process as an example. 我附上了一些屏幕截图,以1个过程为例进行说明。

Before I minimise the window (this is fine): 在最小化窗口之前(这很好):

这是我最小化窗口之前

This is after I minimise and re maximise the window 这是在我最小化并重新最大化窗口之后

在最小化和最大化之后,我得到了这个结果

You can't control when Swing invokes the paintComponent() method. 您无法控制Swing何时调用paintComponent()方法。 Therefore you should NEVER change a property of your class in the paintComponent() method. 因此,永远不要在paintComponent()方法中更改类的属性。 All variables that you manipulate in the paintComponent() method should be local variables. 您在paintComponent()方法中操作的所有变量都应为局部变量。

processes = processes * 50;

Your current code is modifying the "processes" variable. 您当前的代码正在修改“ processes”变量。 Don't do this. 不要这样

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

相关问题 每次执行侦听器时,如何更改面板的图形? - How do I change the graphics of a panel each time a listener executes? Java图形:每次重新绘制时,都会出现黑闪 - Java Graphics: Each time it repaints, i get a blackflash 我可以在每次更改Java代码时阻止重新启动应用程序吗? - Can i prevent restarting my application each time i make a change in my java code? 我无法在Java程序(摆动)的面板/画布/窗口上绘制图形。 有任何想法吗? - I can't get graphics to draw on my panel/canvas/window in my Java program (swing). Any Ideas? 对于我的Java小程序,每个屏幕尺寸都需要不同尺寸的图形吗? - For my Java applet, will I need differently-sized graphics for each screen size? 如何更改java.awt.Graphics绘制对象的方式? - How can I change the way java.awt.Graphics draws my object? 每分钟关闭并重新打开Java窗口 - Close and Reopen Java Window Every Minute 关闭时隐藏后重新打开 Java window - Reopen Java window after hiding it on close 如何在Java中随机更改窗口背景的颜色? - How can I change the colour of my window background randomly in java? 为什么每次重新打开主活动时我的变量都会重置为零 - Why are my variables being reset to zero every time I reopen my Main activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM