【问题标题】:One JFrame don't stop other frame's code一个 JFrame 不会停止其他框架的代码
【发布时间】:2012-12-13 08:07:56
【问题描述】:

当用户单击编辑按钮时,会向他们显示一个新框架,当新框架关闭时,我希望执行刷新线,但问题是,刷新线在关闭新框架之前执行...我正在使用 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 中制作图片中给出的表格。

【问题讨论】:

标签: java swing jframe jdialog joptionpane


【解决方案1】:

您想使用某种模式对话框。对话框的设计目的是在显示它们的位置“停止”程序的执行。

查看How to Make Dialogs。

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

更新示例

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

        }

    }


}

【讨论】:

  • @Haseeb Wali 使用JOptionPane
  • @MadProgrammer 处理这种情况的好主意...感谢您抽出宝贵时间。
【解决方案2】:

你可以使用

'editUserFrame.addWindowListener(new WindowAdapter()
{
    public void windowClosed(WindowEvent e){
    //call your method from here
        RefreshUserTable();
    }
};'

希望对你有帮助

【讨论】:

  • 还有其他几种方法可以做到这一点,但对于这种情况,我认为这已经足够了。很高兴我能提供一些帮助。
  • @HaseebWali 我注意到您的表单中有验证,因此使用 JOptionPane OK_CANCEL 不是理想的解决方案,因为您无法直接访问 OK 按钮 - 可以检索其 OK 按钮加班。总之使用 JOptionPane 的 OK_CANCEL 类型可能不是最好的解决方案。
  • 在这种情况下特别同意你的看法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-27
相关资源
最近更新 更多