Question
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
MyIcon1.png
MyIcon2.png
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