package net.javavideotutorials.example;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SwingTutorial extends JFrame
{
private static final String MY_TEXT_FIELD = ”MyTextField”;
private static final String MY_BUTTON = ”MyButton”;

public static void main (String[] args)
{
new SwingTutorial();
}

public SwingTutorial ()
{
JFrame frame = new JFrame(”My First Swing Tutorial”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 500);

JPanel panel = new JPanel();

panel.setLayout(new GridBagLayout());
panel.addMouseListener(new MouseListener()
{

@Override
public void mouseReleased(MouseEvent e)
{
System.out.println(e);
}

@Override
public void mousePressed(MouseEvent e)
{
System.out.println(e);
}

@Override
public void mouseExited(MouseEvent e)
{
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e)
{
// TODO Auto-generated method stub

}

@Override
public void mouseClicked(MouseEvent e)
{
System.out.println(e);

}
});

// add a label, textField, button to the panel
// first I’ll need to create these components
JLabel label = new JLabel(”This is a label”);

final JTextField textField = new JTextField();
textField.addActionListener(new ActionListener()
{

@Override
public void actionPerformed(ActionEvent e)
{
textField.setText(”Enter key pressed”);

}
});
textField.setActionCommand(MY_TEXT_FIELD);

JButton button = new JButton(”This is a button”);

button.addMouseListener(new MouseListener()
{

@Override
public void mouseReleased(MouseEvent e)
{
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e)
{
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e)
{
System.out.println(e);

}

@Override
public void mouseEntered(MouseEvent e)
{
System.out.println(e);

}

@Override
public void mouseClicked(MouseEvent e)
{
// TODO Auto-generated method stub

}
});
button.addActionListener(new ActionListener()
{

@Override
public void actionPerformed(ActionEvent e)
{
textField.setText(”Button Clicked”);
}
});
button.setActionCommand(MY_BUTTON);

GridBagConstraints c = new GridBagConstraints();

c.gridx = 1;
c.gridy = 0;
c.insets = new Insets(0, 0, 15, 15);
// add the components TO the panel
panel.add(label, c);

c = new GridBagConstraints();

c.gridx = 2;
c.gridy = 0;
c.ipadx = 100;
c.insets = new Insets(0, 15, 0, 0);
panel.add(textField, c);

c = new GridBagConstraints();

c.gridx = 1;
c.gridy = 1;
c.insets = new Insets(15, 0, 0, 15);
panel.add(button, c);

// add the panel to the frame
frame.add(panel);

frame.setVisible(true);
}

}