In: Computer Science
URGENT!!!!!!!!!!!!!!!!!!!
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.
// DoggyButton.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DoggyButton extends JFrame {
public DoggyButton() {
super("Doggy Button"); // pass
the GUI window title
setDefaultCloseOperation(EXIT_ON_CLOSE); // close button will exit
the operation
// creating a panel
JPanel panel = new JPanel();
// creating the button
JButton doggy = new
JButton("Doggy");
// add the button to the
panel
panel.add(doggy);
// add action listener to the
button
doggy.addActionListener(new
ActionListener() {
@Override
public void
actionPerformed(ActionEvent e) {
System.out.println("Bow Bow"); // when clicked,
it displays Bow Bow on console
}
});
// add button to the panel
add(panel);
// using compact size
pack();
}
public static void main(String[] args) {
// create the GUI
DoggyButton btn = new
DoggyButton();
btn.setVisible(true); // set the
frame visible
}
}
//end of DoggyButton.java
Output: