照抄了书上一段代码,请问下awt的问题。
package graphic;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class HelloAwt extends Frame {
/**
*
*/
//private static final long serialVersionUID = 1L;
public static void main(String[] args)
{
new HelloAwt().run();
}
/**
* 运行程序
*/
private void run()
{
createButton();
configureFrame();
createButton();
createButton();
createButton();
createButton();
createButton();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
/**
* 创建主窗口
*
*/
private void configureFrame()
{
setTitle("Hello");
setLayout( new FlowLayout());
setSize(new Dimension(400,400));
setLocation(100,110);
}
/**
* 创建按钮
*/
private void createButton()
{
Button button = new Button("打开");
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
createDialog();
}
});
add(button,BorderLayout.NORTH);
//setVisible(true);
}
private void createDialog()
{
//第一个参数表示是该类,第二个名字,
final Dialog dialog = new Dialog(HelloAwt.this,"对话框",true);
dialog.setSize(new Dimension(267,117));
Toolkit toolkit = dialog.getToolkit();
Dimension screenSize = toolkit.getScreenSize();
// x代表对话框的左边的位置
int x = HelloAwt.this.getX()
+(HelloAwt.this.getWidth()-dialog.getWidth())/2;
// 如果其小于 0 ,则代表其超出了左边屏幕,
// 则将其设置成0
if(x < 0)
x = 0;
//其加上对话框的宽度为右边对话框的x位置,如果其大于屏幕宽度,则将其
//设置为屏幕狂度减去对话框宽度,也就让对话框的右边对其屏幕右边
if((x+dialog.getWidth())>screenSize.width)
{
x = screenSize.width - dialog.getWidth();
}
//获取dialog的上方的Y坐标
int y = HelloAwt.this.getY()
+(HelloAwt.this.getHeight()-dialog.getHeight())/2;
if(y < 0)
y = 0;
//如果其下方在屏幕之下,则将其移至贴着屏幕的地方
if((y + dialog.getHeight()) > screenSize.height)
{
y = screenSize.height - dialog.getHeight();
}
dialog.setLocation(x, y);
dialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
dialog.dispose();
}
});
dialog.setLayout(new GridLayout(2,1));
Panel topPanel = new Panel();
Label label = new Label("这是一个对话框");
topPanel.add(label,BorderLayout.NORTH);
dialog.add(topPanel);
Panel bottomPanel = new Panel();
Button button = new Button("按钮");
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dialog.dispose();
}
});
bottomPanel.add(button,BorderLayout.SOUTH);
dialog.add(bottomPanel);
dialog.setVisible(true);
}
}