gpt4 book ai didi

java - 为什么我的 JPanel 组件会自动填充?

转载 作者:行者123 更新时间:2023-12-02 03:36:21 26 4
gpt4 key购买 nike

我正在设计应用程序的 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
}

}

}

最佳答案

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

关于java - 为什么我的 JPanel 组件会自动填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37409402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com