JDesktopPane不会隐藏它的框架

时间:2017-04-23 03:55:06

标签: java swing jdesktoppane

我想隐藏我的JDesktopPane按钮触发器。但这不会隐藏起来。我创建了一个静态void方法,隐藏框架并在 actionPerformed 中调用它。我是否需要运行另一个EDT来隐藏我的框架?任何提示都会受到赞赏。

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

public class JDesktoppane extends JFrame
{
JDesktopPane desktop;
JMenu menu;

public JDesktoppane()
{
   super("TITLE");
   int inset = 80;
   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

   setBounds(inset, inset,
           screenSize.width - inset*2,
           screenSize.height - inset*2);

    //Set up the GUI
   desktop = new JDesktopPane();
   //Setting JDesktopPane as Container
   setContentPane(desktop);
   setJMenuBar(createMenuBar());
   //Make dragging a little faster but perhaps uglier.
   desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
}

public JMenuBar createMenuBar()
{
    JMenuBar bar = new JMenuBar();

    menu = new JMenu("CLICK ME");

    JMenuItem item = new JMenuItem("CLICK ME AGAIN");
    item.addActionListener(listener);
    menu.add(item);

    bar.add(menu);


    return bar;
}

ActionListener listener = new ActionListener() 
{
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        createFrame();
    }
};

public void createFrame()
{
    InternalFrame frame = new InternalFrame();
    frame.setVisible(true);
    desktop.add(frame);
}

public static void createAndShowGUI()
{
    JDesktoppane p = new JDesktoppane();
    p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    p.setVisible(true);
}

public static void hideGUI()
{
    JDesktoppane p = new JDesktoppane();
    p.setVisible(false);
}

public static void main(String[] args) 
{
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            createAndShowGUI();
        }
    });
}

}

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

public class InternalFrame extends JInternalFrame
{
public InternalFrame()
{
    addComponentsToPane(getContentPane());
    pack();
}

ActionListener listener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        JDesktoppane.hideGUI();
    }
};

private void addComponentsToPane(final Container pane)
{
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    JButton btn = new JButton("HIDE ME");
    btn.addActionListener(listener);
    panel.add(btn);
    pane.add(panel);
}
}

1 个答案:

答案 0 :(得分:1)

hideGUI中,您正在创建一个新窗口,然后隐藏它:

public static void hideGUI()
{
    // creating a new window
    JDesktoppane p = new JDesktoppane();
    p.setVisible(false);
}

我认为您的意思是以某种方式为InternalFrame提供您在createAndShowGUI中创建的原始窗口的引用。

你可以这样做:

public class InternalFrame extends JInternalFrame
{
    JDesktoppane parent;

    public InternalFrame(JDesktoppane parent)
    {
        this.parent = parent;

        addComponentsToPane(getContentPane());
        pack();
    }

    ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            // note
            parent.setVisible(false);
        }
    };
    ...
}

public void createFrame()
{
    //                                      vvvv
    InternalFrame frame = new InternalFrame(this);
    frame.setVisible(true);
    desktop.add(frame);
}

虽然有点奇怪,因为内部窗格有一个隐藏其父级的按钮。一个更惯用的方法是拥有一个JMenuBar或类似的东西,并附带一个选项。 (你有计划让窗户回来吗?通常如果你想隐藏一个窗口,你只需要最小化它。)