Answer to Question #263865 in Java | JSP | JSF for liza

Question #263865

Create frame to display two buttons named "Click Me-1" and "Click Me-2". If you click on the "Click Me-1" button then a JOptionPane Message dialog box should display message " you have clicked Click Me-1 button" and 


If you click on the "Click Me-2" button then a JOptionPane Message dialog box should display message " you have clicked Click Me-2 button". Clicking on the cross button in the frame should exit the program.


1
Expert's answer
2021-11-10T14:51:27-0500


SOLUTION TO THE ABOVE QUESTION


SOLUTION CODE


package com.company;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Main extends JFrame {
    public Main() throws HeadlessException {
        setSize(200, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button1 = new JButton("Click Me-1");
        button1.setPreferredSize(new Dimension(100,70));
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog((Component) e.getSource(), " you have clicked Click Me-1 button");
            }
        });

        JButton button2 = new JButton("Click Me-2");
        button2.setPreferredSize(new Dimension(100,70));
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog((Component) e.getSource(), "you have clicked Click Me-2 button");
            }
        });

        setLayout(new FlowLayout(FlowLayout.CENTER));
        getContentPane().add(button1);
        getContentPane().add(button2);

    }

    public static void main(String[] args) {
        new Main().setVisible(true);
    }
}


SAMPLE PROGRAM OUTPUT







Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS