GUI小程序【GridBagLayout】

自己做练习编写的小程序: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);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值