Lab Button CSIS-1410 In Eclipse create a new package called ImageButton Download
ID: 3593854 • Letter: L
Question
Lab Button CSIS-1410 In Eclipse create a new package called ImageButton Download the images Image1, Image2 and Image3 from Canvas and import them to the package ImageButton (right-click ImageButton, import.) Notice the different file extensions. Create two public classes: ImageButton and ImageButtonApp ImageButton derives from JFrame In ImageButton do the following:· Create two final fields: imgButton of type JButton clickImage of type Icon In the constructor do the following: o Set the title to Lab Button Create two local variables of type lcon: imagel and image2 Initialize them with a new ImageIcon base on Image1 and Image2 like this: Icon image1 = new Imagelcon(getClass().getResource("Inagel.png")); Initialize the field clickImage with a new ImageIcon that is based on Image3 o o o o o Initialize the field imgButton with a new JButton that accepts image1 as the only argument Call the method setRolloverIcon on imgButton and pass image2 as roll-over icon Add the imgButton to this (ImageButton, which is a JFrame) In ImageButtonApp do the following: Crate an instance of ImageButton Make sure that the ImageButton (which is a JFrame) terminates when the user clicks the x-Button to exit Hint: call setDefaultCloseOperation Set the size to 660 x 660 Set visibility to true Compile/ Run.. you should see an image that changes every time you roll over the mouse Back in ImageButton do the following: · Outside the constructor but still inside class ImageButton create a private class called ButtonclickEventHandler. It implements the interface ActionListener (soon we'll look at an alternative way using an anonymous inner class) Eclipse can help with the import statement. It can also create a method stub for the method actionPerformed. In actionPerformed replace the TODO comment with the following code o Disable rollorver on imgButton Hint: use the method setRolloverEnabled and pass false o Set the image to Image3 Hint: use the method setIcon and pass the field clickImage At the end of the constructor body do the following Create an instance of ButtonClickEventHandler and name it buttonEventHandler Associate the buttonEventHandler with the imgButton by calling the method addActionListener and by passing buttonEventHandler as an argument. Compile/ Run.. when you click the button the image changes and mouse- rollover no longer worksExplanation / Answer
package ImageButton;
import javax.swing.JFrame;
public class ImageButtonApp {
public static void main(String args[])
{
ImageButton window = new ImageButton();
window.setSize(600, 480);
window.setLocation(100,100);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
---------------------
package ImageButton;
import javax.swing.*;
public class ImageButton extends JFrame {
JButton imgButton;
Icon clickImage;
ImageButton()
{
this.setTitle("Lab Button");
Icon img1=new ImageIcon(getClass().getResource("Image1.png"));
Icon img2=new ImageIcon(getClass().getResource("Image2.png"));
imgButton=new JButton(img1);
imgButton.setRolloverIcon(img2);
this.add(imgButton);
}
}
------------------------
package ImageButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ImageButton extends JFrame {
JButton imgButton;
Icon clickImage;
ImageButton()
{
this.setTitle("Lab Button");
Icon img1=new ImageIcon(getClass().getResource("Image1.png"));
Icon img2=new ImageIcon(getClass().getResource("Image2.png"));
Icon clickImage=new ImageIcon(getClass().getResource("Image2.png"));
imgButton=new JButton(img1);
imgButton.setRolloverIcon(img2);
ButtonClickEventHandler buttonEventHandler=new ButtonClickEventHandler();
imgButton.addActionListener(buttonEventHandler);
this.add(imgButton);
}
private class ButtonClickEventHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
imgButton.setRolloverEnabled(false);
imgButton.setIcon(clickImage);
}
}
}
-------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.