Design a common dialog box with Java Swing

Source: Internet
Author: User
Tags response code

In Java swing programming, programmers can also customize dialog boxes, which can generally be inherited from the JDialog class. The following is a dialog box class code:

class Helpabout extends JDialog implements ActionListener


{


Javaword MainFrame;


JButton OKButton;


Javax.swing.Timer MyTimer;


int counter=0;


public helpabout (Javaword mainFrame)


 {


Super (MainFrame, "description of this procedure", true); True represents the modal dialog box


this.mainframe= MainFrame;


JPanel contentpanel=new JPanel ();


contentpanel.setlayout (New BorderLayout ());


JLabel imagelabel=new JLabel (New ImageIcon (". \images\javalogo.gif"));


Contentpanel.add (imagelabel,borderlayout.west);


JPanel authorinfopane=new JPanel ();


authorinfopane.setlayout (New GridLayout (1,1));


JTextArea aboutcontent=new JTextArea ("This program is a simple program that the author is learning about JAVA2 swing programming and is not used for commercial purposes." \ nthe author's contact is: \ n ");


Aboutcontent.enable (FALSE);


Authorinfopane.add (aboutcontent);


Contentpanel.add (Authorinfopane,borderlayout.north);


JPanel sysinfopane=new JPanel ();


sysinfopane.setlayout (New GridLayout (5,1));


Sysinfopane.setborder (Borderfactory.createloweredbevelborder ());


Contentpanel.add (Sysinfopane,borderlayout.center);


JLabel username=new JLabel ("The user name of this machine is:" +system.getproperty ("User.Name"));


JLabel osname=new JLabel ("The operating system of this machine is:" +system.getproperty ("Os.name"));


JLabel javaversion=new JLabel ("The version number of the Java SDK installed in this computer is:" +system.getproperty ("java.version"));


JLabel totalmemory=new JLabel ("Total number of memory possible for Java virtual machines in this computer:" +runtime.getruntime (). TotalMemory () + "bytes");


JLabel freememory=new JLabel ("The number of memory remaining in the Java Virtual machine in this computer? Quot;+runtime.getruntime (). Freememory () +" bytes ");


Sysinfopane.add (userName);


Sysinfopane.add (Osname);


Sysinfopane.add (javaversion);


Sysinfopane.add (totalmemory);


Sysinfopane.add (freememory);


JPanel okpane=new JPanel ();


okbutton=new JButton ("OK (O)", New ImageIcon (". \images\ok.gif"));


okbutton.setmnemonic (' O '); Set shortcut key to "Alt + O"


/* The following code is the rollover image of the Set case button/


Icon rollover = new ImageIcon (". \images\exit.gif");


Icon general = new ImageIcon (". \images\ok.gif");


Icon Press = new ImageIcon (". \images\help.gif");


okbutton.setrolloverenabled (TRUE);


Okbutton.seticon (General); Set the image
when leaving

Okbutton.setrollovericon (rollover); Image
When set on button

Okbutton.setpressedicon (press); Set the image
when the button is pressed

This.getrootpane (). Setdefaultbutton (OKButton); Set the button as the default button for this dialog box?


Okbutton.addactionlistener (this);


Okpane.add (OKButton);


Contentpanel.add ("South", Okpane);


Setcontentpane (Contentpanel);


//This.setresizable (FALSE); Set dialog box to be
size

mytimer=new Javax.swing.Timer (1000,this);


Mytimer.start ();


 }


public void actionperformed (ActionEvent parm1)


 {


//Todo:add your code here


if (Parm1.getsource () ==okbutton)


  {


Dispose ();


  }


Else if (Parm1.getsource () ==mytimer)


  {


counter++;


This.settitle ("The value of the current timer is:" +counter+ "seconds");


  }


 }


}

The dialog box appears in the event response code, with the following program code:

HelpAbout aboutDialog=new HelpAbout(this);
aboutDialog.setSize(500,500);
aboutDialog.show();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.