Swing Common components text box and text area _java

Source: Internet
Author: User
Tags gettext

The use of JTextField (text box)

JTextField is a lightweight component that allows you to edit single-line text, cut, Copy, paste, shortcut keys, and so on, and automatically scrolls text if the length of the text exceeds the display range, JTextField class
1.JTextField Common methods of construction:
JTextField () constructs a new TextField.
JTextField (int columns) constructs a new empty TextField with the specified number of columns.
JTextField (String text) constructs a new TextField initialized with the specified text.
JTextField (String text, int columns) constructs a new TextField initialized with the specified text and columns.
2.JTextField Common methods:
SetText (String) to set text values in a text field
GetText () returns the input text value in the text field
GetColumns () returns the number of columns in a text field
Seteditable (Boolean) sets whether the text field is read-only
Examples of 3.JTextField use:

Package ch10; 
 
Import java.awt.event.*; 
 
Import javax.swing.*; 
  public class Logintest extends JFrame implements ActionListener {private JPanel JP = new JPanel (); 
  JLabel name = new JLabel ("Please enter user name"); 
  JLabel password = new JLabel ("Please enter password"); 
  JLabel show = new JLabel (""); 
  Private jlabel[] jl = new Jlabel[]{name,password,show}; 
  JButton login = new JButton ("login"); 
  JButton reset = new JButton ("reset"); 
  Private jbutton[] jb = new Jbutton[]{login,reset}; 
  Private JTextField jname= new JTextField (); 
  Private JPasswordField Jpass = new JPasswordField (); 
    Public Logintest () {jp.setlayout (null); 
      for (int i=0;i<2;i++) {jl[i].setbounds (30,20+40*i,180,20); 
      Jb[i].setbounds (30+110*i,100,80,20); 
      Jb[i].addactionlistener (this); 
      Jp.add (Jl[i]); 
    Jp.add (Jb[i]); 
    } jname.setbounds (130,15,100,20); 
    Jp.add (Jname); 
    Jname.addactionlistener (this); 
    Jpass.setbounds (130,60,100,20); 
    Jp.add (Jpass);Jpass.addactionlistener (this); 
    Jpass.setechochar (' * '); 
    Jl[2].setbounds (10,180,270,20); 
    Jp.add (jl[2]); 
    This.add (JP); 
    This.setbounds (200,200,300,300); 
    This.setvisible (TRUE); 
    This.settitle ("Login Window"); 
  This.setdefaultcloseoperation (Jframe.exit_on_close); 
    public void actionperformed (ActionEvent a) {if (A.getsource () ==jname) {jpass.requestfocus (); 
      else if (A.getsource () ==jb[1]) {Jl[2].settext (""); 
      Jname.settext (""); 
      Jpass.settext (""); 
    Jname.requestfocus (); else {if (Jname.gettext (). Equals ("Lixiang") &&string.valueof (Jpass.getpassword ()). Equals ("20140 
      7239 ") {Jl[2].settext (" login successful, Welcome to your arrival! "); else {Jl[2].settext ("Sorry, your password or username is wrong!") 
      "); 
  }} public static void Main (String args[]) {new logintest ();  } 
}

Second, the use of JTextArea (text area)

1.JTextArea Common methods of construction:
JTextArea () constructs a new TextArea.
JTextArea (String text) constructs a new TextArea that displays the specified text.
JTextArea (int rows, int columns) constructs a new empty TextArea with the specified number of rows and columns.
JTextArea (String text, int rows, int columns) constructs a new TextArea with the specified number of text, rows, and columns.

Use examples:

JTextArea T1 = new JTextArea ();
JTextArea t2 = new JTextArea (2, 8);
jtextarea t3 = new JTextArea ("JTextArea3");
jtextarea T4 = new JTextArea ("JTextArea4", 5, 10);

2.JTextArea Common methods:
Use examples:
T1.settext ("JTextArea1");//SetText () set the contents of the text display
T2.append ("JTextArea2");//The Append () method appends the given text to the end of the document.
T4.setlinewrap (TRUE);//sets the line-wrapping policy for the text area.
T4.setfont (New Font ("superscript italics", Font.Bold, 16)); Sets the current font.
T4.settabsize (2);//Use the Settabsize () method to set the skip distance of the [Tab] key

Put the JTextArea in the JScrollPane so that you can see the text entered more than the JTextArea height using the scrolling effect.

3.JTextArea use of the case:

Import java.awt.*; 
Import java.awt.event.*; 
 
Import javax.swing.*; 
  Implement Interface ActionListener public class JTextAreaDemo3 implements ActionListener {JFrame JF; 
  JPanel JPanel; 
  JButton jb1, JB2, jb3; 
  JTextArea JTA = null; 
 
  JScrollPane JScrollPane; 
    Public JTextAreaDemo3 () {JF = new JFrame ("JTextArea case 3"); 
    Container ContentPane = Jf.getcontentpane (); 
 
    Contentpane.setlayout (New BorderLayout ()); 
    JTA = new JTextArea (10, 15); 
    Jta.settabsize (4); 
    Jta.setfont (New Font ("superscript italics", Font.Bold, 16)); 
 
    Jta.setlinewrap (TRUE);//Activate the wrapping function Jta.setwrapstyleword (true);//Activate the line break Word function jta.setbackground (color.pink); 
    JScrollPane = new JScrollPane (JTA); 
    JPanel = new JPanel (); 
 
    Jpanel.setlayout (New GridLayout (1, 3)); 
    JB1 = new JButton ("copy"); 
    Jb1.addactionlistener (this); 
    JB2 = new JButton ("paste"); 
    Jb2.addactionlistener (this); 
    JB3 = new JButton ("cut"); 
 
    Jb3.addactionlistener (this); 
   Jpanel.add (JB1); Jpanel.add (JB2); 
 
    Jpanel.add (JB3); 
    Contentpane.add (JScrollPane, Borderlayout.center); 
 
    Contentpane.add (JPanel, Borderlayout.south); 
    Jf.setsize (400, 300); 
    Jf.setlocation (400, 200); 
 
    Jf.setvisible (TRUE); 
      Jf.addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent e) {system.exit (0); 
  } 
    }); ///Overwrite interface ActionListener method actionperformed public void actionperformed (ActionEvent e) {if (E.getsource () = j 
    B1) {jta.copy (); 
    else if (e.getsource () = = jb2) {jta.paste (); 
    else if (e.getsource () = = jb3) {jta.cut (); 
  } public static void Main (string[] args) {new JTextAreaDemo3 (); 
 } 
}

The above is the entire content of this article, I hope to help you learn.

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.