如何在关闭孩子后启用父JFrame?

时间:2015-08-11 01:00:19

标签: java swing

我有一个框架,在这个框架上我有一个关于MenuItem的菜单。当我们选择它时,程序会打开一个带有文本的新JPanel,然后使用OK按钮,父面板的启用状态将设置为false。

现在出现了问题。当我们点击OK,然后我想关闭这个About面板,我想转向父面板,我想启用它!

请告诉我,怎么样?

2 个答案:

答案 0 :(得分:0)

考虑使用$user = $db->updateUser($user_group, $uid); 对约会对话的结束事件做出反应。您可以在框架或对话框的构造函数中添加它,只需相应地设置变量。

public void startWorking() {
        ServerSocket server = null;
        try {
            server = new ServerSocket( portNo );
            logger.info( "Server started" );
            while ( true ) {
                final Socket socket = server.accept();
                new MessageHandler( socket );
            }
        }
        catch ( Exception e ) {
            logger.error( "something went wrong here!!!", e );
        }
        finally {
            try {
                server.close();
            }
            catch ( IOException e ) {
                logger.error( "something went wrong here!!!", e );
            }
        }
    }

    class MessageHandler extends Thread {

        private Socket socket;

        public MessageHandler( Socket socket ) throws IOException {
            this.socket = socket;
            start();
        }

        @Override
        public void run() {
            logger.info( "Connected on ----> " + socket );
            try {
                logger.info( "Fetching input stream...." );
                InputStream inputStream = socket.getInputStream();
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] b = new byte[1];
                logger.info( "Reading data...." );
                int pipeCount = 0;

                while ( inputStream.read( b ) != -1 ) {
                    bos.write( b );
                    if ( ( char ) b[0] == '|' ) {
                        pipeCount++;
                    }

                    if ( pipeCount == 4 ) {
                        syso( bos.toByteArray() );
                        bos.reset();
                        pipeCount = 0;
                    }
                }

                logger.info( "Finished reading on socket ----> " + socket );
            }
            catch ( Exception e ) {
                logger.error( "something went wrong here!!!", e );
            }
            finally {
                try {
                    socket.close();
                }
                catch ( IOException e ) {
                    logger.error( "something went wrong here!!!", e );
                }
            }
        }
    }

如果您真的只有转换WindowListener,请使用myDialog.addWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent e) { parentFrame.setEnabled(true); } });

JPanel

正如评论中所提到的,使用模态ButtonListener将是一种更优雅的方法,可以解决在对话框处于活动状态时禁用父框架的问题。 Here is a tutorial

答案 1 :(得分:0)

为什么不使用JOptionPane(特别是showMessageDialog方法)?您可以在那里指定Object(例如JPanel),它将在模态对话框中显示。看一下我为您编写的示例代码(我使用了JButton,但JMenuItem也是如此):

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class AboutDialogDemo extends JFrame {

    private final JButton btnAbout = new JButton("About...");

    public AboutDialogDemo() {
        final JFrame thisFrame = this;
        btnAbout.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(thisFrame, new AboutPanel());
            }
        });
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(btnAbout, BorderLayout.PAGE_END);
        pack();
    }

    public static void main(String[] args) {
        AboutDialogDemo frame = new AboutDialogDemo();
        frame.setSize(400, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

class AboutPanel extends JPanel {

    private final JLabel lblAbout = new JLabel("Sample about text");

    public AboutPanel() {
        setLayout(new BorderLayout());
        add(lblAbout, BorderLayout.PAGE_START);
    }
}

我希望你会发现它很有用