Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A graphic framework In this part, we will create an executable program that disp

ID: 3764004 • Letter: A

Question

A graphic framework In this part, we will create an executable program that displays the button on the screen. To make Dett we must create a framework that button can be found in, and then create a button and put in the frame.

Question 1 Write a framework that creates a JFrame and a myButton. Use Add to add the button to the frame, pack to place it, and setVisible to make it visible. If you run the program, this button should appear in a separate window.

Question 2 What really makes the pack and setVisible? What happens if you do not call these methods?

Question 3 You can set the behavior you want the program to have when you close the frame window, for example by calling the setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE). Response of the program if you do not make this call? What about the call?
How to solve 1,.2, and 3 in Java

Explanation / Answer

1.

package pckg;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.border.*;

import javax.swing.text.*;

public class SwingMain

{

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                createAndShowGUI();

            }

        });

    }//main

    public static void createAndShowGUI() {

        // Swing test

        setSystemLookFeel();

      JFrame frame = new JFrame("JFrame is me");

        frame.setBounds(0, 0, 400, 300);

        frame.addWindowListener(new java.awt.event.WindowAdapter()

        {

            public void windowClosing(java.awt.event.WindowEvent ev)

            { System.exit(0); }

        }

        );

        JPanel jpan = new JPanel();

        frame.getContentPane().add(jpan);

        JButton jbut = new JButton("Hello");

        jpan.add(jbut);

        frame.setVisible(true);

    } //createAndShowGUI

   

    // Setting OS-specific look and feel:

    private static void setSystemLookFeel()

    {

        // Force GUI to come up in the OS-specific look and feel

        String laf = UIManager.getSystemLookAndFeelClassName();

        try

        {

            UIManager.setLookAndFeel(laf);

        }

        catch (UnsupportedLookAndFeelException exc)

        {

            System.err.println("Unsupported: " + laf);

        }

        catch (Exception exc)

        {

            System.err.println("Error loading " + laf);

        }

    }//setSystemLookFeel

} //SwingMain

2.

You should only create one JFrame per Swing GUI. Get rid of the extends JFrame (and the code that goes with it), as it is not good practice to extend JFrame in Swing. Thus of course you won’t be able to call setVisible(true) in your constructor which is fine either call it after creating the GUI in the constructor or make a method like setVisible in GUI class which will be a wrapper for your JFrames setVisble(boolean b) method.

3.

import java.awt.*;

import javax.swing.*;

public class TestSize {

   public static void main(String[] args) {

      JFrame frame = new JFrame("Display Area");

      Container cp = frame.getContentPane();

      cp.setLayout(new FlowLayout());

      JButton btnHello = new JButton("Hello");

      btnHello.setPreferredSize(new Dimension(100, 80));

      cp.add(btnHello);

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      frame.setSize(300, 150); // or pack() the components

      frame.setLocationRelativeTo(null); // center the application window

      frame.setVisible(true);             // show it

       System.out.println(btnHello.getSize());

      System.out.println(btnHello.getLocation());

      System.out.println(btnHello.getLocationOnScreen());

       System.out.println(cp.getSize());

      System.out.println(cp.getLocation());

      System.out.println(cp.getLocationOnScreen());

       System.out.println(frame.getSize());

      System.out.println(frame.getLocation());

      System.out.println(frame.getLocationOnScreen());

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote