Swing 的 JPanel 不是很像 AWT 的 Panel 吗?前者的小部件未显示
我正在将程序从 AWT 更改为 Swing,正如 Java Swing 书中第二章所建议的那样,当我从 Panel 更改为 JPanel 时,面板就消失了。当我从 Button 更改为 JButton 时,不会发生同样的情况。
这似乎是一个错误,因为这样做似乎非常简单 - 只需在名称中添加一个额外的 J - 但我不确定问题出在哪里 - 使用我的 VM (Sun JDK),使用我的 WM ( xmonad)或我的编程方式(Clojure 的 Java 支持)。有什么想法吗?
如前所述,我用 Clojure(JDK 的一种类似 lisp 的语言)编写它。这是我的代码:
(defn main [] (let [toolbar-frame (Frame. "Toolbar Example (Swing)") cut-button (JButton. "Cut") copy-button (JButton. "Copy") paste-button (JButton. "Paste") java-button (JButton. "Java") windows-button (JButton. "Windows") mac-button (JButton. "Mac") motif-button (JButton. "Motif") lnf-panel (JPanel.) toolbar-panel (Panel.) print-listener (proxy [ActionListener] [] (actionPerformed [evt] (.getActionCommand evt))) ] (.addWindowListener toolbar-frame (proxy [WindowAdapter] [] (windowClosing [e] (System/exit 0)))) ;(doto windows-button (.addActionListener lnf-listener)) ;(doto motif-button (.addActionListener lnf-listener)) ;(doto mac-button (.addActionListener lnf-listener)) ;(doto java-button (.addActionListener lnf-listener)) (doto cut-button (.addActionListener print-listener)) (doto copy-button (.addActionListener print-listener)) (doto paste-button (.addActionListener print-listener)) (doto lnf-panel (.add windows-button) (.add java-button) (.add mac-button) (.add motif-button) (.setLayout (FlowLayout. FlowLayout/LEFT))) (doto toolbar-panel (.add cut-button) (.add copy-button) (.add paste-button) (.setLayout (FlowLayout. FlowLayout/LEFT))) (doto toolbar-frame (.add toolbar-panel BorderLayout/NORTH) (.add lnf-panel BorderLayout/SOUTH) (.setSize 450 250) (.setVisible true))))
谢谢
I'm changing a program from AWT to Swing, as proposed on the second chapter of Java Swing's book, and the panel just disappears when I do the change from Panel to JPanel. The same doesn't happen when I change from Button to JButton.
It seems to be a bug, since it appears to be trivially simple to do so - just adding an extra J to the name - but I'm not sure where the problem is - with my VM (Sun JDK), with my WM (xmonad) or with the way I'm programming (Clojure's Java Support). Any idea?
As previously stated, I'm writing it in Clojure (a lisp-like language for the JDK). Here is my code:
(defn main [] (let [toolbar-frame (Frame. "Toolbar Example (Swing)") cut-button (JButton. "Cut") copy-button (JButton. "Copy") paste-button (JButton. "Paste") java-button (JButton. "Java") windows-button (JButton. "Windows") mac-button (JButton. "Mac") motif-button (JButton. "Motif") lnf-panel (JPanel.) toolbar-panel (Panel.) print-listener (proxy [ActionListener] [] (actionPerformed [evt] (.getActionCommand evt))) ] (.addWindowListener toolbar-frame (proxy [WindowAdapter] [] (windowClosing [e] (System/exit 0)))) ;(doto windows-button (.addActionListener lnf-listener)) ;(doto motif-button (.addActionListener lnf-listener)) ;(doto mac-button (.addActionListener lnf-listener)) ;(doto java-button (.addActionListener lnf-listener)) (doto cut-button (.addActionListener print-listener)) (doto copy-button (.addActionListener print-listener)) (doto paste-button (.addActionListener print-listener)) (doto lnf-panel (.add windows-button) (.add java-button) (.add mac-button) (.add motif-button) (.setLayout (FlowLayout. FlowLayout/LEFT))) (doto toolbar-panel (.add cut-button) (.add copy-button) (.add paste-button) (.setLayout (FlowLayout. FlowLayout/LEFT))) (doto toolbar-frame (.add toolbar-panel BorderLayout/NORTH) (.add lnf-panel BorderLayout/SOUTH) (.setSize 450 250) (.setVisible true))))
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我注意到您在设置布局之前添加子组件。首先尝试设置布局。问题可能是当您更改布局时默认约束信息会丢失。如果 JPanel 不可见,可能是因为它尚未自动调整大小以适合其子元素。
还可以尝试在框架上使用
(.pack)
而不是(.setSize 450 250)
。I notice you are adding the child components before setting the layout. Try setting the layout first. The problem may be that the default constraint information is lost when you change the layout. If the JPanel is invisible it may be because it has not been auto-sized to fit its child elements.
Also try
(.pack)
instead of(.setSize 450 250)
on the frame.