Isn't Swing JPanel pretty much reminiscent of an AWT panel? Former widget does not appear

I change the program from AWT to Swing, as suggested in the second chapter of the Java Swing book, and the panel just disappears when I make a change from the panel to JPanel. The same thing does not happen when I switch from Button to JButton.

This seems to be a mistake, as it seems trivially simple - 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 program ( Clojure Java Support). Any idea?

As said earlier, I write it in Clojure (a lisp is a JDK-like language). 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

+4
source share
1 answer

I notice that you add child components before adjusting the layout. First try customizing the layout. The problem may be that the default constraint information is lost when the layout changes. If the JPanel is invisible, this may be due to the fact that it was not automatically installed for its children.

Also try (.pack) instead of (.setSize 450 250) in the frame.

+4
source

All Articles