GridBagLayout

0 respostas
Guitar_Men
Pessoal estou tentando criar um JPanel específico para minha necessidade. Ele precisa ter uma espécie de cabeçalho em degrade que esta feito:
public class PainelGradiente extends JPanel {
    JLabel lblTitulo = null;

   public PainelGradiente(String titulo) {
      this.lblTitulo = new JLabel(titulo);
      this.add(this.lblTitulo);
   }

   @Override
   protected void paintComponent(Graphics g) {
      Graphics2D g2 = (Graphics2D) g;
      GradientPaint gp = new GradientPaint(1.0F, // x1
                                           2.0F, // y1
                                           Color.WHITE, // cor 1
                                           0, // x2
                                           (float) getHeight(), // y2
                                           Color.BLUE); //cor 2

      g2.setPaint(gp);
      g2.fillRect(0, 0, getWidth(), getHeight());
      super.paintChildren(g2);
   }


}
Ai eu tenho a classe PainelDegradeDetalhe que é um Panel com aquele panel degrade como header.
public class PainelDegradeDetalhe extends JPanel {

    private PainelGradiente painelGradiente = null;
    private JLabel labelTeste = null;
    private GridBagLayout layout = null;
    private GridBagConstraints c = null;
    private int ultimaLinha = 0;

    public PainelDegradeDetalhe(String tituloPainel, String texto) {
        this.painelGradiente = new PainelGradiente(tituloPainel);
        this.adicionaComponente(painelGradiente);
        setBackground(Color.WHITE);
        this.inicializaComponente(texto);

    }

    private void inicializaComponente(String texto) {

        this.labelTeste = new JLabel(texto);
        this.adicionaComponente(labelTeste);
    }

    public void adicionaComponente(JComponent component) {
        try {
            if (this.layout == null) {
                layout = new GridBagLayout();
                c = new GridBagConstraints();
                c.gridwidth = 1;
                setLayout(layout);
            }
            if (ultimaLinha == 0) {
                c.fill = GridBagConstraints.HORIZONTAL;
                
            }
            else{
                c.fill = GridBagConstraints.BOTH;
            }
            c.gridy = ultimaLinha;
            c.gridx = 0;
            add(component, c);
            ultimaLinha++;

        } catch (Exception e) {
        }
    }
}
Porém ao rodar o seguinte código:
public class GridBagLayoutDemo {
    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;
    final static boolean RIGHT_TO_LEFT = false;

    public static void addComponentsToPane(Container pane) {
        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }

        JButton button;
	pane.setLayout(new GridBagLayout());
	GridBagConstraints c = new GridBagConstraints();
	if (shouldFill) {
	//natural height, maximum width
	c.fill = GridBagConstraints.HORIZONTAL;
	}

	button = new JButton("Button 1");
	if (shouldWeightX) {
	c.weightx = 0.5;
	}
	c.fill = GridBagConstraints.HORIZONTAL;
	c.gridx = 0;
	c.gridy = 0;
	//pane.add(new PainelGradiente("Teste"), c); //LINHA 1
        pane.add(new PainelDegradeDetalhe("Teste", "testando layout"), c); //LINHA 2
    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("GridBagLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane.
        addComponentsToPane(frame.getContentPane());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Da forma que está não consigo com que o degradê ocupe a célula toda, ja se eu comentar aonde diz LINHA 2 e descomentar aonde diz LINHA 1 o degradê funciona perfeitamente. Estou começando agora a estudar sobre layouts a té li o artigo do guj, mas confesso que estou me batendo um pouco. Se alguém puder me ajudar....

Valew

Criado 18 de dezembro de 2009
Respostas 0
Participantes 1