位置:首頁 > Java技術 > Swing > Swing JWindow類及實例

Swing JWindow類及實例

JWindow類是一個容器,可以顯示,但不具有標題欄或窗口管理按鈕。

類聲明

以下是聲明 javax.swing.JWindow類:

public class JWindow
   extends Window
      implements Accessible, RootPaneContainer

字段域

以下是java.awt.Component 類的字段:

  • protected AccessibleContext accessibleContext -- 訪問上下文屬性。

  • protected JRootPane rootPane -- JRootPane實例管理的contentPane和可選的菜單欄,以及該幀的玻璃麵板。

  • protected boolean rootPaneCheckingEnabled -- 如果為真(true)則調用添加的setLayout將被轉發到contentPane。

類構造函數

S.N. 構造函數 & 描述
1 JWindow() 
Creates a window with no specified owner.
2 JWindow(Frame owner) 
Creates a window with the specified owner frame.
3 JWindow(GraphicsConfiguration gc)
Creates a window with the specified GraphicsConfiguration of a screen device.
4 JWindow(Window owner) 
Creates a window with the specified owner window.
5 JWindow(Window owner, GraphicsConfiguration gc) 
Creates a window with the specified owner window and GraphicsConfiguration of a screen device.

類方法

S.N. 方法 & 描述
1 protected void addImpl(Component comp, Object constraints, int index) 
Adds the specified child Component.
2 protected JRootPane createRootPane() 
Called by the constructor methods to create the default rootPane.
3 AccessibleContext getAccessibleContext() 
Gets the AccessibleContext associated with this JWindow.
4 Container getContentPane() 
Returns the Container which is the contentPane for this window.
5 Component getGlassPane() 
Returns the glassPane Component for this window.
6 Graphics getGraphics() 
Creates a graphics context for this component.
7 JLayeredPane getLayeredPane() 
Returns the layeredPane object for this window.
8 JRootPane getRootPane() 
Returns the rootPane object for this window.
9 TransferHandler getTransferHandler() 
Gets the transferHandler property.
10 protected boolean isRootPaneCheckingEnabled() 
Returns whether calls to add and setLayout are forwarded to the contentPane.
11 protected String paramString() 
Returns a string representation of this JWindow.
12 void remove(Component comp) 
Removes the specified component from the container.
13 void repaint(long time, int x, int y, int width, int height) 
Repaints the specified rectangle of this component within time milliseconds.
14 void setContentPane(Container contentPane) 
Sets the contentPane property for this window.
15 void setGlassPane(Component glassPane) 
Sets the glassPane property.
16 void setLayeredPane(JLayeredPane layeredPane) 
Sets the layeredPane property.
17 void setLayout(LayoutManager manager) 
Sets the LayoutManager.
18 protected void setRootPane(JRootPane root) 
Sets the new rootPane object for this window.
19 protected void setRootPaneCheckingEnabled(boolean enabled) 
Sets whether calls to add and setLayout are forwarded to the contentPane.
20 void setTransferHandler(TransferHandler newHandler) 
Sets the transferHandler property, which is a mechanism to support transfer of data into this component.
21 void update(Graphics g) 
Calls paint(g).
22 protected void windowInit() 
Called by the constructors to init the JWindow properly.

方法繼承

這個類從以下類繼承的方法:

  • java.awt.Window

  • java.awt.Container

  • java.awt.Component

  • java.lang.Object

JWindow 例子

選擇使用任何編輯器創建以下java程序在 D:/ > SWING > com > yiibai > gui >

SwingContainerDemo.java
package com.yiibai.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingContainerDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
   private JLabel msglabel;

   public SwingContainerDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      SwingContainerDemo  swingContainerDemo = new SwingContainerDemo();  
      swingContainerDemo.showJWindowDemo();
   }

   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing 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 JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    

      statusLabel.setSize(350,100);

      msglabel = new JLabel("Welcome to TutorialsPoint SWING Tutorial."
         , JLabel.CENTER);

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

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showJWindowDemo(){
      headerLabel.setText("Container in action: JWindow");   
      final MessageWindow window = 
         new MessageWindow(mainFrame, 
         "Welcome to TutorialsPoint SWING Tutorial.");

      JButton okButton = new JButton("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 JWindow{
      private String message; 

      public MessageWindow(JFrame parent, String 
         message) { 
         super(parent);               
         this.message = message; 
         setSize(300, 300);       
         setLocationRelativeTo(parent);         
      }

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

編譯程序,使用命令提示符。到 D:/ > SWING 然後輸出以下命令。

D:SWING>javac comyiibaiguiSwingContainerDemo.java

如果冇有錯誤出現,這意味著編譯成功。使用下麵的命令來運行程序。

D:SWING>java com.yiibai.gui.SwingContainerDemo

驗證下麵的輸出

SWING JWindow