Swing JWindow类

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

类声明

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

public class JWindow
   extends Window
      implements Accessible, RootPaneContainer

字段

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

  • protected AccessibleContext accessibleContext - 可访问的上下文属性。
  • protected JRootPane rootPane - 管理此框架的contentPane和可选menuBar的JRootPane实例。
  • protected boolean rootPaneCheckingEnabled - 如果为true,则对addsetLayout的调用将转发到contentPane

类构造函数

编号 类构造函数 描述
1 JWindow() 创建一个没有指定所有者的窗口。
2 JWindow(Frame owner) 创建具有指定所有者框架的窗口。
3 JWindow(GraphicsConfiguration gc) 使用屏幕设备的指定GraphicsConfiguration创建一个窗口。
4 JWindow(Window owner) 使用指定的所有者窗口创建一个窗口。
5 JWindow(Window owner, GraphicsConfiguration gc) 使用指定的所有者窗口和屏幕设备的GraphicsConfiguration创建一个窗口。

类方法

编号 类方法 描述
1 protected void addImpl(Component comp, Object constraints, int index) 添加指定的子组件。
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() 返回对addsetLayout的调用是否转发到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) 设置是否将对addsetLayout的调用转发到contentPane
20 void setTransferHandler(TransferHandler newHandler) 设置transferHandler属性,该属性是一种支持将数据传输到此组件的机制。
21 void update(Graphics g) 调用:paint(g)
22 protected void windowInit() 由构造函数调用以正确初始化JWindow

方法继承

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

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

JWindow示例

使用编辑器创建以下Java程序:JWindowDemo.java

package com.zaixian.menu;

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

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

    public JWindowDemo() {
        prepareGUI();
    }

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

    private void prepareGUI() {
        mainFrame = new JFrame("Java Swing JWindowDemo(xuhuhu.com)");
        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("欢迎您来到许虎虎~", 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, "欢迎您来到许虎虎/Swing~");

        JButton okButton = new JButton("打开一个窗口");
        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);
        }
    }
}

执行上面示例代码,得到以下结果:

JWindow示例

打开新的窗口如下:

JWindow


上一篇: Swing容器类 下一篇:无