Pregled JTextArea v Javi

Ko govorimo o Javi in ​​swing paketu, je JTextArea nekaj, kar pride v sliko. Uporablja se za prikaz večvrstičnega prikaza območja, ki vsebuje besedilo in se lahko uporablja tudi za urejanje besedila. Uporablja se za podedovanje komponente komponent Java. Pisava besedila je lahko nastavljena na različne velikosti in barve in jo lahko celo dodate skupaj z novim naborom besedila. V bistvu lahko vse to besedilno območje prilagodimo glede na zahteve in potrebe uporabnikov. Je lahka komponenta, ki se uporablja za zagotavljanje združljivosti virov skupaj z razredom java.awt.TextArea, kjer je združljivost enostavno preslikati.

Konstruktorji JTextArea na Javi

Preučimo več o različnih vrstah konstruktorjev:

  • JTextArea (): Uporablja se za oblikovanje novega praznega besedilnega območja.
  • JTextArea (vrstica int, stolpec int): Ta JTextArea je podobna tistemu neparametriziranega JTextArea s to razliko, da uporablja parametre vrstic in stolpcev. Uporablja se za oblikovanje novega območja, ki temelji na besedilnem polju, skupaj s fiksnim številom vrstic in stolpcev.
  • JTextArea (String s): Uporablja se za oblikovanje novega območja, ki temelji na besedilu, skupaj z dano začetno besedilo.
  • JTextArea (String s, int vrstica, stolpec int): Ta je veliko bolj podoben tistim, kot so vrednosti niza ali tiste, ki vsebujejo parametrizirane vrednosti vrstic in stolpcev, zato to tvori dano začetno besedilo in določeno število vrstic in vrednosti stolpcev .

Metode JTextArea v Javi

Po branju o različnih konstruktorjih, povezanih z JTextArea, preberite tudi nekaj glavnih metod, ki so osnova JTextArea na Javi.

  • Doda (nizov): Kot že ime pove, se ta metoda uporablja za dodajanje enega danega niza z besedilom besedilnega področja.
  • setFont (Font f): Ta metoda se uporablja za določanje velikosti in vrste besedila področja besedila na dano pisavo.
  • getLineCount (): Ta funkcija se uporablja za pridobivanje števila vrstic v besedilnem polju območja z besedilom.
  • setColumns (int c): Uporablja se za nastavitev številke stolpca območja z besedilom skupaj z danim celim številom.
  • setRows (int r): Ta funkcija se uporablja za nastavitev vrstic območja z besedilom in dano celo število.
  • getColumn (): Ta funkcija se uporablja za pridobivanje števila stolpcev skupaj s poljem besedilnega polja.
  • getRows (): Ta funkcija se uporablja za pridobivanje števila vrstic določenega besedilnega območja.

Primeri JTextArea

Preučimo JTextArea s pomočjo osnovnega primera, ki razloži, kako nastane polje JTextArea.

Primer # 1

Programirajte v Javi, da ustvarite vzorčno polje JTextArea.

Koda:

//importing basic packages and relevant classes
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;
class txt extends JFrame implements ActionListener (
// creating JFrame with frame f
static JFrame F1;
// creating JButton with button b
static JButton B1, B2, B4, B3;
// creating label to display text label l
static JLabel L1, L2;
// creating a public text area to create a text area jjt
static JTextArea jjt;
// creating a default constructor
txt()
(
)
// creating main class for compiler entry
public static void main(String args()) throws IOException
(
// creation of a frame for storing text field option and a Jbutton field
F1 = new JFrame("Entered Text Field");
// creation of a new label for storing display text field option
L1 = new JLabel("Not entered anything");
// creation of a new button for submit
B1 = new JButton("submit button");
// creation of an object for text class
txt te = new txt();
// adding an addActionListener event to button
B1.addActionListener(te);
// creation of a new text area. Also specify rows and column numbers
jjt = new JTextArea(100, 100);
//creation of a new JPanel
JPanel P1 = new JPanel();
// addition of the text area and the button to the new panel added
P1.add(jjt);
F1.add(P1);
P1.add(L1);
P1.add(B1);
// setting the frame size in squared form
F1.setSize(100, 100);
//to display
F1.show();
)
// Whenever the button is pressed for actionPerformed
public void actionPerformed(ActionEvent e)
(
//implementation of getActionCommand
String s1 = e.getActionCommand();
if (s1.equalsIgnoreCase("submit button")) (
// setting of label text to field text
L1.setText(jjt.getText());
)
)
)

Izhod:

Primer # 2

V tem primeru bomo videli javno besedilo, ki bo prikazano z uporabo JTextArea.

Koda:

//importing basic packages and relevant classes
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;//defining a class TxtEG
class TxtEG
(
//calling a default constructor after creation
TxtEG()(
//creation of frames with the object f
JFrame f= new JFrame();
// creation of a JTextArea
JTextArea area=new JTextArea("Welcome");
//setting boundary
area.setBounds(10, 30, 200, 200);
//adding properties to the frame
f.setVisible(true);
f.setSize(300, 300);
f.add(area);
f.setLayout(null);
)
//declaring method main
public static void main(String args()) throws IOException
(
new TxtEG();
))

Izhod:

Primer # 3

V tem primeru bomo brali o Java JTextArea skupaj s polji in dogodki poslušalca akcije.

Koda:

//importing basic packages and relevant classes
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
//defining the class which is implementing the interface ActionListener
class TarEG implements ActionListener(
//Defining labels, button and Jtext area
JLabel l1, l2;
JTextArea area;
JButton b;
//defining the default contructor
TarEG() (
//Defining JFrames, Labels and set other important properties
JFrame f= new JFrame()
l1=new JLabel();
//Setting bounds
l1.setBounds(50, 25, 100, 30);
//Setting labels
l2=new JLabel();
l2.setBounds(160, 25, 100, 30);
//Defining new
area=new JTextArea();
//Setting different bounds
area.setBounds(20, 75, 250, 200);
b=new JButton("Word Count");
b.setBounds(100, 300, 120, 30);
//Adding action listeners and putting the value to this to point to current object value
b.addActionListener(this);
//adding properties
f.setVisible(true);
f.setLayout(null);
f.add(l1);f.add(area);f.add(b);f.add(l2);
f.setSize(450, 450);
)
//declaring the actionPerformed event
public void actionPerformed(ActionEvent e)(
//declaring string text area
String text=area.getText();
//splitting words based on the string value
String words()=text.split("\\s");
//calculating the length of words
l1.setText("Words: "+words.length);
//setting the character length
l2.setText("Characters: "+text.length());
)
//declaring the main function
public static void main(String() args) (
new TarEG();
)
)

Izhod:

Zaključek

V tej vadnici smo preučili najpomembnejšo sestavino Java Swings, tj. JTextArea, ki je zelo pomembna pri oblikovanju oken in besedilnih področij. Upam, da vam je bil ta članek všeč. Če želite razširiti svojo podporo tako, da sledite našemu blogu za več takšnih člankov.

Priporočeni članki

To je vodnik za JTextArea na Javi. Tukaj razpravljamo o različnih vrstah konstruktorjev, metodah JTextArea in java skupaj s primeri in implementacijo kode. Če želite izvedeti več, si oglejte tudi naslednje članke -

  1. JScrollPane v Javi
  2. Postavitev v Javi
  3. JTextField v Javi
  4. Java Tools Deployment Tools
  5. Konstruktor v Javi
  6. JFrame v Javi
  7. JLabel v Javi