Swing 的 JPanel 不是很像 AWT 的 Panel 吗?前者的小部件未显示

发布于 2024-08-06 19:55:57 字数 1905 浏览 5 评论 0原文

我正在将程序从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪你到最终 2024-08-13 19:55:57

我注意到您在设置布局之前添加子组件。首先尝试设置布局。问题可能是当您更改布局时默认约束信息会丢失。如果 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文