A Simple GUI


Building a Java GUI - Rule 1:

Decide on the components you will need, including their functionality and then make a sketch of the layout of the components.

The rest is straight-forward but tedious.

The API    How to Use BorderLayout    Servlets and JSP Chapter 4  - A crash course in HTML5 and CSS3

An Example using a BorderLayout:

BoarderLayout

An Example

BL1BL2

The Code:

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

public class aSimpleGUI {

    public static void main(String[] args) {

        JFrame aframe = new JFrame("A Sample");
        final JTextField atextfield=new JTextField();
        Font the_font = new Font("SansSerif", Font.BOLD, 20);
        atextfield.setFont(the_font);
        JButton pressme=new JButton("button 1");
        JButton pressme1=new JButton("<html><font size=\"+1\"><b>button 2</b></font></html>");
        ActionListener actionListener1 = new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                atextfield.setText("hello");
            };
        };
        pressme1.addActionListener(actionListener1);
        JEditorPane anhtmlpane= new JEditorPane();
        anhtmlpane.setEditable(false);
        anhtmlpane.setContentType("text/html;charset=\"UTF-8\"");
        anhtmlpane.setText("<html><center><font size=\"+3\">&delta;</font></center></html>");
        aframe.setLayout(new BorderLayout());
        aframe.add(atextfield, BorderLayout.NORTH);
        JPanel theholder=new JPanel();
        theholder.add(pressme);
        theholder.add(pressme1);
        aframe.add(theholder, BorderLayout.SOUTH);
        aframe.add(Box.createRigidArea(new Dimension(50,100)), BorderLayout.EAST);
        //aframe.add(something, BorderLayout.WEST); Not Used
        aframe.add(anhtmlpane, BorderLayout.CENTER);
        aframe.setPreferredSize(new Dimension(400, 300));
        aframe.pack();
        aframe.setVisible(true);
        aframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ;
    }
}