Simple Java Contact Form

Simple Java Contact Form

The following Java code defines a simple GUI application using Swing. It creates a form with text fields for name and email, a text area for messages, and actions buttons for OK and Cancel operations. Additionally, there is a button to change the background color of the form.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Form extends JFrame {
  private JPanel bottom, top, centre;
  private JButton b1, b2, OK;
  private JLabel n, e, m;
  private Color color = Color.black;
  private String s1, s2, s3, s4 = "", s5;
  TextField screen = new TextField(20);
  TextField scr = new TextField(20);
  TextArea a = new TextArea(6, 20);

  public Form()

  {
    super("Form");

    Container c = new Container();
    c = getContentPane();

    bottom = new JPanel();

    bottom.setBorder(BorderFactory.createEtchedBorder(Color.cyan, Color.blue));

    JButton b1 = new JButton("OK");
    JButton b2 = new JButton("Cancel");
    bottom.add(b1);

    bottom.add(b2);

    c.add(bottom, BorderLayout.SOUTH);

    top = new JPanel();
    n = new JLabel("NAME   ");
    e = new JLabel("EMAIL ");
    m = new JLabel("MESS");

    top.setLayout(new FlowLayout(FlowLayout.LEFT, 200, 50));

    top.add(n);
    top.add(scr);
    top.add(e);
    top.add(screen);
    top.add(m);
    top.add(a);
    c.add(top, BorderLayout.CENTER);
    top.setBorder(BorderFactory.createEtchedBorder(Color.blue, Color.green));

    centre = new JPanel();
    c.add(centre, BorderLayout.NORTH);
    centre.setBorder(BorderFactory.createEtchedBorder(Color.magenta, Color.pink));

    Button b3 = new Button("CHANGE COL");
    centre.add(b3);
    b3.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Container c = getContentPane();
        color = JColorChooser.showDialog(Form.this, "Choose a color", color);
        if (color == null)
          color = Color.black;
        centre.setBackground(color);
        centre.repaint();
      }
    });

    b1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        s1 = "The name is: " + " " + scr.getText();
        s1 += "\nEmail Addres is: " + " " + screen.getText();
        s1 += "\nMessage is: " + a.getText();
        s1 += "\nBackground Color is: " + color.toString();
        JOptionPane.showMessageDialog(null, s1);

      }
    });

    b2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {

        dispose();
        System.exit(0);

      }
    });

    setSize(800, 500);
    setVisible(true);
  }

  public static void main(String args[]) {
    Form app = new Form();

    app.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  }
}

Once you run the code you will see the following Java Form.

A Simple Java Form

 

M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post