Skip to content

Instantly share code, notes, and snippets.

@ProgDan
Created August 31, 2012 18:19
Show Gist options
  • Save ProgDan/3556863 to your computer and use it in GitHub Desktop.
Save ProgDan/3556863 to your computer and use it in GitHub Desktop.
Exemplo: GridBagLayout
import java.awt.*;
import javax.swing.*;
public class MinhaJanela extends JPanel {
public MinhaJanela() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
setLayout(gridbag);
// Distribuicao horizontal de espaço
c.weightx = 1.0;
makeButton("Button 1", gridbag, c);
makeButton("Button 2", gridbag, c);
makeButton("Button 3", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER;
makeButton("Button 4", gridbag, c);
c.weightx = 0.0;
makeButton("Button 5", gridbag, c);
c.gridwidth = GridBagConstraints.RELATIVE;
makeButton("Button 6", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER;
makeButton("Button 7", gridbag, c);
c.gridwidth = 1;
c.gridheight = 2;
c.weightx = 1.0; // Distribuição vertical do espaço
makeButton("Button 8", gridbag, c);
c.weightx = 0.0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 1;
makeButton("Button 9", gridbag, c);
makeButton("Button 10", gridbag, c);
}
void makeButton(String name, GridBagLayout gridbag, GridBagConstraints c) {
JButton button = new JButton(name);
gridbag.setConstraints(button, c);
add(button);
}
}
import java.awt.*;
import javax.swing.*;
public class Principal extends JFrame {
public Principal() {
super("Minha Janela");
setSize(400, 300);
Container c = getContentPane();
MinhaJanela janela = new MinhaJanela();
c.add(janela);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Principal();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment