by creating an object of the Frame class. That is, creation through association
by extending the Frame class. That is, OOP creation through class inheritance
.setTitle(" "); - sets title of frame
.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
exits out of application
.setResizable(false);
prevent frame from being resized
.setSize(420, 420);
sets the dimensions of the frame
.setVisible(true);
make frame visible
.setIconImage();
change icon of the frame
the argument inside of this will come from an image that is instantiated from ImageIcon.
.getContenPane().setBackground(Color.red);
change color of background
can be modified with RGB value / Hexadecimal value
.add()
method of the JFrame to add new components
.setText(" ");
sets text of label
.setIcon();
sets icon of label
the argument inside of this must be an icon like ImageIcon.
.setBounds(int x, int y, int a, int b);
sets position & dimensions of the label
x & y for the position and a & b for the dimension
.setHorizontalTextPosition(JLabel.CENTER)
if you want your label at the center
.setVerticalTextPosition(JLabel.TOP)
if you want your label at the top
.setForeground(Color.gray);
changes the font color
.setFont(newFont());
customize your font with setFont method
.setIconTextGap();
sets gap between text label and icon label
.setVerticalAlignment(Jlabel.Center);
.setHorizontalAlignment(Jlabel.Center);
centers the label's content vertically & horizontally within the label's bounds
JLabel - a component which displays a readable text or an image in the Swing Container User Interface.
JPanel - is a simplest container class. It provides space in which an application can attach other component. It inherits the Jcomponents class.
.setBackground(int x, int, y, int a, int b);
sets the background of the panel
.addActionListener(action);
adds action to the button
.setFocusable(false);
removes focus to the button
BorderLayout - arranges the components to fit in the five regions:
east
west
north
south
center
FlowLayout - arranges the components in a line, one after another. If the horizontal space in the container is too small, it will use the next available row.
GridLayout - arranges the components a rectangular grid. One component is displayed in each rectangle.
JOptionPane - is used to provide standard dialog boxes. These dialog boxes are used to display information or get input from the user
JTextField - is a text component that allows the editing of a single line text.
JCheckBox - is a text component that turn an option on (true) or off (false)
JRadioButton - is used to choose one option from multiple options
JComboBox - shows a popup menu that shows a list and the user can select an option from that specified list
BorderLayout
.setLayout(newBorderLayout());
.add(label,BorderLayout.North);
center component
south and north
west and east
.setLayout(newBorderLayout(5, 10);
.setLayout(newBorderLayout());
to set the BorderLayout to the container
.add(label,BorderLayout.North);
to layout a component with border layout (NORTH, EAST, WEST, SOUTH, CENTER).
center component
will resize proportionally to the size of the frame
south and north
component will only expand horizontally
west and east - component will only expand vertically
.setLayout(newBorderLayout(5,10);
to set up margin for the BorderLayout of each components