Java Swing Demo (Class Name Test)
Java Swing Demo (Class Name Test)
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.SwingUtilities;
import java.awt.Rectangle;
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class test implements ActionListener
{
//public declearation of components
JFrame window;
JPanel pn;
JLabel lbl;
JButton top,bottom,left,right;
//constructor to create window and components
public test()
{
window = new JFrame("Test window");
pn = new JPanel();
pn.setLayout(null);
lbl=new JLabel("This is example of broder layout and frame and panel")
;
lbl.setSize(400,20);
pn.add(lbl);
Container c; //top level container
top = new JButton("Top");
bottom = new JButton("bottom");
left = new JButton("left");
right = new JButton("right");
c=window.getContentPane(); // get the content pane of frame
c.setLayout(new BorderLayout()); // then set the layout of frame
c.add(top,BorderLayout.NORTH);
c.add(bottom,BorderLayout.SOUTH);
c.add(right,BorderLayout.EAST);
c.add(left,BorderLayout.WEST);
c.add(pn,BorderLayout.CENTER); // in center to add a one panel
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); // to get a cu
rrent screen height and width
dim.height = ((int)dim.getHeight()) - 100; // to less some height
dim.width = ((int)dim.getWidth()) - 100; //to less some height
window.setSize(dim); // set the dimension of window(frame)
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
top.addActionListener(this); // add actionlisteners
bottom.addActionListener(this);
left.addActionListener(this);
right.addActionListener(this);
}
public void actionPerformed (ActionEvent e)
{
if (e.getSource() == top)
{
System.out.println("you hava clicked top button");
JOptionPane.showMessageDialog(window,"youhave clicked TOP button","Arun.J"
,JOptionPane.INFORMATION_MESSAGE);
}
else if (e.getSource() == bottom)
{
System.out.println("you hava clicked bottom button");
JOptionPane.showMessageDialog(window,"youhave clicked BOTTOM button","Arun
.J",JOptionPane.INFORMATION_MESSAGE);
}
else if (e.getSource() == left)
{
System.out.println("you hava clicked left button");
JOptionPane.showMessageDialog(window,"youhave clicked LEFT button","Arun.
J",JOptionPane.INFORMATION_MESSAGE);
}
else if (e.getSource() == right)
{
System.out.println("you hava clicked RIGHT button");
JOptionPane.showMessageDialog(window,"youhave clicked LEFT button","Arun.
J",JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] args)
{
test t = new test();
}
}