Java Swing 的 DrawRect:创建新矩形会替换旧的尺寸。

huangapple go评论91阅读模式
英文:

Java Swing DrawRect: creating new replaces old dimensions

问题

我尝试使用下面的DrawRect类创建了两个矩形但是当我创建一个新的DrawRect对象时它会替换旧对象的宽度和高度

package MemDiagramVisualizer;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;

public class DrawRect extends JPanel {
   private static int RECT_X;
   private static int RECT_Y;
   private static int RECT_WIDTH;
   private static int RECT_HEIGHT;
   
   public DrawRect(int w, int h) {
	   RECT_X = 20;
	   RECT_Y = 20;
	   RECT_WIDTH = w;
	   RECT_HEIGHT = h;
   }
   
   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.drawRect(RECT_X, RECT_Y, RECT_WIDTH, RECT_HEIGHT);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(RECT_WIDTH + 2 * RECT_X, RECT_HEIGHT + 2 * RECT_Y);
   }
}
JPanel visDisplay = new JPanel();
visDisplay.setLayout(new GridLayout(1,3));
DrawRect rec2 = new DrawRect(40,60);
visDisplay.add(rec2);
DrawRect rec = new DrawRect(100,600);
visDisplay.add(rec);

上述代码添加到窗体的内容面板中时,会创建两个尺寸为100,600的矩形。


<details>
<summary>英文:</summary>

I tried making two rectangles using the class below: DrawRect but when I create a new DrawRect object it replaces old one&#39;s width and height.

package MemDiagramVisualizer;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;

public class DrawRect extends JPanel {
private static int RECT_X;
private static int RECT_Y;
private static int RECT_WIDTH;
private static int RECT_HEIGHT;

public DrawRect(int w, int h) {
RECT_X = 20;
RECT_Y = 20;
RECT_WIDTH = w;
RECT_HEIGHT = h;
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(RECT_X, RECT_Y, RECT_WIDTH, RECT_HEIGHT);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(RECT_WIDTH + 2 * RECT_X, RECT_HEIGHT + 2 * RECT_Y);
}

}

JPanel visDisplay = new JPanel();
visDisplay.setLayout(new GridLayout(1,3));
DrawRect rec2 = new DrawRect(40,60);
visDisplay.add(rec2);
DrawRect rec = new DrawRect(100,600);
visDisplay.add(rec);

The code above when added to frame content pane creates two rectangles with 100,600 dimensions

</details>


# 答案1
**得分**: 1

你的程序中有一些问题:

1. 所有这四个变量:

       private static int RECT_X;
       private static int RECT_Y;
       private static int RECT_WIDTH;
       private static int RECT_HEIGHT;

   它们都是静态的,但你试图在程序内部对它们进行更改,这会影响到程序的所有实例,它们都会共享相同的值。在这种情况下,我建议你将其移除,然后应该就没问题了。这就是为什么当你使用相同代码创建另一个类时,它能正常工作的原因。

2. `RECT_X = 20;` 和 `RECT_Y = 20;` 在构造函数内部,如果这些是常量,那么请在顶部进行初始化,不要在类的每个实例中设置它们。

3. 这不是一个错误,但根据你的需求,你可能希望停止使用多个 `JPanel`,而是像 [这个答案](https://stackoverflow.com/a/62763643/2180785) 中所示,使用 Shape API 创建一个形状数组,然后在单个 `JPanel` 中绘制它们。当然,这完全取决于你的需求。

在从上述常量中移除 `static` 关键字后,我们得到了这样的代码:

[![enter image description here][1]][1]

  [1]: https://i.stack.imgur.com/k7DaG.png

<details>
<summary>英文:</summary>

You have a couple of issues in your program:

1. All these 4 variables:

       private static int RECT_X;
       private static int RECT_Y;
       private static int RECT_WIDTH;
       private static int RECT_HEIGHT;

    They all are static, but you&#39;re trying to change them inside the program, this will apply for all the instances of your program and they all are gonna share the value. In this case I&#39;d suggest you to remove that and you should be good. This is the reason why when you create another class with the same code, it worked.

2. `RECT_X = 20;` and `RECT_Y = 20;` inside the constructor, if these are constants, then initialize them at the top and don&#39;t set them in every instance of the class.

3. Not an error, but depending on your requirements, you might want to stop using multiple `JPanel`s and instead use the Shape API as shown [in this answer](https://stackoverflow.com/a/62763643/2180785) to create an Array of Shapes that you can draw in a single `JPanel`. Again, this all depends on your needs.

After removing the `static` keyword from those constants above, we have this:

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/k7DaG.png

</details>



huangapple
  • 本文由 发表于 2020年7月25日 07:30:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63082710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定