目录

Window

介绍 (Introduction)

JWindow类是一个可以显示但没有标题栏或窗口管理按钮的容器。

Class 声明 (Class Declaration)

以下是javax.swing.JWindow类的声明 -

public class JWindow
   extends Window
      implements Accessible, RootPaneContainer

字段 (Field)

以下是java.awt.Component类的字段 -

  • protected AccessibleContext accessibleContext - 可访问的上下文属性。

  • protected JRootPane rootPane - 管理此框架的contentPane和可选menuBar的JRootPane实例,以及glassPane。

  • protected boolean rootPaneCheckingEnabled - 如果为true,则对add和setLayout的调用将被转发到contentPane。

类构造函数 (Class Constructors)

Sr.No. 构造函数和描述
1

JWindow()

创建一个没有指定所有者的窗口。

2

JWindow(Frame owner)

创建具有指定所有者框架的窗口。

3

JWindow(GraphicsConfiguration gc)

使用屏幕设备的指定GraphicsConfiguration创建一个窗口。

4

JWindow(Window owner)

使用指定的所有者窗口创建一个窗口。

5

JWindow(Window owner, GraphicsConfiguration gc)

使用指定的所有者窗口和屏幕设备的GraphicsConfiguration创建一个窗口。

Class Methods

Sr.No. 方法和描述
1

protected void addImpl(Component comp, Object constraints, int index)

添加指定的子Component。

2

protected JRootPane createRootPane()

由构造函数方法调用以创建默认的rootPane。

3

AccessibleContext getAccessibleContext()

获取与此JWindow关联的AccessibleContext。

4

Container getContentPane()

返回Container,它是此窗口的contentPane。

5

Component getGlassPane()

返回此窗口的glassPane组件。

6

Graphics getGraphics()

为此组件创建图形上下文。

7

JLayeredPane getLayeredPane()

返回此窗口的layeredPane对象。

8

JRootPane getRootPane()

返回此窗口的rootPane对象。

9

TransferHandler getTransferHandler()

获取transferHandler属性。

10

protected boolean isRootPaneCheckingEnabled()

返回对add和setLayout的调用是否转发到contentPane。

11

protected String paramString()

返回此JWindow的字符串表示形式。

12

void remove(Component comp)

从容器中删除指定的组件。

13

void repaint(long time, int x, int y, int width, int height)

在时间毫秒内重新绘制此组件的指定矩形。

14

void setContentPane(Container contentPane)

设置此窗口的contentPane属性。

15

void setGlassPane(Component glassPane)

设置glassPane属性。

16

void setLayeredPane(JLayeredPane layeredPane)

设置layeredPane属性。

17

void setLayout(LayoutManager manager)

设置LayoutManager。

18

protected void setRootPane(JRootPane root)

为此窗口设置新的rootPane对象。

19

protected void setRootPaneCheckingEnabled(boolean enabled)

设置是否将对add和setLayout的调用转发到contentPane。

20

void setTransferHandler(TransferHandler newHandler)

设置transferHandler属性,该属性是一种支持将数据传输到此组件的机制。

21

void update(Graphics g)

调用画(g)。

22

protected void windowInit()

由构造函数调用以正确初始化JWindow。

方法继承 (Methods Inherited)

该类继承以下类中的方法 -

  • java.awt.Window
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

JWindow示例

使用您选择的任何编辑器创建以下Java程序,例如D:/ 》 SWING 》 com 》 iowiki 》 gui 》

SwingContainerDemo.java

package com.iowiki.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 IoWiki 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 IoWiki 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 com\iowiki\gui\SwingContainerDemo.java

如果没有错误发生,则表示编译成功。 使用以下命令运行该程序。

D:\SWING>java com.iowiki.gui.SwingContainerDemo

验证以下输出。

SWING JWindow
↑回到顶部↑
WIKI教程 @2018