简体   繁体   English

如何隐藏 JFrame,然后在消息框关闭时打开它

[英]How to hide a JFrame but then open it when the message box closes

I'm trying to create a MessageBox Creator.我正在尝试创建一个 MessageBox Creator。

I Have tried to hide the Creator when the Message Box Opens and then show when the Message Box Closes.我试图在消息框打开时隐藏创建者,然后在消息框关闭时显示。 I am using the following plugins for Eclipse Neon:我正在为 Eclipse Neon 使用以下插件:

  • WindowBuilder窗口生成器
  • Swing Designer挥杆设计师

to help me create the program.帮助我创建程序。

A similar one is here but it did not help: Click Me一个类似的在这里,但它没有帮助: 点击我

The source code is here:源代码在这里:

package org.us.me.****.messagebox.creator;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JProgressBar;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MessageBoxCreator {

    private JFrame frmD;
    private JTextField txtMessageGoesHere;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MessageBoxCreator window = new MessageBoxCreator();
                    window.frmD.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
         });
    }

    /**
     * Create the application.
     */
    public MessageBoxCreator() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmD = new JFrame();
        frmD.setTitle("MessageBox: Creator");
        frmD.setBounds(100, 100, 260, 113);
        frmD.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmD.getContentPane().setLayout(null);

        JTextField MessageBox = new JTextField();
        MessageBox.setText("Message goes here...");
        MessageBox.setBounds(10, 11, 222, 20);
        frmD.getContentPane().add(MessageBox);
        MessageBox.setColumns(10);

        JButton btnGenerate = new JButton("Generate");
        btnGenerate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, MessageBox);

            }
        });
        btnGenerate.setBounds(10, 42, 86, 23);
        frmD.getContentPane().add(btnGenerate);
    }
}

Please Help.请帮忙。

Seems like you are trying to hide the Frame on button click and then on popup's button click you want the initial frame back as visible.似乎您试图在单击按钮时隐藏框架,然后在单击弹出窗口的按钮时希望初始框架恢复可见。

The easiest and straight way forward way to do so is to create your own popup box.最简单直接的方法是创建自己的弹出框。

Try this out:试试这个:

  class CustomPopup extends JFrame
  {
    public CustomPopup()
    {
        frmD.setVisible(false);
        this.setName("Popup");
        this.setLayout(new BorderLayout());
        JLabel l = new JLabel("Enter Message here");
        JButton b = new JButton("Submit");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                frmD.setVisible(true);
                CustomPopup.this.setVisible(false);
                CustomPopup.this.dispose();
            }
        });

        this.add(l,BorderLayout.CENTER);
        this.add(b,BorderLayout.SOUTH);
        this.setSize(300, 150);
        this.setResizable(false);
        this.setDefaultCloseOperation(0);
        this.setVisible(true);
    }

}

Then in your button action listener do this:然后在您的按钮动作侦听器中执行以下操作:

btnGenerate.addActionListener(new ActionListener()
{
        public void actionPerformed(ActionEvent e) 
        {
            new CustomPopup();
        }
    });

Note: The above example is in with respect to your code.注意:上面的示例与您的代码有关。 I have used Border layout for popup in the example as it is the simplest to implement.我在示例中使用了用于弹出窗口的边框布局,因为它最容易实现。 I believe you can customize the look and feel of your custom popup on your own.我相信您可以自行自定义自定义弹出窗口的外观。 There are also more options to create popups with custom settings.还有更多选项可以使用自定义设置创建弹出窗口。

Refer to this for more info: http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html有关更多信息,请参阅: http : //docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

I think the design is not good, but to make the frame hide and show change this line:我认为设计不好,但为了让框架隐藏和显示改变这一行:

JOptionPane.showMessageDialog(null, MessageBox);

To

frmD.setVisible(false);
JOptionPane.showMessageDialog(null, MessageBox);
frmD.setVisible(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM