当前位置: 技术问答>java相关
执行结果怎么跟我想的不一样?
来源: 互联网 发布时间:2015-03-17
本文导语: import java.awt.*; import java.awt.event.*; class Qq extends Frame { public Qq() { addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); } public static void main(String args[]) {...
import java.awt.*;
import java.awt.event.*;
class Qq extends Frame
{
public Qq()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
}
public static void main(String args[])
{
System.out.println("Starting Qq...");
Qq mainFrame = new Qq();
mainFrame.setSize(400, 400);
mainFrame.setTitle("qqq");
mainFrame.add (new Button("button"));
mainFrame.add (new Label("label"));
mainFrame.setVisible(true);
}
}
结果只有label.无button.??
import java.awt.event.*;
class Qq extends Frame
{
public Qq()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
}
public static void main(String args[])
{
System.out.println("Starting Qq...");
Qq mainFrame = new Qq();
mainFrame.setSize(400, 400);
mainFrame.setTitle("qqq");
mainFrame.add (new Button("button"));
mainFrame.add (new Label("label"));
mainFrame.setVisible(true);
}
}
结果只有label.无button.??
|
看一下布局管理器方面的知识先。
|
The default layout for a frame is BorderLayout.
所以mainFrame.add (new Button("button"));
mainFrame.add (new Label("label"));
BorderLayout默认为BorderLayout.CENTER。所以label覆盖了button。
改为mainFrame.add (new Button("button"),BorderLayout.SOUTH);
mainFrame.add (new Label("label"));
就可以看到button!!!
所以mainFrame.add (new Button("button"));
mainFrame.add (new Label("label"));
BorderLayout默认为BorderLayout.CENTER。所以label覆盖了button。
改为mainFrame.add (new Button("button"),BorderLayout.SOUTH);
mainFrame.add (new Label("label"));
就可以看到button!!!