Today, we have created an example to create an interface and obtain the input minimum value, which involves many new knowledge points. I am still getting familiar with its usage.
It mainly uses the class methods in the "java. awt. *" and "javax. swing. *" software packages.
For example: EventQueue, JButton, JFrame, JPanel, JLabel, JOptionPane, JTextField, border, etc.
Run the result interface first:
650) this. width = 650; "src =" http://img1.51cto.com/attachment/201309/210537744.jpg "title =" Z85]} ~ 9366x%fjv%7f2ifbkd6.jpg "alt =" 210537744.jpg"/>
Code:
Package com. lixiyu; import java. awt. eventQueue; import java. awt. event. actionEvent; import java. awt. event. actionListener; import javax. swing. JButton; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. JOptionPane; import javax. swing. JPanel; import javax. swing. JTextField; import javax. swing. UIManager; import javax. swing. border. emptyBorder; public class ArrayMinValue extends JFrame {/**** /Private static final long serialVersionUID =-8388043412533827271L; private JPanel contentPane; private JTextField textField; private JLabel label; private JLabel label_1;/*** Launch the application. */public static void main (String [] args) {try {UIManager. setLookAndFeel ("com. sun. java. swing. plaf. nimbus. nimbusLookAndFeel ");} catch (Throwable e) {e. printStackTrace ();} EventQueue. invokeLat Er (new Runnable () {public void run () {try {ArrayMinValue frame = new ArrayMinValue (); frame. setVisible (true);} catch (Exception e) {e. printStackTrace () ;}});}/*** Create the frame. */public ArrayMinValue () {setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); setBounds (100,100,450,149); contentPane = new JPanel (); contentPane. setBorder (new EmptyBorder (5, 5, 5, 5); setContentPane (conten TPane); setTitle ("getting the minimum value of a one-dimensional array"); contentPane. setLayout (null); textField = new JTextField (); textField. setBounds (6, 36,414, 30); contentPane. add (textField); textField. setColumns (10); JButton button = new JButton ("\ u8BA1 \ u7B97"); button. addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {do_button_actionreceivmed (e) ;}}); button. setBounds (16, 76, 90, 30); contentPa Ne. add (button); label = new JLabel ("minimum value:"); label. setBounds (116, 82,304, 18); contentPane. add (label); label_1 = new JLabel ("enter multiple integers in the text box, separated by spaces. Example: 3 5 2 562 125 "); label_1.setBounds (6, 6,422, 18); contentPane. add (label_1);} protected void do_button_actionreceivmed (ActionEvent e) {String arrayStr = textField. getText (). trim (); // remove the Left and Right spaces if (arrayStr. equals ("") {JOptionPane. showMessageDialog (null, "enter a number"); return ;}for (int I = 0; I <arrayStr. length (); I ++) {// filter invalid input char charAt = arrayStr. charAt (I); if (! Character. isDigit (charAt) & charAt! = '') {JOptionPane. showMessageDialog (null, "the input contains non-numeric content"); textField. setText (""); return ;}string [] numStrs = arrayStr. split ("{1,}"); // split string int [] numArray = new int [numStrs. length]; // create an integer array // convert the input to an integer array for (int I = 0; I <numArray. length; I ++) {numArray [I] = Integer. valueOf (numStrs [I]);} int min = numArray [0]; // create the smallest variable for (int j = 0; j <numArray. length; j ++) {if (min> numArray [j]) {// extract the smallest integer min = numArray [j] ;}} label. setText ("the smallest number in the array is:" + min); // display the minimum value to the specified tag }}
Detailed analysis:
1. serialVersionUID
Private static final long serialVersionUID =-8388043412533827271L; // to maintain version compatibility during serialization, deserialization maintains the uniqueness of objects during version upgrade.
2. UIManager
UIManager. setLookAndFeel ("com. sun. java. swing. plaf. nimbus. NimbusLookAndFeel"); // Nimbus style appearance
3. Print exception (To print all exceptions and errors, catch Throwable instead of catch Exception.)
try { ...}throw(Exception e){ e.printStackTrace();}
4. EventQueue
/* The runnable run method is called in the designated thread of the system EventQueue. Parameter: runnable-Runnable object. Its run method should be executed synchronously on EventQueue */EventQueue. invokeLater (new Runnable () {public void run () {try {ArrayMinValue frame = new ArrayMinValue (); // create a form frame. setVisible (true); // visibility} catch (Exception e) {e. printStackTrace ();}}});
5. Close the EXIT_ON_CLOSE window.
Setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); // closes the operation in the default window after exiting the application.
6. setBounds usage
SetBounds (100,100,450,149);/* setBounds (int x, int y, int width, int height) The first two are the coordinates in the upper left corner of the component in the container, and the last two are the width and height of the component */
7. contentPane. setBorder (new EmptyBorder (5, 5, 5 ));
ContentPane = new JPanel (); contentPane. setBorder (new EmptyBorder (5, 5, 5, 5);/* set the Panel boundary. Border describes the boundary around the Panel ), emptyBorder is a blank boundary. The statement indicates that the internal border of contentPane is empty and has a thickness of 5 pixels. if you add a button on contentPane to set it to full ), then the button will be filled with an internal rectangle except the border */
8. setContentPane (contentPane );
SetContentPane (contentPane);/* JpanelcontentPane = newJpanel ();...... // Add other components to the Jpanel; frame. setContentPane (contentPane); // set the contentPane object to the content Panel of the frame */
9. contentPane. setLayout (null );
ContentPane. setLayout (null); // sets the layout manager. null indicates that no layout device name is selected.
10. actionreceivmed
Public void actionreceivmed (ActionEvent e) {// called when an operation occurs
This article is from the "ToBeContinued" blog, please be sure to keep this source http://leexy.blog.51cto.com/4136883/1303209