e738. getting the jframe of a component.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 17 行

TXT
17 行
字号
In order to find the frame that contains a component, it is necessary to walk up the component's parents until the frame is encountered. SwingUtilities.getRoot() is a convenience method that finds the frame. 
This example implements an action that finds and hides the frame of the component that triggered the action. See also e743 Creating a JButton Component. 

    // Create an action
    Action action = new AbstractAction("Action Label") {
        // This method is called when the action is triggered
        public void actionPerformed(ActionEvent evt) {
            Component c = (Component)evt.getSource();
    
            // Get the frame
            Component frame = SwingUtilities.getRoot(c);
    
            // Hide the frame
            frame.setVisible(false);
        }
    };

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?