我试图从JMenuBar中最大化JFrame,我无法传递对帧的引用.是否可以获得对其使用的框架的引用?
我可以进入顶级组件,但它没有办法最大化和最小化框架.
public Container getApplicationFrame(ActionEvent event){
JMenuItem menuItem = (JMenuItem) event.getSource();
JPopupMenu popupMenu = (JPopupMenu) menuItem.getParent();
Component invoker = popupMenu.getInvoker();
JComponent invokerAsJComponent = (JComponent) invoker;
Container topLevel = invokerAsJComponent.getTopLevelAncestor();
return topLevel;
}
Run Code Online (Sandbox Code Playgroud)
您可以获取包含JPanel的Window
Window window = SwingUtilities.getWindowAncestor(popupMenu);
Run Code Online (Sandbox Code Playgroud)
然后你可以使用它来最大化它window.setSize()- 或者,因为你似乎知道它是一个JFrame,把它投射到Frame并使用setExtendedStateKevin提到的方法.来自Java Developers'Almanac的示例代码:
// This method minimizes a frame; the iconified bit is not affected
public void maximize(Frame frame) {
int state = frame.getExtendedState();
// Set the maximized bits
state |= Frame.MAXIMIZED_BOTH;
// Maximize the frame
frame.setExtendedState(state);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1780 次 |
| 最近记录: |