简体   繁体   English

Swing的JPanel不像AWT的Panel吗? 前者的小部件没有显示出来

[英]Isn't Swing's JPanel pretty much like AWT's Panel? The former's widget is not showing up

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. 我正在将一个程序从AWT更改为Swing,正如Java Swing的书第二章所提出的那样,当我从Panel更改为JPanel时,面板就会消失。 The same doesn't happen when I change from Button to JButton. 当我从Button更改为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). 这似乎是一个错误,因为这样做似乎很简单 - 只需在名称中添加一个额外的J - 但我不确定问题出在哪里 - 我的VM(Sun JDK),我的WM( xmonad)或者我正在编程的方式(Clojure的Java支持)。 Any idea? 任何的想法?

As previously stated, I'm writing it in Clojure (a lisp-like language for the JDK). 如前所述,我在Clojure(JDK的类似lisp的语言)中编写它。 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 谢谢

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. 如果JPanel不可见,可能是因为它没有自动调整大小以适应其子元素。

Also try (.pack) instead of (.setSize 450 250) on the frame. 也可以在框架上尝试(.pack)而不是(.setSize 450 250)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM