java中怎样做出像vb里的mdi窗体???

CTaoHero 2003-08-30 09:53:10
如题!
...全文
36 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jhlqab 2003-08-30
  • 打赏
  • 举报
回复
你看看JInternalFrame(子窗体)和JDesktopPane(父窗体)
CTaoHero 2003-08-30
  • 打赏
  • 举报
回复
jbuilder9下怎样做呢?
yshen 2003-08-30
  • 打赏
  • 举报
回复
给你一个例子,有三个.java文件JInternalFrameDemo.java、MainFrame.java、OwnInternalFrame.java


package jinternalframedemo;

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

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/

public class JInternalFrameDemo {
private boolean packFrame = false;

//Construct the application
public JInternalFrameDemo() {
MainFrame frame = new MainFrame();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
/* try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
*/ new JInternalFrameDemo();
}
}






package jinternalframedemo;

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

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/

public class MainFrame extends JFrame {
// private JPanel contentPane;
private JDesktopPane desktop = new JDesktopPane();
// private BorderLayout borderLayout1 = new BorderLayout();
private JMenuBar jMenuBar1 = new JMenuBar();
private JMenu jMenu1 = new JMenu();
private JMenuItem jMenuItem1 = new JMenuItem();

//Construct the frame
public MainFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(MainFrame.class.getResource("[Your Icon]")));
// contentPane = (JPanel) this.getContentPane();
// contentPane.setLayout(borderLayout1);
desktop.putClientProperty("JDesktopPane.dragMode", "outline");
this.setContentPane(desktop);
this.setJMenuBar(jMenuBar1);
this.setSize(new Dimension(400, 300));
this.setTitle("JInternalFrameDemo");
jMenu1.setFont(new java.awt.Font("Dialog", 0, 12));
jMenu1.setText("Option");
jMenuItem1.setFont(new java.awt.Font("Dialog", 0, 12));
jMenuItem1.setText("Add Frame");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuItem1_actionPerformed(e);
}
});
jMenuBar1.add(jMenu1);
jMenu1.add(jMenuItem1);

}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

void jMenuItem1_actionPerformed(ActionEvent e) {
OwnInternalFrame frame = new OwnInternalFrame();
frame.setVisible(true); //necessary as of kestrel
desktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException err) {
err.printStackTrace();
}
}
}


package jinternalframedemo;

import javax.swing.JInternalFrame;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/

public class OwnInternalFrame extends JInternalFrame {

static int frameNo = 0;
static final int xOffset = 20, yOffset = 20;

public OwnInternalFrame() {
super("InternalFrame " + (++frameNo), true, true, true, true);
setSize(150,150);
setLocation(xOffset*frameNo, yOffset*frameNo);
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