Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program using a layout manager. You must write the program to meet the f

ID: 3756641 • Letter: W

Question

Write a program using a layout manager. You must write the program to meet the following criteria: 1. Create a window with a layout. 2. Add 6 buttons to the window, each with a number to identify it-such as Button Button2, Button3, Button4, Button5, Button6. When a button is clicked, it will create an event object. Create a listener object that will respond to the event object by displaying a message to the screen stating whichibutton was clicked 3. 4. Hint: Make a separate class to handle the event (clicking the button.

Explanation / Answer

// Make Sure Your Class name should be ButtonInfo

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JButton;

import java.awt.FlowLayout;

import javax.swing.JLabel;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class ButtonInfo {

private JFrame frame;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

ButtonInfo window = new ButtonInfo();

window.frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the application.

*/

public ButtonInfo() {

initialize();

}

/**

* Initialize the contents of the frame.

*/

private void initialize() {

frame = new JFrame();

frame.setBounds(100, 100, 450, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

JLabel resultshow = new JLabel("Result shown here");

resultshow.setBounds(64, 29, 232, 14);

frame.getContentPane().add(resultshow);

JButton btnNewButton = new JButton("Button1");

btnNewButton.setBounds(64, 98, 89, 23);

btnNewButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

resultshow.setText("");

resultshow.setText("Button1 Pressed");

}

});

frame.getContentPane().add(btnNewButton);

JButton btnNewButton_1 = new JButton("Button3");

btnNewButton_1.setBounds(64, 145, 89, 23);

btnNewButton_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

resultshow.setText("");

resultshow.setText("Button3 Pressed");

}

});

frame.getContentPane().add(btnNewButton_1);

JButton btnNewButton_2 = new JButton("Button5");

btnNewButton_2.setBounds(64, 189, 89, 23);

btnNewButton_2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

resultshow.setText("");

resultshow.setText("Button5 Pressed");

}

});

frame.getContentPane().add(btnNewButton_2);

JButton btnNewButton_3 = new JButton("Button2");

btnNewButton_3.setBounds(224, 98, 89, 23);

btnNewButton_3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

resultshow.setText("");

resultshow.setText("Button2 Pressed");

}

});

frame.getContentPane().add(btnNewButton_3);

JButton btnNewButton_4 = new JButton("Button4");

btnNewButton_4.setBounds(224, 145, 89, 23);

btnNewButton_4.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

resultshow.setText("");

resultshow.setText("Button4 Pressed");

}

});

frame.getContentPane().add(btnNewButton_4);

JButton btnNewButton_5 = new JButton("Button6");

btnNewButton_5.setBounds(224, 189, 89, 23);

btnNewButton_5.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

resultshow.setText("");

resultshow.setText("Button6 Pressed");

}

});

frame.getContentPane().add(btnNewButton_5);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote