歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Java中JDialog的舉例

This program demonstrates the creation of a JDialog from a super-window. The created dialog is on the mode "Modal".

[java]

  1. package com.han;  
  2.   
  3. import java.awt.*;  
  4. import java.awt.event.ActionEvent;  
  5. import java.awt.event.ActionListener;  
  6.   
  7. import javax.swing.*;  
  8.   
  9. /** 
  10.  * This program demonstrates the creation of a JDialog from a super-window. 
  11.  * The created dialog is on the mode "Modal". 
  12.  * @author han 
  13.  * 
  14.  */  
  15. public class SwingJDialog {  
  16.     public SwingJDialog(){  
  17.         final JFrame jf=new JFrame("彈出窗體實驗");  
  18.         // Some methods defined by Toolkit query the native operating system directly.   
  19.         Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();   
  20.         int Swing1x=500;  
  21.         int Swing1y=300;  
  22.         jf.setBounds(screensize.width/2-Swing1x/2,screensize.height/2-Swing1y/2,Swing1x,Swing1y);  
  23.         jf.setVisible(true);  
  24.         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  25.         Container c=jf.getContentPane();  
  26.         c.setBackground(Color.pink);  
  27.         c.setLayout(null);  
  28.   
  29.         Dimension Swing1size=jf.getSize();  
  30.         JButton jb=new JButton("彈出對話窗");  
  31.         int jbx=100;  
  32.         int jby=30;  
  33.         jb.setBounds(Swing1size.width/2-jbx/2,Swing1size.height/2-jby/2,jbx,jby);  
  34.         //jb.setBounds(Swing1x/2-jbx/2,Swing1y/2-jby/2,jbx,jby);   
  35.         c.add(jb);  
  36.   
  37.         class Dialog1 {  
  38.             JDialog jd=new JDialog(jf,"JDialog窗體",true);  
  39.             Dialog1(){  
  40.   
  41.                 jd.setSize(300,200);  
  42.                 Container c2=jd.getContentPane();  
  43.                 c2.setLayout(null);  
  44.                 JLabel jl=new JLabel("只是一個對話框");  
  45.                 jl.setBounds(0,-20,100,100);  
  46.                 JButton jbb=new JButton("確定");  
  47.                 jbb.setBounds(100,100,60,30);  
  48.                 c2.add(jl);  
  49.                 c2.add(jbb);  
  50.                 jbb.addActionListener(new ActionListener(){  
  51.                     @Override  
  52.                     public void actionPerformed(ActionEvent e) {  
  53.                         jd.dispose();  
  54.                         //System.exit(0);   
  55.                     }  
  56.   
  57.                 });  
  58.                 System.out.println("OK");  
  59.   
  60.                 jd.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);  
  61.   
  62.             }  
  63.         }  
  64.   
  65.         jb.addActionListener(new ActionListener(){  
  66.             public void actionPerformed(ActionEvent e){  
  67.                 new Dialog1().jd.setVisible(true);//彈出對話框   
  68.                 System.out.println("OK2");  
  69.             }  
  70.         });  
  71.         System.out.println("OK3");  
  72.     }  
  73.   
  74.     public static void main(String[] args){  
  75.         new SwingJDialog();  
  76.     }  
  77. }  
Copyright © Linux教程網 All Rights Reserved