AWT Window类

1 什么是Java AWT Window

Window类是一个没有边框和菜单栏的顶级窗口。它使用 BorderLayout 作为默认布局管理器。

2 Java AWT Window的语法

public class Window
   extends Container
      implements Accessible

3 Java AWT Window的构造方法

构造方法 描述
Window(Frame owner) 构造一个新的、最初不可见的窗口,并将指定的 Frame 作为其所有者。
Window(Window owner) 以指定的 Window 作为其所有者构造一个新的、最初不可见的窗口。
Window(Window owner, GraphicsConfiguration gc) 使用指定的所有者 Window 和屏幕设备的 GraphicsConfiguration 构造一个新的、最初不可见的窗口。

4 Java AWT Window的例子

让我们看一个简单的Java AWT Window类示例。

package com.yiidian;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class AwtContainerDemo {
   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;
   private Label msglabel;

   public AwtContainerDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtContainerDemo  awtContainerDemo = new AwtContainerDemo();          
      awtContainerDemo.showWindowDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("一点教程网:Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);
   
      msglabel = new Label();
      msglabel.setAlignment(Label.CENTER);
      msglabel.setText("Welcome to yiidian.com AWT Tutorial.");

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   
   private void showWindowDemo(){
      headerLabel.setText("Container in action: Window");   
      final MessageWindow window = 
         new MessageWindow(mainFrame,
         "Welcome to yiidian.com AWT Tutorial.");

      Button okButton = new Button("Open a Window");
      okButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            window.setVisible(true);
            statusLabel.setText("A Window shown to the user.");                
         }
      });
      controlPanel.add(okButton);
      mainFrame.setVisible(true);  
   }

   class MessageWindow extends Window{
      private String message; 

      public MessageWindow(Frame parent, String message) { 
         super(parent);               
         this.message = message; 
         setSize(300, 300);       
         setLocationRelativeTo(parent);
         setBackground(Color.gray);
      }

      public void paint(Graphics g) { 
         super.paint(g);
         g.drawRect(0,0,getSize().width - 1,getSize().height - 1); 
         g.drawString(message,50,150); 
      } 
   }
}

输出结果为:

热门文章

优秀文章