Java graphical interfaces are common for beginners and java graphic interfaces

Source: Internet
Author: User

Java graphical interfaces are common for beginners and java graphic interfaces

1. Create a single interface

Public void mainFrame () {HashMap <String, Component> views = new HashMap <String, Component> (); JFrame jf = new JFrame ("title "); // instantiate a JFrame object jf. setSize (500,300); // set the size position of the form jf. setLayout (null); // No layout jf. setResizable (false); // The jf cannot be minimized. setIconImage (new ImageIcon ("image/abc.jpg "). getImage (); // icon // sets the form close mode jf. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); jf. setVisible (true); // visualize the form // Dimension s = Toolkit in the center of the form. getdefatooltoolkit (). getScreenSize (); jf. setLocation (s. width-jf. getWidth ()/2, (s. height-jf. getHeight ()/2); // various components and containers JPanel; JLabel label; JTextField tfield; JButton button; JComboBox <String> combox ;//... // The first container panel = new JPanel (); panel. setBounds (0, 0, 1000, 40); // layout panel in the container. setLayout (new FlowLayout (FlowLayout. LEFT); // Add the container to the form jf. add (panel); // The first component JLabel label = new JLabel ("I Am a Label"); // add the component to the container panel. add (label); // The second component JTextField tfield = new JTextField (22); tfield. setFont (new Font ("", 0, 16); // tfield. setEditable (false); panel. add (tfield); // add the components to be operated to HashMap <String, Component> to facilitate the call of views. put ("txt_code", tfield); // Add ...}

2. Common Operations

String common operation length () // get the String length startsWith (Strings), endsWith (String s) // determine whether the prefix or suffix of the String is the same as that of the known String equals (String s) // compare two strings Integer. parseInt (string) Long. parseLong (string) Folat. valueOf (string ). floatValue () Double. valueOf (string ). doubleValue () // forcibly convert a String to valueOf (value) // convert a value to a String for retrieval: indexOf (Srting s) // retrieve indexOf (String s, int startpoint) // start from startpoint. If not,-1 string truncation is returned: substring (int startpoint) // Get substring (int start, int end) at rtpoint // replace the character in the string from start to end: replace (char old, char new) // replace the old analysis String StringTokenizer (String s) with new // construct a analyzer and use the default delimiter (space, line feed, carriage return, Tab, paper feed) StringTokenizer (String s, string delim) // delim is the custom delimiter nextToken () // obtain the language symbol boolean hasMoreTokens () in the String one by one. // true is returned as long as the String has a language symbol, otherwise, false countTokens () is returned. // you can obtain the total number of language symbol text boxes and character area text boxes TextField (). // you can create a text box with a length of one character and TextField (int x ). Text Box, x characters long TextField (String s) // construct the text box, display s, setText (String s), set the text to s getText () // get the text setEchoChar (char c) // set the display character to c setEditable (boolean) // set whether the text box can be modified by addActionListener () // Add the monitor removeActionListener () // remove the TextArea () in the monitor detection area // construct the detection area TextArea (String s) // construct the detection area, and display the s TextArea (String s, int x, int y) // construct the detection area, row x and column y show s TextArea (int x, int y) // construct the vertex area, row x, and column y TextArea (String s, int x, ing y, int scrollbar) scroll The bar value is TextArea. SCROLLBARS_BOTH; TextArea. SCROLLBARS_VERTICAL_ONLY TextArea. SCROLLBARS_HORIZONTAL_ONLY; TextArea. SCROLLBARS_NONE setText (String s) // sets the text to s; getText () obtains the text addTextListener () // adds a monitor; removeTextListener () removes the monitor insert (String s, int x) // Insert the text s replaceRange (String s, int x, int y) at x. // Replace the text append (String s) from x to y with s) // append the text s Int getCaretPosition (int n) to the end of the text // obtain the position of the cursor in the cursor area. Button () // construct Button B Utton (String s) // construct the button. The label is s setLabel (String s) // set the button label to s getLabel () // obtain the button label addActionListener () // Add the monitor removeActionListener () // remove the monitor Label () // construct the Label (String s) // construct the Label and display the s Label (String s, int x) // x is the alignment mode; Value: Label. LEFT; Label. RIGHT; Label. CENTER setText (String s) // sets the Text s getText () // gets the text setBackground (Color c) // sets the tag background Color setForeground (Color c) // set the font color selection box Checkbox () // construct the selection box Checkbox (String s) // Construct the selection box and specify the title s Checkbox (String s, boolean B) // B sets the initial status Checkbox (String s, boolean B, CheckboxGroup g) // g sets the group to which the listener belongs (the group becomes a single listener) addItemListener () // Add the monitor removeItemListener () // remove the monitor getState () // return whether the selected status of the Selection box is setState (boolean B) // set the status of the Selection box getLabel () // get the title of the Selection box setLabel (String s) // set the title of the Selection box to s. Select Control Choice () // construct the Selection control add (String s) // add an option to the Selection Control addItemListener () // Add the monitor removeItemListener () // remove the monitor getSele CtedIndex () // returns the index getSelectedItem () of the current option // returns the String of the current option representing insert (String s, int n) // Insert the option s remove (int n) removeAll () in n to scroll down the List () // construct the scroll List (int n) // The parameter n is the number of visible rows List (int n, boolean B) // The parameter B is set to whether multiple options can be added (String s) // add an option add (String s, int n) to the end of the list // add an option AddActionListener () at n // add the monitor addItemListener () to the scrolling list () // Add monitor remove (int n) to the options in the scroll list // Delete the remnoveAll () option at the beginning of n // delete all options getSelectedIndex () // return the current option Index getSelectedItem () // returns the string of the current option to represent some common methods of the component class void setBackground (Color c) // sets the component background Color void setForeground (Color c) // set the foreground color of the component void setFonts (Font f) // set the component Font void setBounds (int x, int y, int w, int h) // set the coordinates, x, y indicates coordinates in the container, w, h indicates width and height void setLocation (int x, int y) // move to x, y; void setSize (int w, int h) set the width and height of void setVisible (boolean B) // set whether the form is visible int getBounds (). wightth // get the int getBounds (). height // get the high int getBo Unds (). x // obtain the x coordinate. Note 1). To fix the size of JComboBox, you need to pay attention to it. 1. What type of layout is the layout of the Panel in your JComboBox? If it is not a null layout, adjust the layout to assign a proper size to JComboBox. 2. If you are using an empty layout, use the setSize method or the setBounds method to change the JComboBox size. setBounds can change the size and position of the control. 2) What is the difference between setBounds and setSize in Java: bounds is a combination of size and location

3. When setSize does not work for some components, use setPreferredSize, for example, button. setPreferredSize (new Dimension (60, 22 ));

4. Tables are generally created using models to facilitate update management:

JTable table; DefaultTableModel model = new DefaultTableModel (); // default model. setColumnIdentifiers (new Object [] {"a", "B", "c"}); // header table = new JTable (model ); JScrollPane scrollPaner = new JScrollPane (table); scrollPaner. setBounds (0, 0,985,500); panel. add (scrollPaner );

Clear and update table data:

// Obtain the default table model DefaultTableModel = (DefaultTableModel) table. getModel (); // clear the table while (model. getRowCount ()> 0) {model. removeRow (model. getRowCount ()-1);} // Insert the data in the table to the model. addRow (new Object [] {"a", "B", "c"}); // notifies the table to update the model. fireTableDataChanged ();

5. Message prompt box

// The following is a confirmation dialog box JOptionPane. showMessageDialog (null, "prompt content"); // blue exclamation mark, titled "message" JOptionPane. showMessageDialog (null, "content", "title", JOptionPane. ERROR_MESSAGE); // indicates the error message. You only need one JOptionPane button. showMessageDialog (null, "Warning box", "title", JOptionPane. WARNING_MESSAGE); JOptionPane. showMessageDialog (null, "error prompt box", "title", JOptionPane. ERROR_MESSAGE); JOptionPane. showMessageDialog (null, "Basic prompt box", "title", JOptionPane. PLAIN_MESSAG E); // No icon // The selection prompt box with the custom selection button below. The buttons and prompt messages can be customized/* showOptionDialog (Component parentComponent, Object message, String title, int optionType, int messageType, Icon, Object [] options, Object initialValue); * The OptionDialog dialog box is a fully customized dialog box, showOptionDialog is a method with eight parameters. The parameter "Object [] options" defines a set of Button arrays. * The return value of each button is its subscript in the array, when the parameter "Object [] options" is not empty, the parameters "int optionType" and "int messageType" do not play much role. * The parameter "Object I NitialValue "determines the selected button by default. */JOptionPane. showConfirmDialog (null, "prompt box", "title", JOptionPane. YES_NO_OPTION); // The returned value is 0 or 1 Object [] options = {"button 1", "button 2"}; // custom button JOptionPane. showOptionDialog (null, "prompt box", "title", JOptionPane. YES_NO_OPTION, JOptionPane. QUESTION_MESSAGE, null, options, options [0]); // showInoutDialog can be used as a prompt box with a drop-down list or input box, the user can also select or enter information to return Object [] obj2 = {"a", "B", "c"}; // drop-down String str = (String) JOptionPane. showInputDialog (null, "select", "title", JOptionPane. PLAIN_MESSAGE, new ImageIcon ("icon"), obj2, "B"); JOptionPane. showInputDialog (null, "enter \ n", "title", JOptionPane. PLAIN_MESSAGE );

 

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.