2012-06-26 15 views
5

Estoy creando un JFrame sobre mi programa. Tengo un ícono que utilicé para el programa y lo tengo como lo primero en JFrame, pero tengo problemas para centrar la imagen. Si hago algún tipo de centrado, estropea toda la alineación de todo lo demás.Imagen de centrado en un JFrame?

Estoy tratando de tener todos los JLabels, además del icono, a la izquierda alineados. Luego, haz que el ícono se alinee con el centro.

Tuve que eliminar algunos datos personales, lo que sea que haya eliminado los pongo entre "[]".

import java.awt.Dimension; 
import java.awt.Font; 

import javax.swing.BorderFactory; 
import javax.swing.Box; 
import javax.swing.BoxLayout; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class About extends JFrame { 

    public About() { 
     super("About [PROGRAM]"); 
     setIconImage([PROGRAM].getInstance().setIcon()); 

     JPanel main = new JPanel(); 

     main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS)); 
     main.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); 

     JLabel icon = new JLabel("", new ImageIcon(getClass().getResource(Constants.ICON_FULL)), JLabel.CENTER);   
     JLabel name = new JLabel("[PROGRAM]"); 
     JLabel expandedName = new JLabel("[PROGRAM DESCRIPTION]"); 
     JLabel copyright = new JLabel("[COPYRIGHT JUNK]"); 
     JLabel credits = new JLabel("[CREDITS]"); 

     name.setFont(new Font(name.getFont().getFamily(), Font.BOLD, 18)); 

     copyright.setBorder(BorderFactory.createEmptyBorder(0,0,10,0)); 

     main.add(icon); 
     main.add(Box.createRigidArea(new Dimension(0, 10))); 
     main.add(name); 
     main.add(expandedName); 
     main.add(copyright); 
     main.add(credits); 

     add(main); 

     pack(); 
     setLocationRelativeTo(null); 
     setVisible(true); 
    } 

} 
+1

desea que el icono que se centra en el fondo? o simplemente centrado con el texto a su alrededor? – Soronthar

+0

Ninguno. Quería tener una imagen y un par de etiquetas apiladas una sobre otra, pero tener la imagen centrada. – samwell

Respuesta

4

considerar el uso de algunos diseños para ayudarle a salir. Los que me vienen a la mente incluyen BorderLayout con el ícono en la posición BorderLayout.CENTER. Puedes apilar cosas en un lado usando BoxLayout usando JPanel que se agrega al JPanel principal que usa BorderLayout.

por ejemplo,

import java.awt.BorderLayout; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 

import javax.imageio.ImageIO; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class About extends JDialog { 
    public static final String IMAGE_PATH = "http://upload.wikimedia.org/wikipedia/" 
     + "commons/thumb/3/39/European_Common_Frog_Rana_temporaria.jpg/" 
     + "800px-European_Common_Frog_Rana_temporaria.jpg"; 

    public About(JFrame frame) { 
     super(frame, "About [PROGRAM]", true); 

     ImageIcon myIcon = null; 
     try { 
     URL imgUrl = new URL(IMAGE_PATH); 
     BufferedImage img = ImageIO.read(imgUrl); 
     myIcon = new ImageIcon(img); 
     } catch (MalformedURLException e) { 
     e.printStackTrace(); 
     System.exit(-1); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     System.exit(-1); 
     } 

     JPanel main = new JPanel(new BorderLayout()); 

     main.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 

     JLabel centerLabel = new JLabel(myIcon); 
     JLabel name = new JLabel("[PROGRAM]"); 
     JLabel expandedName = new JLabel("[PROGRAM DESCRIPTION]"); 
     JLabel copyright = new JLabel("[COPYRIGHT JUNK]"); 
     JLabel credits = new JLabel("[CREDITS]"); 

     name.setFont(new Font(name.getFont().getFamily(), Font.BOLD, 18)); 

     copyright.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0)); 

     int eb = 20; 
     centerLabel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb)); 

     JPanel leftPanel = new JPanel(); 
     leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS)); 
     leftPanel.add(name); 
     leftPanel.add(Box.createVerticalGlue()); 
     leftPanel.add(expandedName); 
     leftPanel.add(copyright); 
     leftPanel.add(credits); 
     leftPanel.add(Box.createVerticalGlue()); 

     main.add(centerLabel, BorderLayout.CENTER); 
     main.add(leftPanel, BorderLayout.LINE_START); 

     add(main); 

     pack(); 
    } 

    public static void main(String[] args) { 
     final JFrame frame = new JFrame("GUI"); 
     JPanel panel = new JPanel(); 
     panel.add(new JButton(new AbstractAction("About") { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      About about = new About(frame); 
      about.setLocationRelativeTo(frame); 
      about.setVisible(true); 
     } 
     })); 
     frame.add(panel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 
+1

@ chudapati09: ¡de nada! Espero que te haya gustado la rana. –

+0

Jaja, no me di cuenta de eso. Acabo de aplicar tus conceptos de código a mi código. Sin embargo, eso fue muy divertido. – samwell