Write a program whose source file is named UnitProgljava tha

Write a program whose source file is named \"UnitProgl.java\" that does the following: Creates a frame with a GridLayout Manager Creates two panels and adds them to the frame, stacked one over the other. In each panel creates three buttons, side by side. The buttons should be labeled \"Button 1\", \"Button 2\", \"Button 3\", \"Button 4\", \"Button 5\" and \"Button 6\". Displays this GUI when run. The only buttons that actually works will be the buttons on the window title bar: minimize, maximize and the exit button in the upper right hand corner of the programs window. Part 2 - Event Driven Programming Write a program whose source file is named \"UnitProg2.java\". Do the following: Start by copying the program code from part 1 and modifying it. This program will have a GUI that looks just like the previous one. The goal is to make the JButtons work. Add code to handle the events when the user presses any of the six buttons on the GUI. When the user presses a button simply write out to the console \"The user pressed Button X\", where \"X\" is the number of the button. For example, when \"Button 2\" is pressed the message written to the console should be \"The user pressed Button 2.\".

Solution

import java.awt.*;

import java.awt.event.*;

public class AwtLayoutDemo {

   private Frame mainFrame;

   private Label headerLabel;

   private Label statusLabel;

   private Panel controlPanel;

   private Label msglabel;

   public AwtLayoutDemo(){

      prepareGUI();

   }

   public static void main(String[] args){

      AwtLayoutDemo awtLayoutDemo = new AwtLayoutDemo();

      awtLayoutDemo.showGridLayoutDemo();      

   }

     

   private void prepareGUI(){

      mainFrame = new Frame(\"PROGRAM FOR GRID CHEGG\");

      mainFrame.setSize(400,400);

      mainFrame.setLayout(new GridLayout(3, 1));

      mainFrame.addWindowListener(new WindowAdapter() {

         public void windowClosing(WindowEvent windowEvent){

            System.exit(0);

         }       

      });   

      headerLabel = new Label();

      headerLabel.setAlignment(Label.CENTER);

      statusLabel = new Label();       

      statusLabel.setAlignment(Label.CENTER);

      statusLabel.setSize(350,100);

      msglabel = new Label();

      msglabel.setAlignment(Label.CENTER);

      msglabel.setText(\"Welcome to CHEGG\");

      controlPanel = new Panel();

      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);

      mainFrame.add(controlPanel);

      mainFrame.add(statusLabel);

      mainFrame.setVisible(true);

   }

   private void showGridLayoutDemo(){

      headerLabel.setText(\"Layout in action: GridLayout\");     

      Panel panel = new Panel();

      panel.setBackground(Color.darkGray);

      panel.setSize(300,300);

      GridLayout layout = new GridLayout(0,3);

      layout.setHgap(10);

      layout.setVgap(10);

     

      panel.setLayout(layout);       

      panel.add(new Button(\"Button 1\"));

      panel.add(new Button(\"Button 2\"));

      panel.add(new Button(\"Button 3\"));

      panel.add(new Button(\"Button 4\"));

      panel.add(new Button(\"Button 5\"));

      controlPanel.add(panel);

      mainFrame.setVisible(true);

   }

}

 Write a program whose source file is named \
 Write a program whose source file is named \
 Write a program whose source file is named \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site