2011-07-25 42 views
2

同樣的問題,不同的背景Java Swing - 添加圖標或文本時JLabel寬度是否改變?

看來我太過於草率地接受我之前,因爲問題仍然存在。問題?當JLabel添加內容時,可以自由擴展其父面板。

這時候每 「氣墊船充滿鰻魚」 重現它-ses建議,在這裏它是:

import java.awt.*; 
import javax.swing.*; 

public class TestLabel {  
    public static void main(String[] args) { 
    // Var inits 
     JFrame frame; 
    JPanel panel; 
    JLabel label; 
    Container pane; 
    GridBagConstraints gbc = new GridBagConstraints(); 

    // Frame, content pane, layout inits 
    frame = new JFrame("Label Tester"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   

     pane = frame.getContentPane(); 
     pane.setLayout(new GridBagLayout()); 

     gbc.weighty = 1; 
     gbc.fill = GridBagConstraints.BOTH; 


     // Add panels (note gbc weighty and fill carries over all instances) 
     gbc.weightx = 0.3; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     panel = new JPanel(); 
     panel.setBackground(Color.GREEN); 
     frame.add(panel,gbc); 

     label = new JLabel("THE PANEL IS NOW DISTORTED TO FIT THIS LABEL WHY IS THIS HAPPENING"); 
     //label = new JLabel(""); 
     label.setOpaque(true); 
     label.setBackground(Color.WHITE); 
     panel.add(label); 

     gbc.weightx = 0.7; 
     gbc.gridx = 1; 
     gbc.gridy = 0; 
     panel = new JPanel(); 
     panel.setBackground(Color.RED); 
     frame.add(panel,gbc); 

     gbc.weightx = 0.3; 
     gbc.gridx = 0; 
     gbc.gridy = 1; 
     panel = new JPanel(); 
     panel.setBackground(Color.BLUE); 
     frame.add(panel,gbc); 

     gbc.weightx = 0.7; 
     gbc.gridx = 1; 
     gbc.gridy = 1; 
     panel = new JPanel(); 
     panel.setBackground(Color.YELLOW); 
     frame.add(panel,gbc); 

     frame.pack(); 
     frame.setSize(800,600); 

     frame.setVisible(true); 
    } 
} 

結果:

enter image description here

正如你所看到的,綠當文本(或原始問題和圖標)被添加到它時,面板被強制變寬並拋出整個佈局。無論內容如何,​​我都希望我的佈局保持相同的權重。這是因爲我試圖將縮放後的圖像作爲圖標添加到標籤中,如原始問題所示。

順便說一句,setPreferredSize()似乎並不奏效。

有沒有辦法解決這個問題?當我添加一個圖標它


原始的問題

我的JLabel元素極大地擴展。這是爲什麼發生?下面的代碼的適用部分:寬度W的我想要做的就是伸展我的圖標(紅色)

// Show label and BG color 
redLabel.setBackground(Color.RED); 
redLabel.setOpaque(true); 

// Grab stretched image (already loaded elsewhere in the code) and turn to icon 
Img = Img.getScaledInstance(redLabel.getWidth(),12,Image.SCALE_REPLICATE); 
ImageIcon icon = new ImageIcon(Img); 

// This line throws everything off! 
//It's commented out in the first pic, and included in the second. 
redLabel.setIcon(icon); 

你可以從第一張圖看,我有一個標籤寬度W並放入標籤中。

當我這樣做時,標籤會展開(正好是50像素,我認爲)並擠壓左邊緣(綠色)。有誰知道爲什麼會發生這種情況?

我已經試過幾件事情是太詳細解釋,但無法找到問題: -/

enter image description here

+0

這是正常行爲。 – Moonbeam

+0

@Op。你使用什麼樣的layoutmanager? – Kaj

+0

@Kaj - GridBagLayout,我很確定我的權重是正確的。 – Ben

回答

3

您的組件因其爲Icon分配了必要的空間而展開。


public class JLabelDemo { 
    private static BufferedImage bi; 

    public static void main(String[] args) throws IOException{ 
     loadImage(); 

     SwingUtilities.invokeLater(new Runnable(){ 
      @Override 
      public void run() { 
       createAndShowGUI();    
      } 
     }); 
    } 

    private static void loadImage() throws IOException{ 
     bi = ImageIO.read(JLabelDemo.class.getResource("../resource/forever-alone.jpg")); 
    } 

