歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java中JDialog的舉例

Java中JDialog的舉例

日期:2017/3/1 10:35:54   编辑:Linux編程

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