import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class mainWindow extends JFrame implements ActionListener{
private JButton Button1;
private JButton Button2;
public mainWindow(){
//Set the name of window....
super("Window Name");
//Getting container handle
Container container=getContentPane();
//Adding layout ....
container.setLayout(null);
//Set the background color of the window...
container.setBackground(new Color(111,143,170));
//Creating first button.....
Button1=new JButton("Button1");
//Set the position of button1.....
Button1.setBounds(new Rectangle(10,30,80,30));
//Adding action listener for button1..
Button1.addActionListener(this);
//Add the button1 to window....
container.add(Button1);
Button2=new JButton("Button2");
Button2.setBounds(new Rectangle(100,30,80,30));
Button2.addActionListener(this);
container.add(Button2);
}
//Adding action for button click...
public void actionPerformed(ActionEvent ae){
try{
//getSource() method give object reference of button during click event.
if(ae.getSource()==Button1) // Executed when button1 pressed.
JOptionPane.showMessageDialog(this,"Button1 Clicked!","Message Heading",JOptionPane.INFORMATION_MESSAGE);
else if(ae.getSource()==Button2) // Executed when button2 pressed.
JOptionPane.showMessageDialog(this,"Button2 Clicked!","Message Heading",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
try{
mainWindow mw=new mainWindow();
//Setting for window width and height
mw.setSize(400,400);
//setVisible option always true..If u set as false than the window not visible to the user...
mw.setVisible(true);
//Resizable option....If u set as ture, user can able to resize the window..
mw.setResizable(false);
mw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
catch(Exception e){
e.printStackTrace();
}
}
}
import java.awt.*;
import java.awt.event.*;
public class mainWindow extends JFrame implements ActionListener{
private JButton Button1;
private JButton Button2;
public mainWindow(){
//Set the name of window....
super("Window Name");
//Getting container handle
Container container=getContentPane();
//Adding layout ....
container.setLayout(null);
//Set the background color of the window...
container.setBackground(new Color(111,143,170));
//Creating first button.....
Button1=new JButton("Button1");
//Set the position of button1.....
Button1.setBounds(new Rectangle(10,30,80,30));
//Adding action listener for button1..
Button1.addActionListener(this);
//Add the button1 to window....
container.add(Button1);
Button2=new JButton("Button2");
Button2.setBounds(new Rectangle(100,30,80,30));
Button2.addActionListener(this);
container.add(Button2);
}
//Adding action for button click...
public void actionPerformed(ActionEvent ae){
try{
//getSource() method give object reference of button during click event.
if(ae.getSource()==Button1) // Executed when button1 pressed.
JOptionPane.showMessageDialog(this,"Button1 Clicked!","Message Heading",JOptionPane.INFORMATION_MESSAGE);
else if(ae.getSource()==Button2) // Executed when button2 pressed.
JOptionPane.showMessageDialog(this,"Button2 Clicked!","Message Heading",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
try{
mainWindow mw=new mainWindow();
//Setting for window width and height
mw.setSize(400,400);
//setVisible option always true..If u set as false than the window not visible to the user...
mw.setVisible(true);
//Resizable option....If u set as ture, user can able to resize the window..
mw.setResizable(false);
mw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
catch(Exception e){
e.printStackTrace();
}
}
}
Sample output looks like this..........
Comments