Interface of the learning process details

1.java consistent string determined in two ways.

(1) calls equals (object) method.

String1.equals (string2), the value of the parameter object (string2) comparing the current object (string1) contains the contains the value are equal, are equal if equals () method returns true, otherwise to false, equals () Comparative considering characters character case-sensitive.

 equalsIgnoreCase () can be case-insensitive comparison of two strings.

 String str1 = "hello Java!"; // instantiate an object str1 direct assignment

Boolean result =str1.equals(“hello Java!”);   // result=true

Boolean result =str1.equals(“hello java!”);// result=false;

Boolean result =str1.equalsIgnoreCase(“hello java!”); //result=true;

(2) == Comparison operators

Comparison operator == whether two object references an instance of the same.

String str1 = "hello"; // instantiate an object str1 direct assignment

String str2 = "hello"; // instantiate an object str2 direct assignment

 Boolean result1=(str1==str2);// result=true

String str3 = new String ( "hello"); // constructor assignment

Boolean result2=(str1==str3); //result=false



2. Generate a random number manner

A, java.lang.Math class random () method;

This call Math.random () function returns the double value with a positive sign, which is greater than or equal to 0.0 and less than 1.0, i.e., is in the range [0.0, 1.0) and close the left and right open interval, the return value is a pseudo-random selection number, uniformly distributed within the range (approximately).

The first time the method is called, it creates a new pseudo random number generator, exactly the same with the following expression

new java.util.Random

Thereafter, the new pseudo-random number generator used for this method all calls, but can not be used elsewhere.

This method is fully synchronized, allows multiple threads without errors. However, if many threads need to generate pseudorandom numbers at a very high rate, then this may reduce contention for each thread has its own pseudo-random number generator used.  

 

Two, java.util.Random class;

1, class java.util.Random random algorithm implemented in pseudo-random, i.e. regular random, is called a regular interval randomly generated numbers within a given species (SEED) is;

2, the same number of seed Random object, the same number of generated random numbers are identical;

3, the method of generating a random number Random class is uniformly distributed, the probability number that is generated inside the equalization section;


3. The drop-down list is implemented by JComboBox class constructor as usual.

  • JComboBox (): Creates an empty JComboBox object.
  • JComboBox (ComboBoxModel aModel): Creates a JComboBox, its options from an existing ComboBoxModel.
  • JComboBox (Object [] items): Creates a JComboBox that contains the specified elements in the array.


JComboBox class provides a plurality of member items method for operating a drop-down list box, as shown in Table 1.
 

Table 1 JComboBox class conventional method
Method name Explanation
void addItem(Object anObject) Adds the specified object is added as an option to the drop-down list box
void insertItemAt(Object anObject,int index) Insert the item at the specified index drop-down list box
void removeltem(0bject anObject) In the drop-down list box to delete the specified object item
void removeItemAt(int anlndex) Delete the specified position in the drop-down list box object entry
void removeAllItems() Remove all items from the drop-down list box
getItemCount int () Returns the number of items in the drop-down list box
Object getItemAt(int index) Gets the index of the list item, an index from 0
int getSelectedIndex() Gets the index of the currently selected
Object getSelectedltem() Gets the currently selected item


JComboBox able to respond to events and ItemEvent ActionEvent event, the timing of which is ItemEvent triggered when the drop-down list box, the option to change the timing ActionEvent is triggered when the user input option directly on the JComboBox and press Enter. To deal with these two events, we need to create the corresponding event class and implement the interface and ItemListener ActionListener interface. 

Example 1

Creating a window using the JFrame component, and then create a drop-down list box contains four options to use JComboBox class. Specific codes are as follows:

 
 
package ch17;import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JComboBoxDemo{public static void main(String[] args)
{
    JFrame frame=new JFrame("Java下拉列表组件示例");
    JPanel jp=new JPanel();
     //创建面板
    JLabel label1=new JLabel("证件类型:"); 
    //创建标签
    JComboBox cmb=new JComboBox();
     //创建
    JComboBoxcmb.addItem("--请选择--");
     //向下拉列表中添加一项
    cmb.addItem("身份证");
    cmb.addItem("驾驶证");
    cmb.addItem("军官证");
    jp.add(label1);
    jp.add(cmb);frame.add(jp);
    frame.setBounds(300,200,400,100);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

You can not add JTextArea when set to null layout

Published 34 original articles · won praise 6 · views 4751

Guess you like

Origin blog.csdn.net/qq_42712280/article/details/104300530