2011-12-20 12 views
7

कैसे कर सकते हैं मैं सही ढंग से JComponent (रों) से XxxSize रिटर्न को जोड़ा गया JLabelकैसे JComponent से XxxSize रिटर्न (रों) JLabel

1 को जोड़ा गया। आंकड़ा >>lets LayoutManager works like as for JPanel, JLabel returns Size(0, 0)

enter image description here

2। आंकड़ा >>added some PreferredSize to the JLabel

enter image description here

3। आंकड़ा >>calculated PreferredSize from JComponent(s) added to the JLabel

enter image description here

4। आंकड़ा >>lets LayoutManager works changed JLabel to JPanel, now LayoutManager correctly calculated Dimension without using any XxxSize

enter image description here

नोटिस साईस वहाँ प्रयोग किया जाता है निंबस एल & एफ, एक ही आउटपुट सब नहीं पहुंच एल के लिए है & एफ

import java.awt.*; 
import java.awt.event.*; 
import java.util.LinkedList; 
import java.util.Queue; 
import javax.swing.*; 

public class NimbusBorderPainterDemo extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JFrame frame = new JFrame(); 
    private JPanel fatherPanel = new JPanel(), titlePanel = new JPanel(); 
    private JLabel buttonPanel = new JLabel(); 


    //figure ---> 4th. switch JLabel with JPanel 
    //private JPanel buttonPanel = new JPanel(); 
    private Queue<Icon> iconQueue = new LinkedList<Icon>(); 

    public NimbusBorderPainterDemo() { 
     iconQueue.add(UIManager.getIcon("OptionPane.errorIcon")); 
     iconQueue.add(UIManager.getIcon("OptionPane.informationIcon")); 
     iconQueue.add(UIManager.getIcon("OptionPane.warningIcon")); 
     JButton button0 = createButton(); 
     JButton button1 = createButton(); 
     JButton button2 = createButton(); 
     button2.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.exit(1); 
      } 
     }); 
     int gap = 5; 
     buttonPanel.setLayout(new GridLayout(0, 3, gap, 0)); 
     buttonPanel.add(button0); 
     buttonPanel.add(button1); 
     buttonPanel.add(button2); 

     // figure 1st. ---> without PreferredSize 

     // figure 2nd. ---> 
     //buttonPanel.setPreferredSize(new Dimension(160, 30)); 

     // figure 3rd. ---> 
     /*Dimension dim = button0.getPreferredSize(); 
     int w = dim.width; 
     int h = dim.height; 
     w = (w + 5) * 3; 
     h += 4; 
     dim = new Dimension(w, h); 
     buttonPanel.setPreferredSize(dim);*/ 

     titlePanel.setLayout(new BorderLayout()); 
     titlePanel.add(new JLabel(nextIcon()), BorderLayout.WEST); 
     titlePanel.add(new JLabel("My Frame"), BorderLayout.CENTER); 
     titlePanel.setBorder(BorderFactory.createLineBorder(Color.GRAY)); 
     titlePanel.add(buttonPanel, BorderLayout.EAST); 
     fatherPanel.setLayout(new BorderLayout()); 
     fatherPanel.add(titlePanel, BorderLayout.CENTER); 
     frame.setUndecorated(true); 
     frame.add(fatherPanel); 
     frame.setLocation(50, 50); 
     frame.pack(); 
     frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
     frame.setVisible(true); 
    } 

    private JButton createButton() { 
     JButton button = new JButton(); 
     button.setBorderPainted(false); 
     button.setBorder(null); 
     button.setFocusable(false); 
     button.setMargin(new Insets(0, 0, 0, 0)); 
     button.setContentAreaFilled(false); 
     button.setIcon(nextIcon()); 
     //button.setRolloverIcon(nextIcon()); 
     //button.setPressedIcon(nextIcon()); 
     //button.setDisabledIcon(nextIcon()); 
     nextIcon(); 
     return button; 
    } 

    private Icon nextIcon() { 
     Icon icon = iconQueue.peek(); 
     iconQueue.add(iconQueue.remove()); 
     return icon; 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
       } catch (Exception fail) { 
       } 
       UIManager.getLookAndFeelDefaults().put("nimbusFocus", Color.RED); 
       NimbusBorderPainterDemo nimbusBorderPainterDemo = new NimbusBorderPainterDemo(); 
      } 
     }); 
    } 
} 
+1

+1 अच्छा सवाल है, लेकिन 'बटन पैनेल' को 'जेलाबेल' क्यों बनाते हैं? – trashgod

+1

@trashgod क्योंकि 1) संभव नहीं है सभी के लिए अस्पष्टता या पारदर्शिता जानता है और महसूस करता है, उदाहरण के लिए निंबस का उपयोग करके एक और वुडू की आवश्यकता होती है, 2) मैंने तकनीकी रूप से संभव होने पर जेएलएबल – mKorbel

+0

पर आधारित अच्छा पॉपअप फैक्ट्री देखा, यह उपयोग करना आसान है JLabel कंटेनर के रूप में ... – kleopatra

उत्तर

7

डिफ़ॉल्ट वरीय आकार गणना लेआउट का उपयोग करने के लिए है एक घटक के पसंदीदा आकार को निर्धारित करने के लिए प्रबंधक। इसका मतलब यह है कि लेआउट मैनेजर प्रत्येक बच्चे के घटकों के माध्यम से प्रत्येक के पसंदीदा आकार को निर्धारित करने के लिए पुनरावृत्त करता है। एक जेपीनल के लिए, जिसका उपयोग कंटेनर के रूप में किया जाना है, इस गणना का उपयोग किया जाता है।

हालांकि, अन्य स्विंग घटकों के लिए, getPreferredSize() विधि हमेशा दिए गए घटक के लिए उचित आकार प्रदान करने के लिए ओवरराइड होती है।

जेएलएबल के मामले में, पसंदीदा आकार गणना टेक्स्ट और आइकन को ध्यान में रखती है। चूंकि आपने या तो पसंदीदा आकार प्रदान नहीं किया है शून्य है। बेशक अगर आप setPreferredSize() विधि का उपयोग कर मैन्युअल रूप से इस गणना को ओवरराइड करते हैं तो घटक का पसंदीदा आकार होगा।

तो स्विंग आपको किसी भी घटक में घटकों को जोड़ने की अनुमति देता है और बाल घटकों को लेआउट करने के लिए लेआउट मैनेजर का उपयोग करता है, इन बाल घटकों को पसंदीदा आकार गणना में उपयोग नहीं किया जाता है।

यह सिर्फ एक निंबस मुद्दा नहीं है।

+3

+1 पसंदीदा आकार की प्रकृति पर प्रकाश डालने के लिए +1। – trashgod

संबंधित मुद्दे