    private static void createAndShowGUI(){ 
     final JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     final JPanel panel = new JPanel(); 
     panel.setBackground(Color.YELLOW); 
     panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
     final JLabel emptyLabel = new JLabel(); 
     final JLabel textLabel = new JLabel("This label has text only"); 
     final JLabel textAndImageLabel = new JLabel("This label has text and image"); 
     textAndImageLabel.setIcon(new ImageIcon(bi)); 

     panel.add(emptyLabel); 
     panel.add(textLabel); 
     panel.add(textAndImageLabel); 
     frame.add(panel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     System.out.println("Empty label dimensions - " + emptyLabel.getSize()); 
     System.out.println("Text only label dimensions - " + textLabel.getSize()); 
     System.out.println("Image width: " + bi.getWidth() + ", Image height: " + bi.getHeight()); 
     System.out.println("Text and image label dimensions - " +textAndImageLabel.getSize()); 
    } 
} 

enter image description here


以下被輸出到控制檯:

Empty label dimensions - java.awt.Dimension[width=0,height=0] 
Text only label dimensions - java.awt.Dimension[width=129,height=16] 
Image width: 194, Image height: 180 
Text and image label dimensions - java.awt.Dimension[width=363,height=180] 
+1

Ahhhhahaha美麗的感謝,這看起來像一個勝利者。你有圖像和動畫GIF的最高級味道,順便說一句。步步高昇。 – Ben

+1

土豆+1 – mKorbel

1

的Java Swing對我來說是很老,但如果我記得很清楚,設置首選大小(setPreferredSize())有時可以解決這些問題...另請嘗試使用setMaximumSize和setMinimumSize進行頂層佈局。

你也許可以Java文檔中找到更多的信息: http://download.oracle.com/javase/tutorial/uiswing/layout/using.html#sizealignment

商祺!

+1

@Moonbeam。你什麼意思? JLabel如何調整大小的行爲取決於JLabel所在組件使用的佈局管理器。 – Kaj

+0

@Hovercraft全部的鰻魚,也許我只是困惑的問題。當你向'JLabel'添加一個'Icon'時,組件會根據文本和圖像的尺寸來調整自己的尺寸,不是嗎? – Moonbeam

2

考慮使用的JLayeredPane在層添加組件。在做不透明度,尺寸和添加元件的位置時,有些旅行和陷阱。

例如,

import java.awt.*; 
import javax.swing.*; 

public class TestLabel { 
    private static final Dimension SIZE = new Dimension(800, 600); 

    public static void main(String[] args) { 
     GridBagConstraints gbc = new GridBagConstraints(); 


     JPanel defaultPane = new JPanel(); 
     defaultPane.setLayout(new GridBagLayout()); 

     gbc.weighty = 1; 
     gbc.fill = GridBagConstraints.BOTH; 

     // Add panels (note gbc weighty and fill carries over all instances) 
     gbc.weightx = 0.3; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     JPanel panel = new JPanel(); 
     panel.setBackground(Color.GREEN); 
     defaultPane.add(panel, gbc); 

     gbc.weightx = 0.7; 
     gbc.gridx = 1; 
     gbc.gridy = 0; 
     panel = new JPanel(); 
     panel.setBackground(Color.RED); 
     defaultPane.add(panel, gbc); 

     gbc.weightx = 0.3; 
     gbc.gridx = 0; 
     gbc.gridy = 1; 
     panel = new JPanel(); 
     panel.setBackground(Color.BLUE); 
     defaultPane.add(panel, gbc); 

     gbc.weightx = 0.7; 
     gbc.gridx = 1; 
     gbc.gridy = 1; 
     panel = new JPanel(); 
     panel.setBackground(Color.YELLOW); 
     defaultPane.add(panel, gbc); 
     defaultPane.setSize(SIZE); 

     JLabel label = new JLabel("THE PANEL IS NOW DISTORTED TO FIT THIS LABEL WHY IS THIS HAPPENING"); 
     label.setOpaque(true); 
     label.setBackground(Color.WHITE); 

     JPanel northPalettePanel = new JPanel(); 
     northPalettePanel.setOpaque(false); 
     northPalettePanel.add(label); 

     JPanel palettePanel = new JPanel(new BorderLayout()); 
     palettePanel.setOpaque(false); 
     palettePanel.setSize(SIZE); 
     palettePanel.setLocation(0, 0); 
     palettePanel.add(northPalettePanel, BorderLayout.NORTH); 

     JLayeredPane layeredPane = new JLayeredPane(); 
     layeredPane.setPreferredSize(SIZE); 
     layeredPane.add(defaultPane, JLayeredPane.DEFAULT_LAYER); 
     layeredPane.add(palettePanel, JLayeredPane.PALETTE_LAYER); 

     JFrame frame = new JFrame("Label Tester"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(layeredPane, BorderLayout.CENTER); 
     frame.pack(); 

     frame.setVisible(true); 
    } 
}