In: Computer Science
Write Java code which will create a GUI window on screen. The window has one button: Doggy. When the user selects Doggy, “Bow Bow” should be printed on screen. Justify your syntax.
provide solution fast as possible
If you use Java Applet for the above mentioned GUI program then the screenshot given below and also sharing the code with explanation in comment line
Code with explanation in comment line:
========================
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
// i am implementing ActionListener Interface for actionPerformed abstract method
public class JavaApplication42 extends Applet implements ActionListener
{
Button b1; //declaring variable
String msg="";
public void init()
{
b1=new Button("Doggy"); //defining button name Doggy
add(b1); // adding button in the GUI
b1.addActionListener(this); //defining action for the button click event
}
public void paint(Graphics g)
{
g.drawString(msg, 50, 50); //displaying the output bow bow
}
@Override
public void actionPerformed(ActionEvent e) {
String h=e.getActionCommand();
if(h.equals("Doggy"))
{
msg="Bow Bow";
}
repaint();
}
}
===================================================================
If you use Java Swing for GUI then you have to take jFrame and the drag one jbutton and one jLabel from the swing control then change the jbutton text property to “Doggy” and then double click on the button
And write the code :
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setText("Bow Bow"); //you have to write this line of code
}