gpt4 book ai didi

java - JPanel 不显示绘图

转载 作者:行者123 更新时间:2023-11-30 07:38:46 24 4
gpt4 key购买 nike

我在 JPanel 上显示绘图时遇到问题。我创建了三个相互链接的类,如下所示。我想知道为什么这段代码不显示我的绘图。

c.add(pDraw);
pDraw.add(draw);

1)主要

public class mainPage {
public static void main(String[]args){
JFrame appFrame = new Frame();
appFrame.setVisible(true);
appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

2) 框架

 public class Frame extends JFrame implements ActionListener{

private drawingBoard draw;

public Frame (){
draw = new drawingBoard(); //generate pattern
GridBagLayout m = new GridBagLayout();
Container c = (Container)getContentPane();
c.setLayout (m);
GridBagConstraints con;
.......

JPanel pDraw = new JPanel();
pDraw.setPreferredSize(new Dimension(500,500));
.....
c.add(pDraw);
pDraw.add(draw); // Call other class for drawing

.....
setResizable(false);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}

3) JPANEL

public class drawingBoard extends JPanel {
.....
public void paint(Graphics g) {
......
}
}

最佳答案

答案在这里,我忘了设置绘图板 JPanel 的大小:-)

 public class drawingBoard extends JPanel {
public drawingBoard(){
setPreferredSize(new Dimension (500,500));

}
}

关于java - JPanel 不显示绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1541541/

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