[JAVA100 example]013, composite dropdown list

Source: Internet
Author: User

Import java.awt.*;


import java.awt.event.*;


import javax.swing.*;


import javax.swing.border.*;


import java.util.*;


import Java.text.SimpleDateFormat;


/**


* <p>title:combobox dropdown field Demo </p>


* <p>description: Format today's date </p>
by selecting or entering a date format

* <p>copyright:copyright (c) 2003</p>


* <p>Filename:ComboBoxDemo.java</p>


* @version 1.0


*/


public class Comboboxdemo extends JPanel


implements ActionListener {


static JFrame frame;


JLabel result;


String Currentpattern;


/**


*<br> Method Description: constructor. Initializing the form widget


*<br> input Parameters:


*<br> return type:


*/


public Comboboxdemo () {


SetLayout (This, Boxlayout.page_axis) (new boxlayout);


string[] Patternexamples = {


"dd mmmmm yyyy",


"DD. Mm.yy ",


"Mm/dd/yy",


"yyyy. Mm.dd g´at´hh:mm:ss z ",


"EEE, MMM d,´´yy",


"h:mm a",


"H:mm:ss:sss",


"k:mm a,z",


"yyyy. MMMMM.DD GGG hh:mm AAA "


         };


currentpattern = patternexamples[0];


//Set up a specification user interface


JLabel patternLabel1 = new JLabel ("Enter a character format or");


JLabel PatternLabel2 = new JLabel ("Select one from the Drop-down list:");


JComboBox patternlist = new JComboBox (patternexamples);


patternlist.seteditable (TRUE),//Callout here ComboBox editable


Patternlist.addactionlistener (this);


//Create a display result user interface


JLabel ResultLabel = new JLabel ("Current date/Time",


jlabel.leading);//equivalent to left


result = new JLabel ("");


Result.setforeground (Color.Black);


Result.setborder (Borderfactory.createcompoundborder (


Borderfactory.createlineborder (color.black),


Borderfactory.createemptyborder (5,5,5,5)


     ));


//Layout Components


JPanel Patternpanel = new JPanel ();


patternpanel.setlayout (New BoxLayout (Patternpanel,


Boxlayout.page_axis));


Patternpanel.add (PATTERNLABEL1);


Patternpanel.add (PATTERNLABEL2);


Patternlist.setalignmentx (component.left_alignment);


Patternpanel.add (patternlist);


JPanel Resultpanel = new JPanel (new GridLayout (0, 1));


Resultpanel.add (ResultLabel);


Resultpanel.add (Result);


Patternpanel.setalignmentx (component.left_alignment);


Resultpanel.setalignmentx (component.left_alignment);


Add (Patternpanel);


Add (Box.createrigidarea (new Dimension (0, 10));


Add (Resultpanel);


SetBorder (Borderfactory.createemptyborder (10,10,10,10));


reformat ();


   }


/**


*<br> Method Description: Event handling


*<br> input Parameters:


*<br> return type:


*/


public void actionperformed (ActionEvent e) {


JComboBox cb = (JComboBox) e.getsource ();


String newselection = (string) cb.getselecteditem ();


Currentpattern = newselection;


reformat ();


   }


/**


*<br> Method Description: Format and display today's date


*<br> input Parameters:


*<br> return type:


*/


public void reformat () {


Date today = new Date ();


SimpleDateFormat formatter =


new SimpleDateFormat (Currentpattern);


try {


String datestring = Formatter.format (today);


Result.setforeground (Color.Black);


Result.settext (datestring);


} catch (IllegalArgumentException iae) {


Result.setforeground (color.red);


Result.settext ("Error:" + iae.getmessage ());


     }


   }


/**


*<br> Method Description: Main method


*<br> input Parameters:


*<br> return type:


*/


public static void Main (string[] args) {


jframe.setdefaultlookandfeeldecorated (TRUE);


Create a form


frame = new JFrame ("Comboboxdemo");


frame.setdefaultcloseoperation (jframe.exit_on_close);


//Create a face plate container


jcomponent Newcontentpane = new Comboboxdemo ();


Newcontentpane.setopaque (TRUE);


Frame.setcontentpane (Newcontentpane);


//Display form


Frame.pack ();


frame.setvisible (TRUE);


   }


}

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.