无法在 Java 中将对象数组绘制到面板上

该程序很简单。我正在尝试在 Java 框架中的面板上打印 3-10 个随机的面孔。问题是面部不会打印在面板上。我对此很陌生,所以我不确定我搞砸了什么,并且我一直试图找到解决我的问题的方法有一段时间了。有什么帮助吗?

智慧大石
浏览 151回答 1
1回答

天涯尽头无女友

class FaceFrame extends JFrame {&nbsp; &nbsp; private FacePanel myFacePanel;&nbsp; &nbsp; public FaceFrame(ArrayList<Face> faceListIn, int width, int height) {&nbsp; &nbsp; &nbsp; &nbsp; setBounds(100, 100, 900, 600);&nbsp; &nbsp; &nbsp; &nbsp; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; FacePanel myFacepanel = new FacePanel(faceListIn);&nbsp; &nbsp; }}添加myFacepanel到JFrame可能是一个好的开始......public FaceFrame(ArrayList<Face> faceListIn, int width, int height) {&nbsp; &nbsp; setBounds(100, 100, 900, 600);&nbsp; &nbsp; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; FacePanel myFacepanel = new FacePanel(faceListIn);&nbsp; &nbsp; // This might be a good place to start&nbsp; &nbsp; add(myFacepanel);}……下一个问题……FacePanel(ArrayList<Face> FaceListIn){&nbsp; &nbsp; setFaceList(FaceList);}您分配FaceList给自己(您没有使用FaceListIn)。我会摆脱static并更新代码......class FacePanel extends JPanel {&nbsp; &nbsp; private ArrayList<Face> FaceList;&nbsp; &nbsp; public void setFaceList(ArrayList<Face> FaceListIn) {&nbsp; &nbsp; &nbsp; &nbsp; FaceList = FaceListIn;&nbsp; &nbsp; }&nbsp; &nbsp; //draw panel&nbsp; &nbsp; FacePanel() {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; }&nbsp; &nbsp; FacePanel(ArrayList<Face> FaceListIn) {&nbsp; &nbsp; &nbsp; &nbsp; setFaceList(FaceListIn);&nbsp; &nbsp; }&nbsp; &nbsp; public void paintComponent(Graphics g) {&nbsp; &nbsp; &nbsp; &nbsp; super.paintComponent(g);&nbsp; &nbsp; &nbsp; &nbsp; for (Face oD : FaceList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oD.paintComponent(g);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java