c.Illustration of TextArea Component. Take input from user in TextField and add it to TextArea.

CODE:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class TextAreaDemo extends JFrame implements ActionListener

{

JLabel lblMsg;

JTextField txtMsg;

JTextArea txt;

JButton btnAdd;

JPanel p1,p2;

TextAreaDemo()

{

setTitle("JTextArea Demo");

   setSize(400, 400);

   setVisible(true);

   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

lblMsg=new JLabel("Enter Text Here");

txtMsg=new JTextField(20);

  txt=new JTextArea(10,30);

  btnAdd=new JButton("Add");

p1=new JPanel();

p2=new JPanel();

JScrollPane sp=new JScrollPane(txt);

p1.add(lblMsg);

p1.add(txtMsg);

p1.add(btnAdd);

p2.add(sp);

btnAdd.addActionListener(this);

getContentPane().add(p1,BorderLayout.CENTER);

getContentPane().add(p2, BorderLayout.SOUTH);

}

public void actionPerformed(ActionEvent ae){

if(ae.getSource()==btnAdd)

{

String str=txtMsg.getText();

txt.append(str+"\n");

txtMsg.setText("");

}

}

public static void main(String args[])

{

new TextAreaDemo();

}

}

 

OUTPUT:

Programs based on AWT and Swing.

Post a Comment

0 Comments