Tips for heathy life

Tips for heathy life

Kamis, 30 Oktober 2008

GRIDBAG LAYOUT in J2SE



I want to share how to GRIDBAG LAYOUT in J2SE. You can use this code but if you copy this code and paste in your blog/website, please insert this link of article.

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;


public class Main {


// public Main() {
//}

public static void main(String[] args) {

JFrame frame =new JFrame("GridBagLayout");
Container contentPane=frame.getContentPane();
GridBagLayout gridbag=new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();
contentPane.setLayout(gridbag);
c.fill=GridBagConstraints.HORIZONTAL;
JButton button;

button=new JButton("Button 1");
c.weightx=0.5;
c.gridx=0;
c.gridy=0;
gridbag.setConstraints(button,c);
contentPane.add(button);

button=new JButton("Button 2");
c.gridx=1;
c.gridy=0;
gridbag.setConstraints(button,c);
contentPane.add(button);

button=new JButton("Button 3");
c.gridx=2;
c.gridy=0;
gridbag.setConstraints(button,c);
contentPane.add(button);

button=new JButton("Button 4");
c.ipady=30;
c.gridwidth=3;
c.gridx=0;
c.gridy=1;
gridbag.setConstraints(button,c);
contentPane.add(button);

button=new JButton("Button 5");
c.ipady=0;
c.weighty=1;
c.anchor=GridBagConstraints.SOUTH;
c.insets=new Insets(10,0,0,0);

c.gridx=1;
c.gridwidth=1;
c.gridy=2;
gridbag.setConstraints(button,c);
contentPane.add(button);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
}
}

0 komentar:

Posting Komentar