Document the source code with each question his code attempts answer, but do not
ID: 3700897 • Letter: D
Question
Document the source code with each question his code attempts answer, but do not answer the questions.
Now, do research about the JButton class to learn how to ask-then-answer at least five more questions about JButton. Document the code you add to GUI.java with the questions you asked and allow your code to be the answer to each of your questions.
List:
How do I use GridLayout?
How do I instantiate a JButton?
How do I define a Font and then use the font for the text that labels a button?
How many different uses do buttons have for an ImageIcon?
How do I dynamically change a button icon attribute?
How many different uses do buttons have for Color?
How do I use the same event handler for more than one button?
//---------------------------------------------------------
// Chapter #14, Problem #3
// 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);
}
}
//---------------------------------------------------------
// Class GUI for Chapter #14, Problem #3
// 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);
}
}
}
}
Program GUI Screenshot Chapter #14, Problem #3 (2,2)Explanation / Answer
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
public class GUI extends JFrame{
private JButton R1C1;
private JButton R1C2;
private JButton R2C1;
private JButton R2C2;
//How do I define a Font
/*
* Fonts are defind in java using Font class. Font can be created using no argument constructor
* then Font tryoe size and other parameters can be set. Or it can be created using the argument
* constructor like below which takes Font type and font decoration like BOLD, UNDERLINE and its size
*
*/
private Font font = new Font("Courier", Font.PLAIN + Font.BOLD, 20);
public GUI(){
super("Chapter #14, Problem #3");
//How do I use GridLayout?
/* GridLayout(): Creates a grid with one column per component in a row default
* Optionally GridLayout(int rows,int columns) takes
* two parameter which defines how many rows and columns the Grid contain
* Another constructor to GridLayout takes four parameter. GridLayout(int rows, int columns, int hgap, int vgap)
* creates a grid with the given rows and columns alongwith given horizontal and vertical gaps.
*/
setLayout(new GridLayout(2, 2, 5, 5));
Icon myIcon1 = new ImageIcon(getClass().getResource("MyIcon1.png"));
//How do I instantiate a JButton?
/*
* One JButton is created like JButton R1C! = null;
* new operator should be used to instantiate the JButton like below. JButton object
* can be created using no argument onstructor JBUtton() or with one argument
* JButton("Some TExt to display")
*/
R1C1 = new JButton ("(1,1)");
// How to use the font for the text that labels a button
/*
* Once Fonts are defind using Font class then using the setFont method on any JComponent
* like JButton or JLable to set the Font by passig the created Font object
*/
R1C1.setFont(font);
//How many different uses do buttons have for Color?
/*
* Color can be used with Jbutton directly with two methods
* setBackground which sets background color of the button
* setForeground which sets the color of the text displayed on the button
* Addionally Color can be used along with the Border object which can be used to
* set the border of the button
*/
R1C1.setForeground(Color.WHITE);
R1C1.setBackground(Color.BLUE);
R1C1.setRolloverIcon(myIcon1);
//R1C1.setOpaque(true);
// How to add button to layout
/*
* Once JBtutton object created with all parameters
* we have to use the add() method which would add the component in this case button
* passed to it with its parent i.e. JFrame in this case
*/
add(R1C1);
R1C2 = new JButton ("(1,2)");
R1C2.setFont(font);
R1C2.setForeground(Color.WHITE);
R1C2.setBackground(Color.CYAN);
//R1C2.setOpaque(true);
add(R1C2);
R2C1 = new JButton ("(2,1)");
R2C1.setFont(font);
R2C1.setForeground(Color.WHITE);
R2C1.setBackground(Color.GREEN);
//R2C1.setOpaque(true);
add(R2C1);
R2C2 = new JButton ("(2,2)");
R2C2.setFont(font);
R2C2.setForeground(Color.WHITE);
R2C2.setBackground(Color.MAGENTA);
//R2C2.setOpaque(true);
add(R2C2);
ButtonHandler handler = new ButtonHandler();
R1C1.addActionListener(handler);
R1C2.addActionListener(handler);
R2C1.addActionListener(handler);
R2C2.addActionListener(handler);
}
//How do I use the same event handler for more than one button?
/*
* To use the same Event Hanler with different button we needed to create a class by extending the
* ActionLister class. This provides abstract methods like actionPerfomred which would captures the event
* Once Event handler class is created using the addActionLister method event is ties to the
* button
* ButtonHandler handler = new ButtonHandler();
* R1C1.addActionListener(handler);
* This is done with all the button which we want to handle with single handler
* Once event tied with the button actionPerfomed class takes a parater ActionEvent
* which has a property eventSource. This can be accessed using the getSource methos
* which returns the object i.e. from which button triggered the event
* Then checking the output of the getSource againt the JButton object event can be handled
*
*/
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)
//How do I dynamically change a button icon attribute?
/*
* To change the icon of the Button first needed to create ImageIcon using the ImgageIcon class
* Then based on any event like button click window open, mouse entered, mouse left
* icon of the JButton can be changed using setIcon () method by passing the Icon object created
*
*/
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 static void main(String[] args){
GUI gui = new GUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(800, 600);
gui.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.