自己做练习编写的小程序:GridBagLayout布局使用
下面是自己写的代码
package layout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShowGridBagLayout extends JApplet{
private JLabel jbl1=new JLabel("resize the window and study gridbaglayout",JLabel.CENTER);
private JTextArea jta1=new JTextArea("jtextarea1",5,15);
private JTextArea jta2=new JTextArea("jtextarea2",5,15);
private JTextField jtf=new JTextField("JTextField");
private JPanel jp=new JPanel();
private JButton jbt1=new JButton("jbutton1");
private JButton jbt2=new JButton("jbutton2");
private JFrame jf=new JFrame("GridBagLayout");
public ShowGridBagLayout(){
Container c=jf.getContentPane();
c.setLayout(new GridBagLayout());
GridBagConstraints gbConstraints=new GridBagConstraints();
gbConstraints.fill=GridBagConstraints.BOTH;
gbConstraints.anchor=GridBagConstraints.CENTER;
addComp(jta1,c,gbConstraints,0,0,1,4,0,0);
addComp(jta2,c,gbConstraints,1,0,2,1,5,1);
addComp(jtf,c,gbConstraints,1,3,1,1,5,1);
addComp(jbt1,c,gbConstraints,3,1,1,1,5,0);
addComp(jbt2,c,gbConstraints,3,2,1,1,5,0);
jp.setBackground(Color.blue);
jp.setBorder(new javax.swing.border.LineBorder(Color.black));
gbConstraints.insets=new Insets(10,10,10,10);
addComp(jp,c,gbConstraints,1,1,2,2,10,1);
jf.setSize(500,500);
jf.setLocation(200,200);
jf.setVisible(true);
}
public void addComp(Component c,Container container,
GridBagConstraints gbConstraints,
int row,int column,
int numberOfRows,int numberOfColumn,
double weightx,double weighty){
gbConstraints.gridx=column;
gbConstraints.gridy=row;
gbConstraints.gridwidth=numberOfColumn;
gbConstraints.gridheight=numberOfRows;
gbConstraints.weightx=weightx;
gbConstraints.weighty=weighty;
container.add(c,gbConstraints);
}
}
下面是自己写的代码
package layout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShowGridBagLayout extends JApplet{
private JLabel jbl1=new JLabel("resize the window and study gridbaglayout",JLabel.CENTER);
private JTextArea jta1=new JTextArea("jtextarea1",5,15);
private JTextArea jta2=new JTextArea("jtextarea2",5,15);
private JTextField jtf=new JTextField("JTextField");
private JPanel jp=new JPanel();
private JButton jbt1=new JButton("jbutton1");
private JButton jbt2=new JButton("jbutton2");
private JFrame jf=new JFrame("GridBagLayout");
public ShowGridBagLayout(){
Container c=jf.getContentPane();
c.setLayout(new GridBagLayout());
GridBagConstraints gbConstraints=new GridBagConstraints();
gbConstraints.fill=GridBagConstraints.BOTH;
gbConstraints.anchor=GridBagConstraints.CENTER;
addComp(jta1,c,gbConstraints,0,0,1,4,0,0);
addComp(jta2,c,gbConstraints,1,0,2,1,5,1);
addComp(jtf,c,gbConstraints,1,3,1,1,5,1);
addComp(jbt1,c,gbConstraints,3,1,1,1,5,0);
addComp(jbt2,c,gbConstraints,3,2,1,1,5,0);
jp.setBackground(Color.blue);
jp.setBorder(new javax.swing.border.LineBorder(Color.black));
gbConstraints.insets=new Insets(10,10,10,10);
addComp(jp,c,gbConstraints,1,1,2,2,10,1);
jf.setSize(500,500);
jf.setLocation(200,200);
jf.setVisible(true);
}
public void addComp(Component c,Container container,
GridBagConstraints gbConstraints,
int row,int column,
int numberOfRows,int numberOfColumn,
double weightx,double weighty){
gbConstraints.gridx=column;
gbConstraints.gridy=row;
gbConstraints.gridwidth=numberOfColumn;
gbConstraints.gridheight=numberOfRows;
gbConstraints.weightx=weightx;
gbConstraints.weighty=weighty;
container.add(c,gbConstraints);
}
}