0% found this document useful (0 votes)
95 views

Java Swing Demo (Class Name Test)

This Java code defines a class called "test" that creates a GUI application with a JFrame containing buttons in the north, south, east, and west borders. The class constructor initializes the frame and GUI components, adds action listeners to the buttons, and sets the frame to visible. The actionPerformed method displays different messages depending on which button is clicked.

Uploaded by

bejarun
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

Java Swing Demo (Class Name Test)

This Java code defines a class called "test" that creates a GUI application with a JFrame containing buttons in the north, south, east, and west borders. The class constructor initializes the frame and GUI components, adds action listeners to the buttons, and sets the frame to visible. The actionPerformed method displays different messages depending on which button is clicked.

Uploaded by

bejarun
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*

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();
}
}

You might also like