2016-10-01 72 views
-1

我正在嘗試創建MessageBox Creator。如何隱藏JFrame,但隨後在消息框關閉時打開它

我試圖隱藏創建者當消息框打開 ,然後顯示消息框關閉時。我使用下面的Eclipse插件霓虹燈:

  • 的WindowBuilder
  • 搖擺設計師

幫我創建程序。

一個類似的是在這裏,但它並沒有幫助: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); 
    } 
} 

請幫助。

+0

順便說一下,我仍然看着,但我仍然會在這個領域尋找答案 –

+1

你爲什麼要這樣做?似乎迷失方向,特別是如果消費者的框架最大化 –

回答

0

好像你正試圖隱藏框架上的按鈕點擊然後彈出的按鈕點擊你想要的初始框架可見。

最簡單直接的方法是創建自己的彈出框。

嘗試了這一點:

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

0

我認爲設計不好,而是使框架隱藏和顯示變化 這一行:

JOptionPane.showMessageDialog(null, MessageBox); 

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