gpt4 book ai didi

java - 同一类中多个面板的重绘方法

转载 作者:行者123 更新时间:2023-12-01 12:06:46 25 4
gpt4 key购买 nike

我想知道如何重写同一类中每个面板的绘制方法以及如何单独调用它们?

当您位于扩展 JPanel 的类中(因此仅在一个面板中)时,我只知道 repaint() 调用,而不是当您只制作面板时。

提前致谢。

最佳答案

通常,您创建一个扩展 JPanel 的类来重写 paintComponent 方法:

public class Test extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// code here
}

public void doStuff() { repaint(); }
}

您可能会考虑创建一个嵌套类,如下所示:

public class Test {
public class MyPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// code here
}
}
JPanel panel = new MyPanel();
panel.repaint();
}

或者您可以在不创建扩展 JPanel 的类的情况下执行此操作:

JPanel panel1 = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// code here
}
};

panel1.repaint();

关于java - 同一类中多个面板的重绘方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27536818/

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