gpt4 book ai didi

java - 当我最大化 JInternalFrame 时,JInternalFrame 上的示例程序 JMenubar

转载 作者:搜寻专家 更新时间:2023-11-01 02:13:00 26 4
gpt4 key购买 nike

您好,我需要一个示例程序,其中当我最大化 JInternalFrame 时,JFrame 的 JMenuBar 应该设置在 JInternalFrame 上当我再次最小化 JInternalFrame 时,JMenuBar 应该离开 JinternalFrame 并设置到 JFrame 如下所示 When the JInternalFrame is Maximized

When JInternalFrame is Minimized

请提供一个 Java Swing 示例程序

最佳答案

似乎工作正常,请发帖 SSCCE显示具体问题:

enter image description here

import java.awt.*;
import java.awt.event.*;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

public class JInternalFrameDemo {

JDesktopPane jdpDesktop;
static int openFrameCount = 0;

public JInternalFrameDemo() {
JFrame frame = new JFrame("JInternalFrame Usage Demo");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// A specialized layered pane to be used with JInternalFrames
jdpDesktop = new JDesktopPane() {
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
};


createFrame(); // Create first window

frame.setContentPane(jdpDesktop);

frame.setJMenuBar(createMenuBar());

// Make dragging faster by setting drag mode to Outline
jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");

frame.pack();
frame.setVisible(true);
}

protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Frame");
menu.setMnemonic(KeyEvent.VK_N);
JMenuItem menuItem = new JMenuItem("New IFrame");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createFrame();
}
});
menu.add(menuItem);
menuBar.add(menu);
return menuBar;
}

protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true);
// Every JInternalFrame must be added to content pane using JDesktopPane
jdpDesktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}

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

class MyInternalFrame extends JInternalFrame {

static final int xPosition = 30, yPosition = 30;

public MyInternalFrame() {
super("IFrame #" + (++openFrameCount), true, // resizable
true, // closable
true, // maximizable
true);// iconifiable
setSize(300, 300);
// Set the window's location.
setLocation(xPosition * openFrameCount, yPosition
* openFrameCount);
}
}
}

关于java - 当我最大化 JInternalFrame 时,JInternalFrame 上的示例程序 JMenubar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13651991/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com