急求 关于JInternalFrame之间的操作问题!

xlking 2004-11-21 03:58:59
我在创建多个子窗体的同时,想分别对每个子窗体进行操作,应该怎么实现?

我写了个简单代码,想对多个子窗体中的当前窗体中的表进行加行操作,但总是只能加到最新的哪个窗体中,望高手指点一下。

代码如下

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


class MainFrame extends JFrame
{
JDesktopPane desktop;
JToolBar toolbar;
MyinnFrame doc;

public MainFrame()
{
toolbar=new JToolBar();
JButton button1=new JButton("打开");
JButton button2=new JButton("添加");

button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
newfrm();
}
});

button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
rowadd();}});

toolbar.add(button1);
toolbar.add(button2);
getContentPane().add(toolbar,BorderLayout.NORTH);

desktop = new JDesktopPane();
getContentPane().add(desktop);

Dimension Size = Toolkit.getDefaultToolkit().getScreenSize();

setBounds(50,50,Size.width-100,Size.height-100);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public void newfrm() {
doc=new MyinnFrame();
desktop.add(doc);
try {
doc.setVisible(true);
doc.setSelected(true);
} catch (java.beans.PropertyVetoException e2) {}
}

public void rowadd() {
Object[] newdata={"","",new Integer(0),new Boolean(false),"",""};
doc.dataModel.addRow(newdata);
}

public static void main(String args[])
{

new MainFrame();

}
}

class MyinnFrame extends JInternalFrame
{
JTable table;
DefaultTableModel dataModel;
JScrollPane tableScrollPane;

static int incount=0;
static final String[] columns = {"姓名","性别","年龄","婚否","电话","地址"};

public MyinnFrame()
{


super("",true,true,true,true);

dataModel = new DefaultTableModel() {
public Class getColumnClass(int col)
{
return getValueAt(0,col).getClass();
}};

dataModel.setColumnIdentifiers(columns);

//创建一个已有表格模式的表格
table=new JTable(dataModel);
table.setPreferredScrollableViewportSize(new Dimension(500,70));

tableScrollPane = new JScrollPane(table);
tableScrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));

JPanel top = new JPanel();
top.setBorder(new EmptyBorder(10, 10, 10, 10));
top.setLayout(new BorderLayout());
top.add(tableScrollPane,BorderLayout.CENTER);

setContentPane(top);

setBounds(30*incount,30*incount,600,400);
incount++;
setTitle("通讯录"+incount);
}
}
...全文
73 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jxj12345678 2004-11-21
  • 打赏
  • 举报
回复
你每次一"打开",doc就指向最新的那个窗体.
而你"添加"又是:doc.dataModel.addRow(newdata);
当然就只加在最新的这个窗体了.
xlking 2004-11-21
  • 打赏
  • 举报
回复
没人帮忙啊?

62,614

社区成员

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

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