JAVA GUI
Created by | Borhan |
---|---|
Last edited time | |
Tag |
JFRAME : A GUI window to add components to
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
......
JFrame frame = new JFrame();
frame.setTitle("Title"); // title on title bar
frame.setSize(420, 420); // width and height
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // operation when click on exit button
frame.setResizable(false); // the frame resizable or not
frame.setVisible(true); // frame visibility
// adding frame icon on top left
ImageIcon image = new ImageIcon("logo.jpg");
frame.setIconImage(image.getImage());
// changing frame background
frame.getContentPane().setBackground(new Color(0xFFFFFF));
JLabel : A GUI display are for a string of text, an image or both
import java.awt.Color;
import java.awt.Font;
import javax.swing.*;
import javax.swing.border.Border;
....
JLabel label = new JLabel();
label.setText("Hello world");
Border border = BorderFactory.createLineBorder(Color.green, 3); // border
ImageIcon image = new ImageIcon("logo.jpg");
label.setIcon(image);
label.setHorizontalTextPosition(JLabel.CENTER); // Text position horizontally relative to icon, LEFT, CENTER or RIght
label.setVerticalTextPosition(JLabel.TOP); // Text position vertically relative to icon, TOP, CENTER or BOTTOM
label.setForeground(Color.green); // Text color
label.setFont(new Font("Roboto", Font.BOLD , 20)); // set font
label.setIconTextGap(10); // gap between image and text
label.setBackground(Color.black); // bg of whole label
label.setOpaque(true); // to show bg
label.setBorder(border);
label.setVerticalAlignment(JLabel.CENTER);
label.setHorizontalAlignment(JLabel.CENTER);
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label);
// to specifies the label dimension and w, h
frame.setLayout(null); // it refers that we will manually set dimension of labels or these
label.setBounds(10, 10, 200, 200);
JPanel : A GUI component that functions as a container to hold other components
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.*;
import javax.swing.border.Border;
...
JLabel label = new JLabel("Hi");
JLabel label2 = new JLabel("Hi");
JLabel label3 = new JLabel("Hi");
JPanel redpanel = new JPanel();
redpanel.setBackground(Color.red);
redpanel.setBounds(0, 0,250, 250);
JPanel bluepanel = new JPanel();
bluepanel.setBackground(Color.blue);
bluepanel.setBounds(250, 0, 250, 250);
bluepanel.setLayout(null);
label3.setBounds(0, 0, 100, 100);
JPanel greenpanel = new JPanel();
greenpanel.setBackground(Color.green);
greenpanel.setBounds(0, 250, 500, 250);
greenpanel.setLayout(new BorderLayout());
JFrame frame = new JFrame();
frame.setTitle("Title");
frame.setSize(500, 500);
frame.getContentPane().setBackground(new Color(0xFFFFFF));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
redpanel.add(label);
greenpanel.add(label2);
bluepanel.add(label3);
frame.add(redpanel);
frame.add(bluepanel);
frame.add(greenpanel);
...
JButton : a button that performs an action when clicked on
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Button extends JFrame {
static JButton btn;
Button() {
btn = new JButton();
btn.setBounds(200, 100, 100, 20);
btn.addActionListener(e -> System.out.println("Clickd"));
btn.setText("Button");
btn.setFocusable(false);
btn.setForeground(Color.red);
btn.setBackground(Color.black);
btn.setBorder(BorderFactory.createEtchedBorder());
//btn.setEnabled(false);
this.setSize(500, 500);
this.setVisible(true);
this.setLayout(null);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.add(btn);
}
}
FlowLayout : Places components in a row, sized at their preferred size. if the horizontal space in the container is too small, the flowlayout class uses the next available row.
import java.awt.FlowLayout;
import javax.swing.*;
public class Flowlayout extends JFrame {
Flowlayout(){
this.setSize(500, 500);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout(FlowLayout.LEADING, 10, 20));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.setVisible(true);
}
}
GridLayout: Places components in a grid of cells. Each component takes all the available space within its cell and each cell is the same size
import java.awt.GridLayout;
import javax.swing.*;
public class Gridlayout extends JFrame{
Gridlayout(){
this.setSize(500, 500);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(3, 3, 10, 10)); //x,y, h-margin, v-margin
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.add(new JButton("1"));
this.setVisible(true);
}
}
JLayeredPane : Swing container that provides a third dimension for positioning components. ex : depth, z-index
import java.awt.Color;
import javax.swing.*;
public class Jlayeredpane extends JFrame{
Jlayeredpane(){
JLabel label1 = new JLabel();
label1.setOpaque(true);
label1.setBackground(Color.black);
label1.setBounds(50, 50, 200, 200);
JLabel label2 = new JLabel();
label2.setOpaque(true);
label2.setBackground(Color.red);
label2.setBounds(100, 100, 200, 200);
JLabel label3 = new JLabel();
label3.setOpaque(true);
label3.setBackground(Color.blue);
label3.setBounds(150, 150, 200, 200);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setBounds(0, 0, 500, 500);
//layeredPane.add(label1, JLayeredPane.DRAG_LAYER);
//layeredPane.add(label2, JLayeredPane.PALETTE_LAYER);
//layeredPane.add(label3, JLayeredPane.DEFAULT_LAYER);
layeredPane.add(label1, Integer.valueOf(0));
layeredPane.add(label2, Integer.valueOf(100));
layeredPane.add(label3, Integer.valueOf(1));
this.setSize(500, 500);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLayout(null);
this.add(layeredPane);
this.setVisible(true);
}
}
Opening new window
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Newwindow implements ActionListener{
JFrame frame = new JFrame();
JButton btn = new JButton("new window");
Newwindow(){
frame.setSize(500, 500);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLayout(null);
btn.setBounds(100, 150, 200, 20);
btn.setFocusable(false);
btn.addActionListener(this);
frame.add(btn);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btn) {
frame.dispose();
Demonewwindow mywindow = new Demonewwindow();
}
}
}
JOptionPane : pop up a standard dialog box that prompts users for a value or informs them of something
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class Joptionpane {
Joptionpane(){
JOptionPane.showMessageDialog(null, "INfooo", "title", JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(null, "INfooo", "title", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "INfooo", "title", JOptionPane.QUESTION_MESSAGE);
JOptionPane.showMessageDialog(null, "INfooo", "title", JOptionPane.WARNING_MESSAGE);
JOptionPane.showMessageDialog(null, "INfooo", "title", JOptionPane.ERROR_MESSAGE);
int n = JOptionPane.showConfirmDialog(null, "Infooo", "Title", JOptionPane.YES_NO_CANCEL_OPTION);
System.out.println(n);
String name = JOptionPane.showInputDialog("Name");
System.out.print(name);
String [] res = {"Option 1", "Option 2", "Option 3"};
ImageIcon icon = new ImageIcon("logo.jpg");
JOptionPane.showOptionDialog(null, "Infoo", "title", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, icon, res, 0);
}
}
Checkbox
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class Checkbox implements ActionListener{
JFrame frame = new JFrame();
JCheckBox checkbox;
JButton btn = new JButton();
Checkbox(){
frame.setSize(500, 500);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
checkbox = new JCheckBox();
checkbox.setText("I am not robot");
checkbox.setFocusable(false);
checkbox.setFont(new Font("Tahoma", Font.PLAIN, 12));
btn.setText("Submit");
btn.addActionListener(this);
frame.add(checkbox);
frame.add(btn);
frame.pack();
frame.setVisible(true);
};
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btn) {
System.out.println(checkbox.isSelected());
}
}
}
Radio button
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class Radiobtn extends JFrame implements ActionListener{
JRadioButton r1, r2, r3;
Radiobtn(){
this.setTitle("Title");
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
r1 = new JRadioButton("R1");
r2 = new JRadioButton("R2");
r3 = new JRadioButton("R3");
ButtonGroup group = new ButtonGroup();
group.add(r1); group.add(r2); group.add(r3);
this.add(r1); this.add(r2); this.add(r3);
r1.addActionListener(this);
r2.addActionListener(this);
r3.addActionListener(this);
this.setVisible(true);
ImageIcon image = new ImageIcon("logo.jpg");
this.setIconImage(image.getImage());
this.getContentPane().setBackground(new Color(255,255,255));
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == r1) {
System.out.println("R1");
}
if(e.getSource() == r2) {
System.out.println("R2");
}
if(e.getSource() == r3) {
System.out.println("R3");
}
}
}