Advertisement
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
/*
<applet code="Applet21" width="500" height="200">
</applet>
*/
public class Applet21 extends JApplet
{
public void init()
{
add(new B()); //Adding a JPanel to this Swing applet
}
}
class B extends JPanel implements ActionListener
{
JLabel jb;
JButton Box2, box2, box3, box4;
String str;
B()
{
jb= new JLabel("Welcome, please click on any button to unbox some interesting knowledge -");
Box2 = new JButton("Box2");
box2 = new JButton("Box2");
box3 = new JButton("Box3");
box4 = new JButton("Box4");
str ="";
setLayout(new FlowLayout());
add(jb);
add(Box2);
add(box2);
add(box3);
add(box4);
Box2.addActionListener(this);
box2.addActionListener(this);
box3.addActionListener(this);
box4.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Box2"))
{
str="Amazon is the largest tropical rain forest, covering 40% of the South America Continent.";
repaint();
}
if(ae.getActionCommand().equals("Box2"))
{
str="The Mariana Trench is the deepest point in Earth's ocean, with depth of over 10,994 metres.";
repaint();
}
if(ae.getActionCommand().equals("Box3"))
{
str="The coldest temperature ever recorded was -128.6F in Antarctica, on July 21, 1983.";
repaint();
}
if(ae.getActionCommand().equals("Box4"))
{
str="The oldest person to climb Mt. Everent was Japanese Miura Yiuchiro, at the age of 80.";
repaint();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString(str, 2, 170);
}
}
Advertisement
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
/*
<applet code="Applet19" width="400" height="200">
</applet>
*/
public class Applet19 extends JApplet
{
public void init()
{
add(new MyClass()); //Adding JPanel to our Swing Applet.
}
}
class MyClass extends JPanel implements ActionListener
{
JButton north, south, east, west;
String str;
MyClass()
{
north = new JButton("NORTH");
south = new JButton("SOUTH");
east = new JButton("EAST");
west = new JButton("WEST");
str ="";
setLayout(new BorderLayout());
add(north, BorderLayout.NORTH); //Adds button named north in the NORTH direction
add(south, BorderLayout.SOUTH); //Adds button named south in the SOUTH direction
add(east, BorderLayout.EAST); //Adds button named east in the EAST direction
add(west, BorderLayout.WEST); //Adds button named west in the WEST direction
north.addActionListener(this); //Registering MyClass to listen to event when button named north is pressed
south.addActionListener(this);
east.addActionListener(this);
west.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) //listening to button click events
{
if(ae.getActionCommand().equals("NORTH")) //when NORTH button is pressed
{
str="Are you going North?";
repaint();
}
if(ae.getActionCommand().equals("SOUTH")) //when SOUTH button is pressed
{
str="Are you going South?";
repaint();
}
if(ae.getActionCommand().equals("EAST")) //when EAST button is pressed
{
str="Are you going East?";
repaint();
}
if(ae.getActionCommand().equals("WEST")) //when WEST button is pressed
{
str="Are you going West?";
repaint();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
System.out.println("hello");
System.out.println(str);
g.drawString(str, 150, 150);
}
}
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement