[英]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.