Question
Java Overloaded constructor that will take the title as a parameter Instructions: Create a second constructor called GUIApp that takes string for the title, use
Java Overloaded constructor that will take the title as a parameter
Instructions:
Create a second constructor called GUIApp that takes string for the title, use that to set title for GUI app
Create Overloaded constructor that will take the title as a parameter
**********Orignal Instructor Code which still needs instructions to be added to:********
//Imports import javax.swing.*; import java.awt.*; import java.util.Scanner; import java.util.Random;
public class GUIApp extends JFrame { public GUIApp() { //Output Window formatting this.setVisible(true); this.setSize(300,400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("GUI App"); } //main method public static void main(String[] args) { GUIApp gui = new GUIApp(); } }
My Code Attempt 1 wrong:
//Imports import javax.swing.*; import java.awt.*; import java.util.Scanner; import java.util.Random;
public class GUIApp extends JFrame { //Content Pane Container content = this.getContentPane(); JLabel lbl = new JLabel ("Hello"); public GUIApp() { //3 rows one column content.setLayout(new GridLayout (3,1)); content.add(lbl); //J Labels content.add(new JLabel("Aisha")); content.add(new JLabel("Perwez")); content.add(new JLabel("July 19, 2018")); //Output Window formatting this.setVisible(true); this.setSize(300,400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle(""); } //main method public static void main(String[] args) { GUIApp gui = new GUIApp(); } }
My Code Attempt 2 wrong:
//Imports
import javax.swing.JFrame;
public class GUIApp extends JFrame
{
//Property
private String title;
//Default constructor to initialize
public GUIApp()
{
title = " ";
setTitle(title);
}
//Parameter second constructor takes in title
public GUIApp(String title)
{
this.title = title;
setTitle(title);
}
//Main method
public static void main(String[] args)
{
//Create an instance of GUIApp with title
GUIApp gui = new GUIApp("Titlessss");
//set size
gui.setSize(300, 400);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Visible
gui.setVisible(true);
}
}
Both of these codes above are wrong
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started