Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem Pick one or more Swing GUI components ( JLabel , JTextField , JCheckBox , JComboBox , JList , JPanel ) and treat them the

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

image text in transcribed

MyIcon1.png

image text in transcribed

MyIcon2.png

image text in transcribed

image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 11 The Ultimate Database For Cloud Analytics And Mobile

Authors: John Campbell, Chris Crone, Gareth Jones, Surekha Parekh, Jay Yothers

1st Edition

1583474013, 978-1583474013

More Books

Students also viewed these Databases questions

Question

6. Explain the strengths of a dialectical approach.

Answered: 1 week ago

Question

2. Discuss the types of messages that are communicated nonverbally.

Answered: 1 week ago