1

首先,让我指出我是 Java 桌面 GUI 编程的新手。我创建了一个可以容纳多个 JInternalFrames 的桌面窗格。目前,每个 JinternalFrame 都包含相同的 GUI 组件,并且可以通过单击菜单项来创建新的 jInternalFrame(此处称为内部框架)。

每个内部 Frame 上的元素如下:3 个 Jlabels、3 个 JTextFields、一个 Jbutton 和一个 Jtable。渲染第一个内部框架后,我用信息填充 3 个文本字段,然后单击按钮,一个新行被添加到表中。该行包含用于填充文本字段的信息。

打开第二个内部框架在视觉上给了我第一个的确切界面。我以同样的方式填充这个框架,点击按钮,瞧,第二个框架中的表格被填充了。

当我返回之前的内部框架时,问题就出现了。我可以毫无问题地输入文本字段,但单击填充表格的按钮会导致填充第二个内部框架中的表格。我怀疑我正在共享一个数据模型,但不确定如何在每个框架的 GUI 相同时为 jInternalFrame jTables 创建不同的数据模型

下面是代码(因为我是 Java Desktop GUI 开发的新手,所以我遵循了 Oracle How-To 之一来呈现内部框架本身 - 非常简单的示例):

public class InternalFrames extends JFrame 
                        implements ActionListener  {
private static final long serialVersionUID = 1L;
JDesktopPane desktop;

JLabel label1;
JLabel label2;
JLabel label3;
JLabel label4;

JTextField text1;
JTextField text2;
JTextField text3;

JTable table1;

public InternalFrames(){
    super("Practice Internal Frames");

    int inset = 50;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds(inset,inset,screenSize.width - (inset*2),screenSize.height - (inset*2));
    desktop = new JDesktopPane();

    createFrame();
    setContentPane(desktop);

    setJMenuBar(setMenuBar());


}

protected JMenuBar setMenuBar(){

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Document");
    menu.setMnemonic(KeyEvent.VK_D);
    menuBar.add(menu);

    JMenuItem menuItem = new JMenuItem("New");
    menuItem.setMnemonic(KeyEvent.VK_N);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.ALT_MASK));
    menuItem.setActionCommand("New");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    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;
}

public void actionPerformed(ActionEvent e){
    if ("New".equals(e.getActionCommand())){
        createFrame();
    } else if ("Quit".equals(e.getActionCommand())){

    }

}

protected void createFrame(){
    InternalFrame internalFrame = new InternalFrame();

    label1 = new JLabel("Name");
    label2 = new JLabel("Email Address:");
    label3 = new JLabel("Mobile Number:");
    label4 = new JLabel("Test Frames");

    label1.setSize(100,10);
    label2.setSize(100,10);
    label3.setSize(100,10);
    label4.setSize(200,10);

    text1 = new JTextField(40);
    text2 = new JTextField(40);
    text3 = new JTextField(40);

    internalFrame.setLayout(new MigLayout());
    internalFrame.getContentPane().add(label1);
    internalFrame.getContentPane().add(text1, "wrap");
    internalFrame.getContentPane().add(label2);
    internalFrame.getContentPane().add(text2, "wrap");
    internalFrame.getContentPane().add(label3);
    internalFrame.getContentPane().add(text3,"wrap");
    internalFrame.getContentPane().add(label4, "span 2, wrap");
    internalFrame.getContentPane().add(new JScrollPane(createTable()), "span 2 2, wrap");
    internalFrame.getContentPane().add(createButton());


    internalFrame.setVisible(true);
    desktop.add(internalFrame);

    try {

        internalFrame.setSelected(true);

    } catch (java.beans.PropertyVetoException pve){ }


}

protected JTable createTable(){
    DefaultTableModel dModel = new DefaultTableModel();
    table1 = new JTable(dModel);
    dModel.addColumn("Name");
    dModel.addColumn("Email Address");
    dModel.addColumn("Mobile Number");

    return table1;
}

protected JButton createButton(){
    JButton button1 = new JButton("Add New List Member");
    button1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            //Execute when button is pressed
            System.out.println("You clicked the button");
            populateTable(table1);
        }
    });
    return button1;
}

protected void populateTable(JTable theTable){

    if (validateEntry() == 0)
    {
        ListMembers listMember = new ListMembers();
        listMember.setName(text1.getText());
        listMember.setEmailAddress(text2.getText());
        listMember.setMobilePhone(text3.getText());
        Object[] data = {listMember.getName(),listMember.getEmailAddress(),listMember.getMobilePhone()};
        DefaultTableModel dm = (DefaultTableModel) table1.getModel();
        dm.addRow(data);


    }

}

private static void createAndShowGUI(){
    JFrame.setDefaultLookAndFeelDecorated(true);
    InternalFrames internalFrame = new InternalFrames();
    internalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    internalFrame.setVisible(true);

}


public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });

}
}

任何帮助将不胜感激。

4

1 回答 1

1

Each time you create a new frame, you initialize a single field (table1) with this newly created table. And the listener of every button you create populates the table referenced by this unique table1 variable. The same goes for your text fields, etc.

Either only use local variables, and pass them between methods (for example, you need to pass the table you just created to the method creating the button, in order for this button to populate this table. Or you extract all the code to a new class, MyInternalFrame (choose a better name), make the table, textfields and buttons instance fields of this new class, and create a new instance of this MyInternalFrame class each time you need a new one.

This second solution looks like the best one to me.

于 2012-09-29T14:05:27.663 回答