Change Font, Cut, Copy, paste using Applet in java

Using applet in Java we can create Jbuttons like change font, cut, copy, paste. Here we will learn how to create those buttons in Java using Applet.

In this Tutorial, we learn how to  perform Following Operations using Applet in java

  1. Changes in Font
  2. The cut from the Text area
  3. Copy from the text area
  4. Paste on the Text area

I know all of you are excited about this activity, so let’s start.

How to create JButton in Java using Applet

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class FontTest implements ActionListener
{
JFrame fr;

JFrame is a  Class to create a Frame in java and else are classes that are mandatory for the perform giving operation.

JButton b1,b2,b3,b4;

By the use of JButton, we can Create the Button on the frame.

JTextField tf1;

By the use of JTextField, we can Create a Text Field on the frame.

Java program:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class FontTest implements ActionListener
{
JFrame fr;
JButton b1,b2,b3,b4;
JTextField tf1;

FontTest()
{
fr=new JFrame();
fr.setLayout(null);
fr.setSize(600,400);

b1=new JButton("font");
b1.setBounds(100,50,70,30);
fr.add(b1);
b2=new JButton("cut");
b2.setBounds(190,50,70,30);
fr.add(b2);
b3=new JButton("copy");
b3.setBounds(280,50,70,30);
fr.add(b3);
b4=new JButton("paste");
b4.setBounds(370,50,70,30);
fr.add(b4);

tf1=new JTextField();
tf1.setBounds(100,100,400,300);
fr.add(tf1);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
fr.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
Font f=new Font("jokerman",Font.BOLD,25);
tf1.setFont(f);
}
if(e.getSource()==b2)
tf1.cut();
if(e.getSource()==b3)
tf1.copy();
if(e.getSource()==b4)
tf1.paste();
}

public static void main(String args[])
{
new FontTest();
}
}

In this code, we use action Listener for performing the event after pressing the button and we already define a Font which is used after clicking the Font Button.

I hope All of you understand the logic behind this program.

Output:

jbutton in Java applet

output

Also learn,

Leave a Reply

Your email address will not be published. Required fields are marked *