有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在尝试创建MessageBox创建者

我试图在消息框打开时隐藏创建者 然后在消息框关闭时显示。我正在为Eclipse Neon使用以下插件:

  • WindowBuilder
  • Swing设计器

来帮助我创建程序

这里有一个类似的例子,但没有帮助:Click Me

源代码如下:

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

请帮忙


共 (2) 个答案

  1. # 1 楼答案

    似乎您正试图在单击按钮时隐藏帧,然后在弹出窗口的单击按钮时希望初始帧恢复为可见

    最简单直接的方法是创建自己的弹出框

    试试这个:

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

    然后在按钮操作侦听器中执行以下操作:

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

    注意:上面的示例与您的代码有关。我在示例中使用了弹出窗口的边框布局,因为它是最容易实现的。我相信你可以自定义自己的自定义弹出窗口的外观和感觉。还有更多选项可用于创建带有自定义设置的弹出窗口

    有关更多信息,请参阅此文档:http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

  2. # 2 楼答案

    我觉得这个设计不好,只是为了让画面隐藏和显示变化 这一行:

    JOptionPane.showMessageDialog(null, MessageBox);
    

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