JAVA问题,因为偶不懂

来源:百度知道 编辑:UC知道 时间:2024/06/13 22:36:46
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Panel;
import java.awt.TextField;
public class GridBagLayoutDemo1
{

public static void main(String args[])
{
new GridBagLayoutDemo1Frame();//①这句是什么意思呀?不明白
}
}
class GridBagLayoutDemo1Frame extends Frame//②怎么又出现了一个类啦
{
public GridBagLayoutDemo1Frame()
{
this.setLayout(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();
Button b = new Button("One");
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 1;
this.add(b, c);// button 1 added
c.gridy++;
b = new Button("Two");
this.add(b, c);

c.gridx = 2;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 2;
b = new Button("Three");
this.add(b, c);<

问题1:常见一个GridBagLayoutDemo1Frame类实例,注意该实例是没有引用的!!
问题2:在class GridBagLayoutDemo1 这个类中定义一个内部类GridBagLayoutDemo1Frame,也就是问题1实例化的类。这个类继承Frame类
至于内部类,你去baidu下就会清楚,它是在一个类中定义的类,只能被本类调用。在这个例子里内部类的构造方法做了所有的操作
public GridBagLayoutDemo1Frame()
{
this.setLayout(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();
Button b = new Button("One");
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 1;
this.add(b, c);// button 1 added
c.gridy++;
b = new Button("Two");
this.add(b, c);

c.gridx = 2;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 2;
b = new Button("Three");
this.add(b, c);

c.gridx = 0;
c.gridy = 2;
c.gridwidth = 4;
c.gridheight = 1;
this.add(new TextField(), c);

this.pack();
this.setVisible(true);
}

main方法实例化这个内部类的时候,也就是问题1。执行了上面所有的操作。
本类GridBagLayoutDemo1