2012-03-25 32 views

Respuesta

7

GridLayout ignora efectivamente tamaño preferido de un componente, pero se puede controlar el aspect ratio de lo que está dibujado en paintComponent(), como se muestra en este example. La forma representada sigue siendo circular (aspecto 1: 1), mientras que (casi) llena el contenedor en la dimensión más estrecha. Cambie el tamaño del marco para ver el efecto.

Adición: Por ejemplo, agregué N * N instancias de CirclePanel a GridLayout a continuación.

enter image description here

import java.awt.*; 
import java.awt.event.*; 
import java.util.Random; 
import javax.swing.*; 

/** 
* @see https://stackoverflow.com/a/9858355/230513 
* @see https://stackoverflow.com/a/3538279/230513 
*/ 
public class SwingPaint { 

    private static final int N = 4; 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       JFrame frame = new JFrame(); 
       frame.setLayout(new GridLayout(N, N)); 
       for (int i = 0; i < N * N; i++) { 
        frame.add(new CirclePanel()); 
       } 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    private static class CirclePanel extends JPanel { 

     private static final Random r = new Random(); 

     public CirclePanel() { 
      this.setPreferredSize(new Dimension(80, 80)); 
      this.setForeground(new Color(r.nextInt())); 
      this.addMouseListener(new MouseAdapter() { 

       @Override 
       public void mousePressed(MouseEvent e) { 
        CirclePanel.this.update(); 
       } 
      }); 
     } 

     public void update() { 
      this.setForeground(new Color(r.nextInt())); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Dimension size = this.getSize(); 
      int d = Math.min(size.width, size.height) - 10; 
      int x = (size.width - d)/2; 
      int y = (size.height - d)/2; 
      g.fillOval(x, y, d, d); 
      g.setColor(Color.blue); 
      g.drawOval(x, y, d, d); 
     } 
    } 
} 
+0

Tiene que haber algo por lo que no tiene ningún ejemplo, me pregunto !!!! :-) –

+0

Soy un fan de la [sscce] (http://sscce.org/). :-) – trashgod

+0

OK, entonces las cosas deben hacerse en paintComponent. He estado jugando, todavía no he resuelto el problema. Por el momento, estoy buscando poner mi grilla en un JPanel intermediario (JPanel -> intermediario JPanel -> GridLayout). Estoy intentando controlar el tamaño de este JPanel intermediario en el componente paint del JPanel raíz. Cómo te sientes sobre eso ? Havent ya funcionaba ... – Jerome

Cuestiones relacionadas