0
我想躲我JDesktopPane
按鈕的觸發。但是這不會隱藏自己。我創建了一個靜態void方法,隱藏框架並在actionPerformed中調用此方法。我是否需要運行另一個EDT來隱藏我的框架?任何提示將不勝感激。的JDesktopPane不會隱藏它的框架
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);
}
}
謝謝!它實際上工作。當我隱藏窗口時,我感到困惑。我只需將按鈕更改爲JMenu。 :) – Francisunoxx