2011-12-20 25 views
7

thế nào tôi có thể trả về một cách chính xác XxxSize từ JComponent (s) bổ sung vào JLabelLàm thế nào trả XxxSize từ JComponent (s) bổ sung vào JLabel

1st. con số >>lets LayoutManager works like as for JPanel, JLabel returns Size(0, 0)

enter image description here

2nd. con số >>added some PreferredSize to the JLabel

enter image description here

3rd. con số >>calculated PreferredSize from JComponent(s) added to the JLabel

enter image description here

4. con số >>lets LayoutManager works changed JLabel to JPanel, now LayoutManager correctly calculated Dimension without using any XxxSize

enter image description here

thông báo mặt sáu có được sử dụng Nimbus L & F, cùng công suất là có cho tất cả L accesible & F

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 Câu hỏi hay, nhưng tại sao làm cho 'buttonPanel' thành' JLabel'? – trashgod

+1

@trashgod vì 1) không thể thiết lập Opacity hoặc Translucency cho tất cả biết Nhìn và Cảm thấy, ví dụ bằng cách sử dụng Nimbus yêu cầu một Woodoo cho rằng, 2) Tôi thấy nhà máy popup đẹp dựa trên JLabel – mKorbel

+0

trong khi kỹ thuật có thể, nó đơn giản sai để sử dụng một JLabel là vùng chứa ... – kleopatra

Trả lời

7

Giá trị mặc định tính toán kích thước ưa thích là sử dụng cách bố trí người quản lý để xác định kích thước ưa thích của một thành phần. Điều này có nghĩa là trình quản lý bố cục lặp qua tất cả các thành phần con để xác định kích thước ưa thích của mỗi thành phần. Đối với JPanel, được sử dụng như một Container, phép tính này được sử dụng.

Tuy nhiên, đối với các thành phần Swing khác, phương thức getPreferredSize() luôn được ghi đè để cung cấp kích thước hợp lý cho thành phần đã cho.

Trong trường hợp JLabel, phép tính kích thước được ưu tiên sẽ tính đến văn bản và biểu tượng được sử dụng. Vì bạn không cung cấp kích thước ưa thích là 0. Tất nhiên nếu bạn tự ghi đè lên tính toán này bằng cách sử dụng phương thức setPreferredSize() thì thành phần sẽ có kích thước ưa thích. Vì vậy, mặc dù Swing cho phép bạn thêm các thành phần vào bất kỳ thành phần nào và sử dụng trình quản lý bố cục để bố trí các thành phần con, các thành phần con này không được sử dụng trong phép tính kích thước ưa thích.

Đây không chỉ là vấn đề về Nimbus.

+3

+1 để làm sáng tỏ bản chất của kích thước ưa thích. – trashgod

Các vấn đề liên quan