Cấp bậc tác giả:

JAVA

Lớp SWING JWindow

Được viết bởi webmaster ngày 06/03/2014 lúc 02:54 PM
Lớp JWindow là container có thể hiển thị nhưng không có thanh tiêu đề hoặc các nút quản lý cửa sổ
  • 0
  • 7946

Lớp SWING JWindow

Khai báo lớp

Theo sau là khai báo lớp javax.swing.JWindow

public class JWindow
   extends Window
      implements Accessible, RootPaneContainer

Field

Sau đây là các field của lớp java.awt.Component 

protected AccessibleContext accessibleContext -- Thuộc tính ngữ cảnh truy cập.

protected JRootPane rootPane -- JRootPane yêu cầu quản lý các contentPane và thanh menu tùy chọn cho Frame, như glassPane.

protected boolean rootPaneCheckingEnabled -- Nếu true thì gọi add và setLayout sẽ được chuyển tiếp đến contentPane.

Lớp Constructors

STT Mô tả
1 JWindow() 
Tạo ra cửa sổ không sở hữu.
2 JWindow(Frame owner) 
Tạo ra cửa sổ với Frame sở hữu.
3 JWindow(GraphicsConfiguration gc)
Tạo ra cửa sổ với GraphicsConfiguration của thiết bị màn hình.
4 JWindow(Window owner) 
Tạo ra cửa sổ với cửa sổ sở hữu.
5 JWindow(Window owner, GraphicsConfiguration gc) 
Tạo ra cửa sổ với cửa sổ sở hữu và GraphicsConfiguration của thiết bị màn hình.

Các phương thức lớp

STT Mô tả
1 protected void addImpl(Component comp, Object constraints, int index) 
Thêm các thành phần con
2 protected JRootPane createRootPane() 
Được gọi bở các phương thức constructor để tạo ra các rootPane mặc định.
3 AccessibleContext getAccessibleContext() 
Gets AccessibleContext liên quan JWindow này.
4 Container getContentPane() 
Trả về container là contentPane cho cửa sổ này.
5 Component getGlassPane() 
Trả về thành phần glassPane cho cửa sổ này.
6 Graphics getGraphics() 
Tạo ra ngữ cảnh graphics cho thành phần này.
7 JLayeredPane getLayeredPane() 
Trả về đối tượng layeredPane cho cửa sổ này.
8 JRootPane getRootPane() 
Trả về đối tượng rootPane cho cửa sổ này.
9 TransferHandler getTransferHandler() 
Gets thuộc tính transferHandler.
10 protected boolean isRootPaneCheckingEnabled() 
Trả về nơi gọi add và setLayout được chuyển tiếp đến các contentPane.
11 protected String paramString() 
Trả về chuỗi đại diện của JWindow này.
12 void remove(Component comp) 
Loại bỏ các thành phần quy định từ container.
13 void repaint(long time, int x, int y, int width, int height) 
Vẽ lại hình chữ nhật của thành phần này trong khoảng mili giây.
14 void setContentPane(Container contentPane) 
Thiết lập thuộc tính contentPane cho cửa sổ này.
15 void setGlassPane(Component glassPane) 
Thiết lập thuộc tính glassPane.
16 void setLayeredPane(JLayeredPane layeredPane) 
Thiết lập thuộc tính layeredPane.
17 void setLayout(LayoutManager manager) 
Thiết lập LayoutManager.
18 protected void setRootPane(JRootPane root)
Thiết lập đối tượng rootPane mới cho cửa sổ này.
19 protected void setRootPaneCheckingEnabled(boolean enabled) 
Thiết lập nơi gọi add và setLayout được chuyển tiếp đến các contentPane.
20 void setTransferHandler(TransferHandler newHandler) 
Thiết lập thuộc tính transferHandler,  cơ chế để hỗ trợ chuyển dữ liệu vào thành phần này.
21 void update(Graphics g) 
Gọi paint(g).
22 protected void windowInit() 
Được gọi bởi các constructor để init thuộc tính JWindow.

Kế thừa các phương thức

Lớp này kế thừa các phương thức từ các lớp sau:

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

Ví dụ về Swing:

Tạo ra chương trình java sau:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswing;

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

/**
 *
 * @author Administrator
 */
public class JavaSwing {

    JFrame mainFrame;

    public JavaSwing() {
        //prepareGUI();
    }
    
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        JavaSwing js = new JavaSwing();
        js.prepareGUI();
    }
    
    void prepareGUI()
    {
        mainFrame = new JFrame("Vi du ve Swing Frame");
        mainFrame.setSize(400, 400);
        mainFrame.setLayout(new GridLayout(3, 1));//rows -> columns
        
        mainFrame.setVisible(true);
    }
}

swing.jpg

Ví dụ về JWindow:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswing;

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

/**
 *
 * @author Administrator
 */
public class JavaSwing {

    JFrame mainFrame;
    JLabel headerLabel;
    JLabel statusLabel;
    JLabel msglabel;
    JPanel controlPanel;

    public JavaSwing() {
        prepareGUI();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        JavaSwing js = new JavaSwing();
        js.showJWindowDemo();
    }

    void prepareGUI() {
        mainFrame = new JFrame("Vi du ve Swing Frame");
        mainFrame.setSize(400, 400);
        mainFrame.setLayout(new GridLayout(3, 1));//rows -> columns
        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);
        }
    }
}

swing_JWindow_01.jpg

Nguồn bài viết: DOTNET.VN

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML