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,則對add和setLayout的調用將轉發到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() |
返回對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) |
調用:paint(g) |
| 22 | protected void windowInit() |
由構造函數調用以正確初始化JWindow。 |
方法繼承
該類繼承以下類中的方法 -
java.awt.Windowjava.awt.Containerjava.awt.Componentjava.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);
}
}
}
執行上面示例代碼,得到以下結果:

打開新的窗口如下:

上一篇:
Swing容器類
下一篇:無
