2017-05-15 6 views
2

フォームJPNel内にいくつかのJDialogを使用して、不正なデータとフォーム提出をユーザーに通知しようとしています。JDialogとその所有者

JDialogコンストラクタとちょっと混乱しています。ダイアログをパネルにリンクしたいのですが(それが作成された場所であるため)、許可されるオーナーのパラメータはトップレベルのフレーム(JPanelからアクセスできないと思われます)またはダイアログ(私は私を助けてくれない)。

FrameのリファレンスをJPanelに渡すことはできますが、ちょっと変わったデザインではありませんか?あるいは、私はクラスを誤解しているのですか?もっと一般的にJDialogをインスタンス化すべきですか?

私は自分自身を明確にしました。役立つ場合は、私はそれを作ることができます。ありがとう。

答えて

2

許可されている唯一の所有者パラメータは、私は、私はあなたが使用することにより、パネルの親フレームにアクセスすることができますJPanelの

からアクセスすることができるとは思わないトップレベルのフレーム(以下のとおりです。

Window window = SwingUtilities.windowForComponent(yourPanelHere); 

それからちょうどダイアログの所有者としてウィンドウを使用

2

JComponent.getTopLevelAncestorはあなたのJPanelの所有者を与える:このコンポーネントは が任意に追加されていない場合

は、(いずれか 含むウィンドウまたはアプレット)このコンポーネントのトップレベルの先祖を返し、またはnullコンテナ。

0

をあなたはそれを試すことができます。

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 

public class DialogTest { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       DialogFrame frame = new DialogFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setVisible(true); 
      } 
     }); 
    } 

} 

/** 
    * Frame contains menu. When we choose menu "File-> About" JDialog will be shown 
*/ 
class DialogFrame extends JFrame { 

    public DialogFrame() { 
     setTitle("DialogTest"); 
     setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 

     // Menu 
     JMenuBar menuBar = new JMenuBar(); 
     setJMenuBar(menuBar); 
     JMenu fileMenu = new JMenu("File"); 
     menuBar.add(fileMenu); 

     // Add About & Exit. 
     // Choose About - > About 
     JMenuItem aboutItem = new JMenuItem("About"); 
     aboutItem.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent event) { 
       if (dialog == null) //if not 
       { 
        dialog = new AboutDialog(DialogFrame.this); 
       } 
       dialog.setVisible(true); // to show dialog 
      } 
     }); 
     fileMenu.add(aboutItem); 

     // When Exit 
     JMenuItem exitItem = new JMenuItem("Exit"); 
     exitItem.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent event) { 
       System.exit(0); 
      } 
     }); 
     fileMenu.add(exitItem); 
    } 

    public static final int DEFAULT_WIDTH = 300; 
    public static final int DEFAULT_HEIGHT = 200; 

    private AboutDialog dialog; 
} 

/* 
* Modal dialog waits on click 
*/ 
class AboutDialog extends JDialog { 

    public AboutDialog(JFrame owner) { 
     super(owner, "About DialogTest", true); 

     // Mark with HTML centration 
     add(new JLabel(
       "<html><h1><i>Все о Java</i></h1><hr>" 
       + "Something about java and JDialog</html>"), 
       BorderLayout.CENTER); 

     // When push "ok" dialog window will be closed 
     JButton ok = new JButton("ok"); 
     ok.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent event) { 
       setVisible(false); 
      } 
     }); 

     // Button ОК down of panel 
     JPanel panel = new JPanel(); 
     panel.add(ok); 
     add(panel, BorderLayout.SOUTH); 
     setSize(260, 160); 
    } 
} 
関連する問題