java - 如何实现InternalFrameListener?

标签 java swing jinternalframe windowlistener

由于我能够为 JFrame 实现 WindowListener(下面的代码),所以我想为我的 JInternalFrame 执行相同的操作,我尝试了很多替代方案,但没有任何效果对我有用,有什么建议吗?

this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowOpened(WindowEvent MyWindowEvent) {
                method.relayout(true);
            }
        });

最佳答案

请参阅How to Write an Internal Frame Listener其中包括以下示例代码:

package events;

/*
 * InternalFrameEventDemo.java requires no other files.
 */

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

public class InternalFrameEventDemo
                     extends JFrame
                     implements InternalFrameListener,
                                ActionListener {
    JTextArea display;
    JDesktopPane desktop;
    JInternalFrame displayWindow;
    JInternalFrame listenedToWindow;
    static final String SHOW = "show";
    static final String CLEAR = "clear";
    String newline = "\n";
    static final int desktopWidth = 500;
    static final int desktopHeight = 300;

    public InternalFrameEventDemo(String title) {
        super(title);

        //Set up the GUI.
        desktop = new JDesktopPane();
        desktop.putClientProperty("JDesktopPane.dragMode",
                                  "outline");
        //Because we use pack, it's not enough to call setSize.
        //We must set the desktop's preferred size.
        desktop.setPreferredSize(new Dimension(desktopWidth, desktopHeight));
        setContentPane(desktop);

        createDisplayWindow();
        desktop.add(displayWindow); //DON'T FORGET THIS!!!
        Dimension displaySize = displayWindow.getSize();
        displayWindow.setSize(desktopWidth, displaySize.height);
    }

    //Create the window that displays event information.
    protected void createDisplayWindow() {
        JButton b1 = new JButton("Show internal frame");
        b1.setActionCommand(SHOW);
        b1.addActionListener(this);

        JButton b2 = new JButton("Clear event info");
        b2.setActionCommand(CLEAR);
        b2.addActionListener(this);

        display = new JTextArea(3, 30);
        display.setEditable(false);
        JScrollPane textScroller = new JScrollPane(display);
        //Have to supply a preferred size, or else the scroll
        //area will try to stay as large as the text area.
        textScroller.setPreferredSize(new Dimension(200, 75));
        textScroller.setMinimumSize(new Dimension(10, 10));

        displayWindow = new JInternalFrame("Event Watcher",
                                           true,  //resizable
                                           false, //not closable
                                           false, //not maximizable
                                           true); //iconifiable
        JPanel contentPane = new JPanel();
        contentPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        contentPane.setLayout(new BoxLayout(contentPane,
                                            BoxLayout.PAGE_AXIS));
        b1.setAlignmentX(CENTER_ALIGNMENT);
        contentPane.add(b1);
        contentPane.add(Box.createRigidArea(new Dimension(0, 5)));
        contentPane.add(textScroller);
        contentPane.add(Box.createRigidArea(new Dimension(0, 5)));
        b2.setAlignmentX(CENTER_ALIGNMENT);
        contentPane.add(b2);

        displayWindow.setContentPane(contentPane);
        displayWindow.pack();
        displayWindow.setVisible(true);
    }

    public void internalFrameClosing(InternalFrameEvent e) {
        displayMessage("Internal frame closing", e);
    }

    public void internalFrameClosed(InternalFrameEvent e) {
        displayMessage("Internal frame closed", e);
    }

    public void internalFrameOpened(InternalFrameEvent e) {
        displayMessage("Internal frame opened", e);
    }

    public void internalFrameIconified(InternalFrameEvent e) {
        displayMessage("Internal frame iconified", e);
    }

    public void internalFrameDeiconified(InternalFrameEvent e) {
        displayMessage("Internal frame deiconified", e);
    }

    public void internalFrameActivated(InternalFrameEvent e) {
        displayMessage("Internal frame activated", e);
    }

    public void internalFrameDeactivated(InternalFrameEvent e) {
        displayMessage("Internal frame deactivated", e);
    }

    //Add some text to the text area.
    void displayMessage(String prefix, InternalFrameEvent e) {
        String s = prefix + ": " + e.getSource();
        display.append(s + newline);
        display.setCaretPosition(display.getDocument().getLength());
    }

    //Handle events on the two buttons.
    public void actionPerformed(ActionEvent e) {
        if (SHOW.equals(e.getActionCommand())) {
            //They clicked the Show button.

            //Create the internal frame if necessary.
            if (listenedToWindow == null) {
                listenedToWindow = new JInternalFrame("Event Generator",
                                                      true,  //resizable
                                                      true,  //closable
                                                      true,  //maximizable
                                                      true); //iconifiable
                //We want to reuse the internal frame, so we need to
                //make it hide (instead of being disposed of, which is
                //the default) when the user closes it.
                listenedToWindow.setDefaultCloseOperation(
                  WindowConstants.HIDE_ON_CLOSE);

                //Add an internal frame listener so we can see
                //what internal frame events it generates.
                listenedToWindow.addInternalFrameListener(this);

                //And we mustn't forget to add it to the desktop pane!
                desktop.add(listenedToWindow);

                //Set its size and location.  We'd use pack() to set the size
                //if the window contained anything.
                listenedToWindow.setSize(300, 100);
                listenedToWindow.setLocation(
                    desktopWidth/2 - listenedToWindow.getWidth()/2,
                    desktopHeight - listenedToWindow.getHeight());
            }

            //Show the internal frame.
            listenedToWindow.setVisible(true);

        } else { //They clicked the Clear button.
            display.setText("");
        }
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        //Create and set up the window.
        JFrame frame = new InternalFrameEventDemo(
                "InternalFrameEventDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

关于java - 如何实现InternalFrameListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25183780/

相关文章:

java - 从键码中输入键值

java - 如何转换不互相继承的对象?

java - 在 Java 中从多个 JTextField 中单击鼠标清除文本

java - 为什么将鼠标移到 Java 应用程序上会更流畅?包括视频

java - 是否可以删除 JInternalFrame 中的小下拉箭头?

java - 需要高性能的文本文件读取和解析(类似 split())

java - Android:- 将录制的音频文件转换为 float 组

java - 在 JFrame 中显示多个网页

java - 如何刷新JInternalFrame或JTable

java - 在最小化和取消最小化之前无法调整内部框架的大小