没分提问题,不一般吧!既然进来了看看吧
两个JPanel用同一个布局对象
import java.awt.*;
import javax.swing.*;
class Win extends JFrame
{
GridBagLayout gb1=new GridBagLayout();                 //这个对象
GridBagConstraints gbc=new GridBagConstraints();       //和这个对象被重复使用了
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JLabel lblName=new JLabel("姓名: ");
JLabel lblAge=new JLabel("年龄: ");
Win()
{
Container c=getContentPane();
c.setLayout(new BorderLayout());
p1.setLayout(gb1);                    //这里
gbc.gridx=0;                          //这里以下
gbc.gridy=0;
gbc.gridwidth=1;
gbc.gridheight=1;
gb1.setConstraints(lblName,gbc);
p1.add(lblName);
c.add(p1,BorderLayout.NORTH);
setSize(300,200);
p2.setLayout(gb1);                   //这里
gbc.gridx=0;                         //这里以下
gbc.gridy=0;
gbc.gridwidth=1;
gbc.gridheight=1;
gb1.setConstraints(lblAge,gbc);
p2.add(lblAge);
c.add(p2,BorderLayout.SOUTH);
p1.setBackground(Color.red);
p2.setBackground(Color.green);
show(); }
public static void main(String args[])
{
new Win();
}
}他们使用了同一个布局,会有什么影响呢?为什么不会有影响呢?