Re: [問題] JLabel

看板java作者 (隱者)時間19年前 (2006/05/23 19:08), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/4 (看更多)
※ 引述《tinkywinky ()》之銘言: : 我有個class ppp extends JFrame : 然後我想要在上面加一個JLabel b 顯示字 : 可是在main裡我寫 : ppp application= new ppp(); : JLabel b=new JLabel("hey"); : application.add(b,BorderLayout.SOUTH); : ... : 卻不會顯示hey : 連灰灰的label都看不到 : 請問該怎麼辦? : 謝謝 因為要加在Container中啊 讓Layout (預設的)幫你管理。 不然,你想看到他那只好自己setSize和location了 import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JLabel; public class SwingApp extends JFrame { private Container container; public SwingApp(String title){ super(title); container = super.getContentPane(); super.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } public static void main(String[] args){ SwingApp sapp = new SwingApp("Test"); JLabel b=new JLabel("hey"); sapp.container.add(b, BorderLayout.SOUTH); sapp.setVisible(true); sapp.pack(); } } ========================================================================== 若是不用Container 我在linux下測他會噴error (windows下我確定能加,以前try過) Exception in thread "main" java.lang.Error: Do not use SwingApp.add() use SwingApp.getContentPane().add() instead at javax.swing.JFrame.createRootPaneException(JFrame.java:465) at javax.swing.JFrame.addImpl(JFrame.java:491) at java.awt.Container.add(Container.java:307) at SwingApp.main(SwingApp.java:19) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 163.26.34.105 ※ 編輯: qrtt1 來自: 163.26.34.105 (05/23 19:16)
文章代碼(AID): #14SkqVr2 (java)
討論串 (同標題文章)
本文引述了以下文章的的內容:
以下文章回應了本文
完整討論串 (本文為第 2 之 4 篇):
文章代碼(AID): #14SkqVr2 (java)