Create a GUI which uses a JFrame. The GUI has 5 JButtons in the order and locati
ID: 3764486 • Letter: C
Question
Create a GUI which uses a JFrame. The GUI has 5 JButtons in the order and location shown below. These buttons are placed inside a JPanel and placed in a BorderLayout. Set the border using the BorderFactory class. When a button is pushed, the Finch responds by executing the button’s label command. The distance for the Finch to travel forward or backwards is 6 –- 12 inches. Turning should be in the range of 45 – 90 degrees. The dance button has the Finch perform a short 7 step dance routine. Have the Finch’s beak change color for each button command. Beside is an example of the GUI.
Explanation / Answer
import java.awt.*;
import javax.swing.*;
class Example extends JFrame
{
JButton button1, button2, button3, button4, button5 ; // reference to the button object
public void createTheButtons()
{
JPanel buttonPanel = new JPanel();
button1 = new JButton("Enter Finch Action Label here");
button2 = new JButton("Enter Finch Action Label here");
button3 = new JButton("Enter Finch Action Label here");
button4 = new JButton("Enter Finch Action Label here");
button5 = new JButton("Enter Finch Action Label here");
buttonPanel.add( button1 );
buttonPanel.add( button2 );
buttonPanel.add( button3 );
buttonPanel.add( button4 );
buttonPanel.add( button5 );
Finch exampleFinch = new Finch();
button1.addActionListener(new ButtonListener(exampleFinch));
button2.addActionListener(new ButtonListener(exampleFinch));
button3.addActionListener(new ButtonListener(exampleFinch));
button4.addActionListener(new ButtonListener(exampleFinch));
button5.addActionListener(new ButtonListener(exampleFinch));
}
public class ButtonListener implements ActionListener {
Finch exampleFinch;
public FowardButtonListener(Finch exFinch) {
exampleFinch = exFinch;
}
public void actionPerformed (ActionEvent e) {
exampleFinch.setWheelVelocities(1234, 1234, 1234); //specify time value here
// void setWheelVelocities(int leftVelocity, int rightVelocity, int timeToHold)
}
}
public class ButtonDemo
{
public static void main ( String[] args )
{
Example obj = new Example();
obj.createTheButtons();
obj.setSize( 150, 75 );
obj.setVisible( true );
}
}