java - 一个 JFrame 不会停止其他框架的代码

标签 java swing jframe jdialog joptionpane

当用户单击编辑按钮时,会向他们显示一个新框架,当新框架关闭时,我希望执行刷新行,但问题是,在关闭新框架之前执行刷新行...我正在使用 NetBeans 来制作 UI。

//If user clicks on edit button new frame will be shown to him 
EditUserFrame editUserFrame = new EditUserFrame();
Iterator itr = userList.iterator();
while(itr.hasNext()){
    user = (Users)itr.next();
    if((user.getF_name().equals(f_name) && user.getL_name().equals(l_name))){
        break;
    }
}//End of While Loop
editUserFrame.setUserObj(user);
editUserFrame.getExistingValues();
editUserFrame.setVisible(true);
//I want this line to be executed when the editUserFrame is closed..
RefreshUserTable();

我想要像这样的新框架,是否可以按照你们的建议在 JOPtionPane 中制作图中给出的表格。

enter image description here

最佳答案

您想要使用某种模式对话框。对话框被设计为在显示时“停止”程序的执行。

查看How to Make Dialogs .

您还可以使用 JOptionPane 让您的生活更轻松,因为它们可以相对轻松地配置不同的按钮/选项。查看JOptionPane Features概览

已更新示例

enter image description here

public class TestOptionPane01 {

    public static void main(String[] args) {
        new TestOptionPane01();
    }

    public TestOptionPane01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }
                
                FormPane formPane = new FormPane();
                int result = JOptionPane.showConfirmDialog(
                                null, 
                                formPane, 
                                "Edit User", 
                                JOptionPane.OK_CANCEL_OPTION, 
                                -1);
                
                switch (result) {
                    
                    case JOptionPane.OK_OPTION:
                        System.out.println("You selected okay");
                        break;
                    case JOptionPane.OK_CANCEL_OPTION:
                        System.out.println("You selected cancel");
                        break;
                    
                }
                
            }
        });
    }
    
    public class FormPane extends JPanel {
    
        public FormPane() {
            
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(2, 2, 2, 2);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.anchor = GridBagConstraints.WEST;
            add(new JLabel("Edit User"), gbc);
            gbc.gridy++;
            add(new JSeparator(), gbc);
            
            gbc.gridwidth = 1;
            gbc.weightx = 0;
            gbc.fill = GridBagConstraints.NONE;
            gbc.gridy++;
            add(new JLabel("Fill all fields to edit user tecord"), gbc);

            gbc.gridy++;
            gbc.gridheight = 2;
            add(new JLabel("Select Image Icon"), gbc);
            gbc.gridheight = 1;
            gbc.gridy += 2;
            add(new JLabel("First Name"), gbc);
            gbc.gridy++;
            add(new JLabel("Last Name"), gbc);
            gbc.gridy++;
            add(new JLabel("Password"), gbc);
            gbc.gridy++;
            add(new JLabel("Confirm Password"), gbc);
            gbc.gridy++;
            add(new JLabel("Email Address"), gbc);
            
            gbc.gridx++;
            gbc.gridy = 3;
            gbc.anchor = GridBagConstraints.EAST;
            add(new JLabel("Maximum Image Size 32x32"), gbc);
            gbc.gridy++;
            add(new JButton("Select"), gbc);
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridy++;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JTextField(10), gbc);

            gbc.gridx++;
            gbc.gridy = 3;
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.gridheight = 3;
            gbc.fill = GridBagConstraints.BOTH;
            JPanel panel = new JPanel();
            panel.setBorder(new BevelBorder(BevelBorder.RAISED));
            add(panel, gbc);
            gbc.gridy += 3;
            gbc.gridheight = 1;
            gbc.fill = GridBagConstraints.NONE;
            add(new JButton("Clear Image"), gbc);
            
        }
        
    }
    
    
}

关于java - 一个 JFrame 不会停止其他框架的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13855526/

相关文章:

java - 如何正确重写 Runnable.run(),以便它可以接受参数?

java - 使用 .clone() 方法和 = 号克隆对象有什么区别?

java - 结合 Java Swing 和 Java3D : performance problems with concurrency

java - 强制顶部 JPanel 调整大小至其子 JPanel 的高度

java - 如何将我的 jfreecharts 重定向到选项卡?

java - 如何在框架中显示图像?

java - 试图找到Java数组的最小值

java - 为什么即使不满足循环内设置的条件,我的循环错误消息也会打印出来

java - GUI 使用 JFrame 和 JPanel 绘制自定义形状

java - 如何设置 JLabel 的边距?