Java中JDialog的举例

Linux大全评论854 views阅读模式

[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. }  

企鹅博客
  • 本文由 发表于 2020年8月9日 03:08:53
  • 转载请务必保留本文链接:https://www.qieseo.com/173769.html

发表评论