import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;
import java.awt.event.*;
public class MyGridBagEx implements ActionListener
{
JFrame frame;
JPanel pan,pann;
JButton add,update,del;
JButton test,ok,cancel,exit;
public MyGridBagEx()
{
frame= new JFrame();
int width=500,height=350;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x= (screen.width-width)/2;
int y= (screen.height-height)/2;
frame.setBounds(x,y,width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Testing Application");
Container contentPane = frame.getContentPane();
pann = new JPanel();
pann.setLayout(new BorderLayout());
pann.setOpaque(true);
pann.setBorder(BorderFactory.createLineBorder(Color.RED));
pan =new JPanel();
pan.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
pan.setBackground(new Color(202,215,237));
pan.setOpaque(true);
pan.setBorder(BorderFactory.createLineBorder(Color.BLACK));
add= new JButton("Add");
c.gridx = 0;
c.gridy = 0;
pan.add(add, c);
update= new JButton("Update");
c.gridx = 1;
c.gridy = 0;
pan.add(update, c);
del= new JButton("Delete");
c.gridx = 2;
c.gridy = 0;
pan.add(del, c);
ok= new JButton("OK- This is Ok Button for testing");
c.ipady = 40;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
ok.addActionListener(this);
pan.add(ok, c);
cancel= new JButton("Press Cancel ");
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 2; //third row
cancel.addActionListener(this);
pan.add(cancel, c);
exit= new JButton("Exit");
c.ipady = 0;
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 2;
pan.add(exit,c);
test= new JButton("Testing");
c.gridx = 3;
c.gridy = 0;
pan.add(test, c);
pann.add(pan, BorderLayout.NORTH);
contentPane.add(pann);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == ok)
{
JOptionPane.showMessageDialog(null," Ok");
}
if(e.getSource() == cancel)
{
System.exit(0);
}
}
public static void main (String []arg)
{
MyGridBagEx f = new MyGridBagEx();
}
}
0 comments: