2014-07-26 6 views
6

처음에 난 그냥GridBagLayout에 정렬되지 이미지가 제대로

public class GuiTut extends JPanel { 
    private GridBagConstraints c = new GridBagConstraints(); 
    private JLabel deckLabel = new JLabel(); 

    public GuiTut() { 
     setLayout(new GridBagLayout()); 

     try { 
      deck = ImageIO.read(new File("resources/images/deck.jpg")); 
     } catch (Exception e) {} 

     c.gridx = 0; 
     c.gridy = 0; 
     JLabel deckPic = new JLabel(new ImageIcon(deck));  
     add(deckPic, c); 

     deckLabel.setText("Deck"); 
     c.gridx = 0; 
     c.gridy = 1; 
     add(deckLabel, c); 
} 

하지만 내 gridLayout 패널을 추가 한 후

pic1, 내 모든 GUI를 잘 보이는 이미지 아래 갑판의 이미지 및 텍스트 "데크"가 디자인이 엉망이되었습니다. 내 갑판 이미지가 내 gridLayout 의 첫 번째 행과 제대로 정렬되지 않아 내 텍스트 "갑판"이 몇 개의 넓은 공간으로 구분되어 있습니다.

public class GuiTut extends JPanel { 
    private GridBagConstraints c = new GridBagConstraints(); 
    private JLabel deckLabel = new JLabel(); 
    private JPanel gridLayoutPanel = new JPanel(new GridLayout(2, 0)); 
    private JLabel[] label = new JLabel[14]; 

    public GuiTut() { 
     setLayout(new GridBagLayout()); 

     try { 
      card = ImageIO.read(new File("resources/images/card.jpg")); 
     } catch (Exception e) {} 

     for(int i = 0; i < 14; i++) { 
      label[i] = new JLabel(new ImageIcon(card); 
      gridLayoutPanel.add(label[i]); 
     } 

     try { 
      deck = ImageIO.read(new File("resources/images/deck.jpg")); 
     } catch (Exception e) {} 

     c.gridx = 0; 
     c.gridy = 0; 
     JLabel deckPic = new JLabel(new ImageIcon(deck));  
     add(deckPic, c); 

     deckLabel.setText("Deck"); 
     c.gridx = 0; 
     c.gridy = 1; 
     add(deckLabel, c); 

     c.gridx = 1; 
     c.gridy = 0; 
     add(gridLayoutPanel, c); 
} 

내가 원하는 것은 갑판 이미지

pic2

gridLayout의 첫 번째 행과 바로 갑판 이미지 아래의 텍스트 "deck"으로 정렬합니다하지만 난 그것을 얻을 수없는 것.

답변

3

HotLinked 이미지

갑판 레이블

enter image description hereenter image description here

  1. , 당신은 단지 수평/수직 텍스트 poistions

    JLabel label = new JLabel(new ImageIcon(new URL(DECK_URL))); 
    label.setText("DECK"); 
    label.setHorizontalTextPosition(JLabel.CENTER); 
    label.setVerticalTextPosition(JLabel.BOTTOM); 
    
  2. 을 레이블의 텍스트를 설정하고 설정할 수 있습니다 데크 위치 지정 문제의 경우, 외부 패널에 대해 BorderLayout과 같은 다른 레이아웃 관리자를 사용할 수 있으며, 기본 흐름 레이어 ut를 데크 패널 용으로 사용하십시오. 카드는 여전히 GridLayout입니다. 당신이

enter image description here

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.net.URL; 

import javax.swing.ImageIcon; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class GuiTest { 

    private static final String DECK_URL = "http://i.stack.imgur.com/xNffR.png"; 
    private static final String CARD_URL = "http://i.stack.imgur.com/uVS1D.png"; 

    private static JPanel createDeckPanel() { 
     JPanel panel = new JPanel(); 
     try { 
      JLabel label = new JLabel(new ImageIcon(new URL(DECK_URL))); 
      label.setText("DECK"); 
      label.setHorizontalTextPosition(JLabel.CENTER); 
      label.setVerticalTextPosition(JLabel.BOTTOM); 
      panel.add(label); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return panel; 
    } 

    private static JPanel createCenterPanel(int rows, int cols) { 
     JPanel panel = new JPanel(new GridLayout(rows, cols)); 
     for (int i = 0; i < rows*cols; i++) { 
      try { 
       panel.add(new JLabel(new ImageIcon(new URL(CARD_URL)))); 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 
     } 
     return panel; 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable(){ 
      public void run() { 
       JPanel panel = new JPanel(new BorderLayout()); 
       panel.add(createDeckPanel(), BorderLayout.LINE_START); 
       panel.add(createCenterPanel(2, 6)); 
       JOptionPane.showMessageDialog(null, panel); 
      } 
     }); 
    } 
} 
을 필요로 할 때 그것은 둥지 패널에 일반적으로 도움이
관련 문제