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

Problem Pick one or more Swing GUI components ( JLabel , JTextField , JCheckBox

ID: 3700880 • Letter: P

Question

Problem

Pick one or more Swing GUI components (JLabel, JTextField, JCheckBox, JComboBox, JList, JPanel) and treat them the same way Dr. Smith treated the JButton class.

Please take screenshot once compiled.

//---------------------------------------------------------

// Dr. Smith

// Problem3.java

//---------------------------------------------------------

import javax.swing.*;

public class Problem3

{

   public static void main(String[] args)

   {

      GUI gui = new GUI();

      gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      gui.setSize(800,600);

      gui.setVisible(true);

   }

}

//---------------------------------------------------------

// Dr. Smith

// GUI.java

//---------------------------------------------------------

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

//---------------------------------------------------------

public class GUI extends JFrame

//---------------------------------------------------------

{

   private JButton R1C1;

   private JButton R1C2;

   private JButton R2C1;

   private JButton R2C2;

   private Font font = new Font("Courier",Font.PLAIN+Font.BOLD,20);

   //----------------------------------------------------

   public GUI()

   //----------------------------------------------------

   {

      super("Chapter #14, Problem #3");

      setLayout( new GridLayout(2,2,5,5) );

      Icon myIcon1 = new ImageIcon(getClass().getResource("MyIcon1.png"));

     

      R1C1 = new JButton("(1,1)");

      R1C1.setFont(font);

      R1C1.setForeground(Color.WHITE);

      R1C1.setBackground(Color.BLUE);

      R1C1.setRolloverIcon(myIcon1);

      add(R1C1);

      R1C2 = new JButton("(1,2)");

      R1C2.setFont(font);

      R1C2.setForeground(Color.WHITE);

      R1C2.setBackground(Color.CYAN);

      add(R1C2);

      R2C1 = new JButton("(2,1)");

      R2C1.setFont(font);

      R2C1.setForeground(Color.WHITE);

      R2C1.setBackground(Color.GREEN);

      add(R2C1);

      R2C2 = new JButton("(2,2)");

      R2C2.setFont(font);

      R2C2.setForeground(Color.WHITE);

      R2C2.setBackground(Color.MAGENTA);

      add(R2C2);

      ButtonHandler handler = new ButtonHandler();

      R1C1.addActionListener(handler);

      R1C2.addActionListener(handler);

      R2C1.addActionListener(handler);

      R2C2.addActionListener(handler);

   }

   //----------------------------------------------------

   private class ButtonHandler implements ActionListener

   //----------------------------------------------------

   {

      //----------------------------------------------------

      @Override

      public void actionPerformed(ActionEvent event)

      //----------------------------------------------------

      {

         Icon myIcon2 = new ImageIcon(getClass().getResource("MyIcon2.png"));

         if      ( event.getSource() == R1C1 )

         {

            if ( R1C1.getIcon() == null )

               R1C1.setIcon(myIcon2);

            else

               R1C1.setIcon(null);

         }

         else if ( event.getSource() == R1C2 )

         {

            if ( R1C2.getIcon() == null )

               R1C2.setIcon(myIcon2);

            else

               R1C2.setIcon(null);

         }

         else if ( event.getSource() == R2C1 )

         {

            if ( R2C1.getIcon() == null )

               R2C1.setIcon(myIcon2);

            else

               R2C1.setIcon(null);

         }

         else if ( event.getSource() == R2C2 )

         {

            if ( R2C2.getIcon() == null )

               R2C2.setIcon(myIcon2);

            else

               R2C2.setIcon(null);

         }

      }

   }

}

MyIcon.png

MyIcon1.png

MyIcon2.png

Explanation / Answer

//Start of Problem3.java

import javax.swing.*;

public class Problem3

{

public static void main(String[] args)

{

GUI gui = new GUI();

gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

gui.setSize(800,600);

gui.setVisible(true);

}

}
//==================== End of Problem3.java =================

/*

using JLabel repeated the process like in the question

Whenever label is clicked an image is shown and again clicking on that label will restore previous state

THIS IS DONE USING MOUSELISTENER

adding mouselistener to the label

SYNTAX:

label.addMouseListener(this);

class must implements "MouseListener"

other abstract methods to implement

  

public void mouseClicked(MouseEvent event)

{

//changes label icon

}

public void mouseExited(MouseEvent event)

{

}

public void mouseEntered(MouseEvent event)

{

}

public void mouseReleased(MouseEvent event)

{

}

public void mousePressed(MouseEvent event)

{

}

*/

//======== Start of GUI.JAVA ========================

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

//---------------------------------------------------------

public class GUI extends JFrame implements MouseListener

//---------------------------------------------------------

{

private JLabel R1C1;

private JLabel R1C2;

private JLabel R2C1;

private JLabel R2C2;

private Font font = new Font("Courier",Font.PLAIN+Font.BOLD,20);

//----------------------------------------------------

public GUI()

//----------------------------------------------------

{

super("Chapter #14, Problem #3");

setLayout( new GridLayout(2,2,5,5) );

Icon myIcon1 = new ImageIcon(getClass().getResource("MyIcon1.png"));

R1C1 = new JLabel("(1,1)");

R1C1.setFont(font);

R1C1.setForeground(Color.WHITE);

R1C1.setBackground(Color.BLUE);

R1C1.setOpaque(true);

add(R1C1);

R1C2 = new JLabel("(1,2)");

R1C2.setFont(font);

R1C2.setForeground(Color.WHITE);

R1C2.setBackground(Color.CYAN);

R1C2.setOpaque(true);

add(R1C2);

R2C1 = new JLabel("(2,1)");

R2C1.setFont(font);

R2C1.setForeground(Color.WHITE);

R2C1.setBackground(Color.GREEN);

R2C1.setOpaque(true);

add(R2C1);

R2C2 = new JLabel("(2,2)");

R2C2.setFont(font);

R2C2.setForeground(Color.WHITE);

R2C2.setBackground(Color.MAGENTA);

R2C2.setOpaque(true);

add(R2C2);

R1C1.addMouseListener(this);

R1C2.addMouseListener(this);

R2C1.addMouseListener(this);

R2C2.addMouseListener(this);

}

public void mouseClicked(MouseEvent event)

{

Icon myIcon2 = new ImageIcon(getClass().getResource("MyIcon2.png"));

if( event.getSource() == R1C1 )

{

if ( R1C1.getIcon() == null )

R1C1.setIcon(myIcon2);

else

R1C1.setIcon(null);

}

else if ( event.getSource() == R1C2 )

{

if ( R1C2.getIcon() == null )

R1C2.setIcon(myIcon2);

else

R1C2.setIcon(null);

}

else if ( event.getSource() == R2C1 )

{

if ( R2C1.getIcon() == null )

R2C1.setIcon(myIcon2);

else

R2C1.setIcon(null);

}

else if ( event.getSource() == R2C2 )

{

if ( R2C2.getIcon() == null )

R2C2.setIcon(myIcon2);

else

R2C2.setIcon(null);

}

}

public void mouseExited(MouseEvent event)

{

}

public void mouseEntered(MouseEvent event)

{

}

public void mouseReleased(MouseEvent event)

{

}

public void mousePressed(MouseEvent event)

{

}

}

//==================== END OF GUI.JAVA =====================

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