关于InternalFrame的问题

bibleboy 2004-06-16 08:30:29
想在点击一个InternalFrame中的JButton,打开一个和这个InternalFrame同级别的InternalFrame,如何实现那?
(不是InternalFrame的子InternalFrame,是同在一个Frame下的InternalFrame)
...全文
430 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
eboyzhang 2004-06-17
  • 打赏
  • 举报
回复
JInternalFrame实例必须被add到一个JDesktopPane实例中去,于是你可以通过JInternalFrame的getParent方法获得该JDesktopPane(当然返回值是一个Container,你得强制转换)。

既然得到了parent,就可以使用parent添加新的JInternalFrame了。比如你自己定义了一个createInternalFrame()什么的,使用它就OK了~
congliu 2004-06-17
  • 打赏
  • 举报
回复
JInternalFrame实例必须被add到一个JDesktopPane(即是上面程序的desktop)实例中去。然后在子窗口的按纽响应函数设为“this.getParent().add”即可
bibleboy 2004-06-17
  • 打赏
  • 举报
回复
多谢大家:)
huangsuipeng 2004-06-16
  • 打赏
  • 举报
回复
fuzuyuan(happyboy) 的, congliu() 能讲解一下嘛?大家不想知其然,不知其所以然
:)
帮顶
congliu 2004-06-16
  • 打赏
  • 举报
回复
运行InternalFrameDemo类
congliu 2004-06-16
  • 打赏
  • 举报
回复
InternalFrameDemo.java文件:

import javax.swing.JInternalFrame;
import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

import java.awt.event.*;
import java.awt.*;

/*
* InternalFrameDemo.java is a 1.4 application that requires:
* MyInternalFrame.java
*/
public class InternalFrameDemo extends JFrame
implements ActionListener {
JDesktopPane desktop;

public InternalFrameDemo() {
super("InternalFrameDemo");

//Make the big window be indented 50 pixels from each edge
//of the screen.
int inset = 50;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(inset, inset,
screenSize.width - inset*2,
screenSize.height - inset*2);

//Set up the GUI.
desktop = new JDesktopPane(); //a specialized layered pane
createFrame(); //create first "window"
setContentPane(desktop);
setJMenuBar(createMenuBar());

//Make dragging a little faster but perhaps uglier.
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
}

protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();

//Set up the lone menu.
JMenu menu = new JMenu("Document");
menu.setMnemonic(KeyEvent.VK_D);
menuBar.add(menu);

//Set up the first menu item.
JMenuItem menuItem = new JMenuItem("New");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_N, ActionEvent.ALT_MASK));
menuItem.setActionCommand("new");
menuItem.addActionListener(this);
menu.add(menuItem);

//Set up the second menu item.
menuItem = new JMenuItem("Quit");
menuItem.setMnemonic(KeyEvent.VK_Q);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_Q, ActionEvent.ALT_MASK));
menuItem.setActionCommand("quit");
menuItem.addActionListener(this);
menu.add(menuItem);

return menuBar;
}

//React to menu selections.
public void actionPerformed(ActionEvent e) {
if ("new".equals(e.getActionCommand())) { //new
createFrame();
} else { //quit
quit();
}
}

//Create a new internal frame.
protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true); //necessary as of 1.3
desktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}
}

//Quit the application.
protected void quit() {
System.exit(0);
}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.
InternalFrameDemo frame = new InternalFrameDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Display the window.
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
congliu 2004-06-16
  • 打赏
  • 举报
回复
MyInternalFrame.java文件:


import java.awt.event.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import javax.swing.*;
import java.beans.*;

/* Used by InternalFrameDemo.java. */
public class MyInternalFrame
extends JInternalFrame {
static int openFrameCount = 0;
static final int xOffset = 30, yOffset = 30;

public MyInternalFrame() {
super("Document #" + (++openFrameCount),
true, //resizable
true, //closable
true, //maximizable
true); //iconifiable

//...Create the GUI and put it in the window...

//...Then set the window size or call pack...
setSize(300, 300);

//Set the window's location.
setLocation(xOffset * openFrameCount, yOffset * openFrameCount);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
jButton1.setText("单击此处新增子窗口");
jButton1.addActionListener(new MyInternalFrame_jButton1_actionAdapter(this));
this.getContentPane().setLayout(xYLayout1);
this.getContentPane().add(jButton1, new XYConstraints(107, 96, -1, -1));
}

XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();

void jButton1_actionPerformed(ActionEvent e) {
System.out.println("adfas");
MyInternalFrame a = new MyInternalFrame();
this.getParent().add(a);
a.setVisible(true);
try {
a.setSelected(true);
}
catch (PropertyVetoException ex) {
}
// JOptionPane.showMessageDialog(null,"dfd");

}
}

class MyInternalFrame_jButton1_actionAdapter
implements java.awt.event.ActionListener {
MyInternalFrame adaptee;

MyInternalFrame_jButton1_actionAdapter(MyInternalFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
shuneng 2004-06-16
  • 打赏
  • 举报
回复
mark
fuzuyuan 2004-06-16
  • 打赏
  • 举报
回复
下面只做了个大概的程序,进一步的完善靠你自己了,运行的时候,先产生的是父窗体,里面有一个按钮

,点击他可以产生子窗体,生成的子窗体中也有一个按钮,点击这个按钮的时候,也将产生一个窗体,但

这个窗体是属于父窗体的,运行的时候可能你点击按钮后看不到效果,将窗体最大化在还原即可。(也就

是说刷新的工作没有完成)
父窗体文件:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class FrameTest extends JFrame implements ActionListener{
private JButton bn=new JButton("this is a test");
public FrameTest(){
super("this is a test");
this.setSize(500,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con=this.getContentPane();
FlowLayout fl=new FlowLayout();
con.setLayout(fl);
bn.addActionListener(this);
con.add(bn);
this.setContentPane(con);
this.setVisible(true);

}
public void actionPerformed(ActionEvent e){
InFrame subfrm=new InFrame();
this.getContentPane().add(subfrm);
}
public static void main(String[] args){
new FrameTest();
}
}
子窗体文件:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class InFrame extends JInternalFrame implements ActionListener{
UIManager.LookAndFeelInfo looks[];
private JButton bn=new JButton("this is a test");
public InFrame(){
super("InFrame");
looks=UIManager.getInstalledLookAndFeels();
try
{
UIManager.setLookAndFeel(looks[1].getClassName());
SwingUtilities.updateComponentTreeUI(this);
}
catch (Exception e)
{
e.printStackTrace();
}
this.setSize(500,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con=this.getContentPane();
FlowLayout fl=new FlowLayout();
con.setLayout(fl);
setLocation(200,150);
bn.addActionListener(this);
con.add(bn);
this.setContentPane(con);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
InFrame subfrm=new InFrame();
this.getParent().add(subfrm);
}
}
bibleboy 2004-06-16
  • 打赏
  • 举报
回复
最好有个样例代码,分不够可以再加!

62,623

社区成员

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

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