Java的swing中,如何将一个JLabel[]的数组添加到JFrame中?
String[]dishes;JLabel[]dishes_label=newJLabel[dishes.length];for(inti=0;i<dishes.leng...
String[] dishes;
JLabel[] dishes_label = new JLabel[dishes.length];
for(int i=0; i<dishes.length; i++){
dishes_label[i] = new JLabel();
dishes_label[i].setText(dishes[i]);
//contentPane.add(dishes_label[i],BorderLayout.CENTER);
}
contentPane.add(dishes_label[dishes.length],BorderLayout.CENTER);
编译可以通过,当运行到这的时候就出现了
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
等问题 展开
JLabel[] dishes_label = new JLabel[dishes.length];
for(int i=0; i<dishes.length; i++){
dishes_label[i] = new JLabel();
dishes_label[i].setText(dishes[i]);
//contentPane.add(dishes_label[i],BorderLayout.CENTER);
}
contentPane.add(dishes_label[dishes.length],BorderLayout.CENTER);
编译可以通过,当运行到这的时候就出现了
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
等问题 展开
展开全部
错误在
contentPane.add(dishes_label[dishes.length],BorderLayout.CENTER);
dishes_label的长度为dishes.length
所以,dishes_label[dishes.length]为null,
如果你要拿dishes_label最后一个,应该是dishes_label[dishes.length-1],
contentPane.add(dishes_label[dishes.length],BorderLayout.CENTER);
dishes_label的长度为dishes.length
所以,dishes_label[dishes.length]为null,
如果你要拿dishes_label最后一个,应该是dishes_label[dishes.length-1],
追问
感谢你的回答。那个是我不小心打错了。主要想问的是如何将这个JLabel数组整个添加到JFrame的中间?contentPane.add(dishes_label,BorderLayout.CENTER);像这样。可是编译的时候报错了。应该怎样改呢?谢谢
追答
简单。。
1、设置父容器的布局,即setLayout(new BorderLayout())
2、如果你要把一堆东西,都放到north,那么,你就应该先在north添加一个jpanel,然后再设置jpanel的布局,之后就应该在jpanel里面调用add方法添加进去
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询