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.
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
Comments
Leave a comment