https://www.guru99.com/java-swing-gui.html#3
java GUI summary
* Components (or in Swing, JComponents)
JComponents are the fundamental primative widgets everything is built from
JTextField
JTextArea
JComboBox
JLabel
JList
JMenuBar
JOptionPane
JPanel
JScrollBar
JButton
* Containers
Containers contain components
Container are themselves components
For creating a GUI, we need at least one container
Three of the most useful types of containers:
JPanel - A pure container, not a window by itself
JFrame - A fully functioning window with a title and icons
JApplet - Usually used in a browser
Other containers : JToolBar JScrollPane JDialog
***************************************
* Simple example with no layout manager
***************************************
import javax.swing.*;
class gui {
public static void main(String args[]) {
JFrame frame = new JFrame("My First GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
frame.getContentPane().add(button1);
frame.getContentPane().add(button2);
frame.setVisible(true);
}
}
***************************************
Note that we typically create our own class which is an
extension of JFrame, but has some :
smart layout manager stuff
its own variables
its own special methods
BorderLayout is the default layout manager of JFrame
FlowLayout is the default layout manager of JPanel
Note that a frame can hold JComponents or conatiners
***************************************
* The content pane of a window is where you place stuff. It is the
inner area of the window.
A JFrame will always have a content pane, even if you don't
create it.
You get it with
frame.getContentPane()
Container con = getContentPane();
con.setLayout(flow);
con.add(princLabel);
.
.
.
You can also create your own content pane, and then set it to be the frame content pane
// in class definition
BorderLayout bord = new BorderLayout()
JPanel pane = new JPanel();
// in the constructor
pane.setlayout(bord);
pane.add(comp1, BorderLayout.NORTH)
pane.add(comp2, BorderLayout.South)
setContentPane(pane);
***************************************
***************************************
* JFrame()
***************************************
The class JFrame is one of the most important classes
A JFrame object is the top level window in your GUI
If you create your own kind of object, it will just be an
extension of JFrame
Important methods provided by the class JFrame
public JFrame() - constructor
public JFrame(String s) - constructor with a title
public void setSize(int w, int h)
public void setTitle(String s)
public void setVisible(boolean b)
public void setDefaultCloseOperation(int operation)
(usually "operation" is EXIT_ON_CLOSE)
public void addWindowListener(WindowEvent e)
You can directly use the class JFrame, and create your
own JFrame object, which will be your GUI window
***************************************
***************************************
* Basic Framework of a java class --> gregThing.java
***************************************
public class gregThing
{
// all my static variables that come with each gregThing
int xval;
double lenghval;
.
.
.
// the default constructor
public gregThing()
{
}
// other methods that come with a gregThing
public void calculateVal()
{
}
.
.
.
// main method
public static void main(String [] args)
{
//usually create one instance of the class
}
}
***************************************
* Basic Framework of a java GUI class --> johnnyFrame.java
***************************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class johnnyFrame extends JFrame implements ActionListener
{
// static variables (include your basic JComponents - Jbutton, JTextField, ...)
// default constructor
public johnnyFrame()
{
}
// alternate constructor
public johnnyFrame(String namestr)
{
}
// Main Method
public static void main(String[] args)
{
johnnyFrame frm = new JohnnyFrame()
frm.setTitle("Whei")
}
}