gpt4 book ai didi

Java 绘制组件()

转载 作者:行者123 更新时间:2023-11-30 04:10:09 25 4
gpt4 key购买 nike

java PaintComponent()

     class MyClass extends JComponent {
void paintComponent() {
//code for class here
}
}

class myTestClass extends JPanel {
MyClass temp = new MyClass();

void paintComponent() {
super.paintComponent();


static void main(String[] a) {
JFrame f = new JFrame();
//Does the temp have to be added to the panel before it can be repainted
}
} //End Class

我知道JPanel进入 JFrame 。但是类( MyClass )是否必须添加到 JPanel 中?对于paintComponent()将用 super.paintComponent() 重新绘制子节点。我认为情况会是这样。如果是这样的话 temp.repaint()如果 super.paintComponent() 应该在任何被调用的地方重新绘制该特定组件重新绘制组件。

这似乎是一个没有人知道的答案,或者至少没有人真正发布过的答案。我的解释可能有点少,所以我会尝试在这里澄清它。

如果我有几个扩展 JComponent 的类,并且我希望它们全部在类中使用,以便我可以重新绘制它们。假设一个是圆形、长方形、正方形等等..............

如果他们都有 paintComponent()super.paintComponent()他们应该能够用 VariableName.repaint(); 重新绘制

是否必须将它们添加到 JPanel这将使它们成为内容。他们可以被称为repaint()吗?从代码中的任何地方调用(我认为就是这种情况)。

驱动程序类别:

  class MyMainDriverClass {
A a = new A();
B b = new B();
C c = new C();

void someClass() {
a.repaint();
b.repaint();
c.repaint(); //this should repaint the components if the JPanel is inside of the JFrame
//(I didn't put this code in)
}
}

如果创建的所有类都有paintComponent(),这应该重新绘制上面的组件。 .

道格·豪夫

最佳答案

是的,MyClass 需要添加到可在屏幕上显示的内容中才能进行绘制。这里的原因是 MyClass 扩展了 JComponentmyTestClass 扩展了 JPanel,它们没有任何关系,所以调用myTestClass 中的 super.paintComponent 不会以任何方式绘制或更新 MyClass

任何您想要绘制的东西都必须添加到可显示的容器中。

看看Painting in AWT and Swing有关绘画的更多详细信息。

您可以根据需要在任何 Component 个体或其父容器上调用 repaint

关于Java 绘制组件(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19827387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com