【问题标题】:Why my JPanel components get autofilled?为什么我的 JPanel 组件会自动填充?
【发布时间】:2016-05-24 17:37:37
【问题描述】:

我正在设计我的应用程序的 GUI,但我的 Swing 组件之间的通信出现了一些问题。 我有一个 MainGUI 类,在用户单击时必须创建并显示一个新的 JDialog 对象。对话框会按我的意愿弹出,但它总是显示我上次访问它时输入的先前值。如何防止这种情况以及这种行为的原因是什么?我想我在 openActorDialog() 函数上做错了。

public class MainGUI extends JPanel {
private ArrayList<Actor> actorList = new ArrayList<Actor>();
private JTextField field = new JTextField(10);
private JButton openDialogeBtn = new JButton("Open Dialog");
private String description;
// here my main gui has a reference to the JDialog and to the
// MyDialogPanel which is displayed in the JDialog

private JDialog dialog;

public MainGUI() {

    setLayout(new BorderLayout(0, 0));
    add(openDialogeBtn);
    field.setEditable(false);
    field.setFocusable(false);

    add(field);

    JMenuBar menuBar = new JMenuBar();
    add(menuBar, BorderLayout.NORTH);

    JMenu mnFile = new JMenu("File");
    menuBar.add(mnFile);

    JMenu mnNew = new JMenu("New");
    mnFile.add(mnNew);

    JMenuItem mntmSystem = new JMenuItem("system");
    mnNew.add(mntmSystem);

    JMenuItem mntmUseCase = new JMenuItem("use case");
    mnNew.add(mntmUseCase);

    JMenuItem mntmActor = new JMenuItem("actor");
    mnNew.add(mntmActor);

    JTree tree = new JTree();
    add(tree, BorderLayout.WEST);

    /*
     * Here we set the actions to be performed when the user interacts
     */

    mntmActor.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            openActorDialog();
        }
    });

}

private void openActorDialog() {
    //Creation of the JDialog
    CreateActorDialog createActorPanel = new CreateActorDialog();
    if (dialog == null) {
        Window win = SwingUtilities.getWindowAncestor(this);
        if (win != null) {
            dialog = new JDialog(win, "Create an actor", ModalityType.APPLICATION_MODAL);
            dialog.getContentPane().add(createActorPanel);
            dialog.setSize(500, 360);
            dialog.setLocationRelativeTo(null);

        }
    }
    dialog.setVisible(true); // here the modal dialog takes over

    // this line starts *after* the modal dialog has been disposed
    // **** here's the key where I get the String from JTextField in the GUI
    // held
    // by the JDialog and put it into this GUI's JTextField.

    Actor a = new Actor(createActorPanel.getActorNameFromDialog(),
            createActorPanel.getActorDescriptionFromDialog());
    field.setText(createActorPanel.getActorNameFromDialog());
    actorList.add(a);
    System.out.println("It is written:" + createActorPanel.getActorDescriptionFromDialog());
    for (Actor actor : actorList) {
        System.out.println(actor.name + " with description :" + actor.description); // Will
                                                                                    // invoke
                                                                                    // override
    }

}

}

【问题讨论】:

  • CreateActorDialog 在做什么?
  • CreateActorDialog 是一个扩展 JPanel 的类,并有一些文本字段供用户设置值。
  • dialog,一旦创建,将始终相同,您永远不会替换对话框上的createActorPanel 实例,因此它将包含上次显示时的旧值。替换组件(确保删除旧组件)或使用某种“重置”方法来重置其值
  • 我只是将对话框设置为 null ,正如 Huellif 建议的那样,现在它工作正常 :)

标签: java eclipse swing


【解决方案1】:

您必须在使用后或使用前再次将对话框设置为空,否则您将看到旧对话框。

【讨论】:

  • 啊,我没有注意到这个……现在它工作正常了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
  • 1970-01-01
相关资源
最近更新 更多