2014-07-26 7 views
6

शुरू में मैं एक डेक छवि और एक पाठ "डेक" बस छवि है जो ठीकGridBagLayout संरेखित नहीं छवियां ठीक

pic1

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 पैनल जोड़ने के लिए, मेरे पूरे जीयूआई लग रहा है नीचे है डिजाइन गड़बड़ कर दिया गया है। जैसा कि आप देख सकते हैं कि मेरी डेक छवि को मेरे ग्रिडलेआउट की पहली पंक्ति के साथ ठीक से संरेखित नहीं किया गया है और मेरा टेक्स्ट "डेक" कुछ विस्तृत स्थान से अलग किया गया है।

pic2

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); 
} 

क्या मैं चाहता हूँ डेक छवि है 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. सेट कर सकते हैं डेक पोजिशनिंग समस्या के लिए, आप बाहरी पैनल के लिए बॉर्डरलेआउट जैसे एक अलग लेआउट मैनेजर का उपयोग कर सकते हैं, और एक डिफ़ॉल्ट प्रवाह लेओ डेक पैनल के लिए। कार्ड अभी भी ग्रिडलाउट होंगे। यह घोंसला पैनलों के लिए आम तौर पर उपयोगी है जब आप करने के लिए

उदाहरण

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); 
      } 
     }); 
    } 
} 
की जरूरत
संबंधित मुद्दे