Swing
Swing
Creating Frame:
JFrame
Java: JFrame
Java swing: JFrame class 4 Java: swing methods
The class JFrame is an extended version of java.awt.Frame that adds
support for the JFC/Swing component architecture. It is like a board where
you can add your swing objects
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Container;
Java: ImageIcon
7
Container c=frame.getContentPane();
c.setBackground(Color.RED); Java swing : JFrame setResizable()
//create color object frame.setResizable(false);
//Color color=new Color(255,0,0)
Notes If the argument is false, the frame cann’t be resized.
//c.setBackground(color);
Notes To change the background of the frame, first create a
Container class object then set its background color using
Color class
Jlabel8
Font
setText()
setFont()
add()
import java.awt.Font;
import javax.swing.JLabel
Java: JLabel
Java swing : Jlabel class 9 Java swing : Font class
JFrame frame=new JFrame("JLabel"); Font font=new Font("Arial",Font.PLAIN,12);
frame.setBounds(200, 100,400,300); label.setFont(font);
frame.setVisible(true); Notes Font class constructor requires three parameters like
Font name, Font type and Font size
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=frame.getContentPane();
c.setLayout(null); Java swing : ImageIcon with JLabel class
JLabel label=new JLabel("Username:");
//label.setText(“User Name:”); ImageIcon icon=new
label.setBounds(50,70,100,30); ImageIcon("F:\\Zaman\\Image\\Apple.png");
c.add(label); JLabel label=new
JLabel("Apple",icon,JLabel.LEFT);
label.setBounds(50,70,500,200);
c.add(label);
frame.setVisible(true);
setText()
setEditable()
setForeground()
setBackground()
setEchoChar()
import javax.swing.JTextField
import javax.swing.JPasswordField
Java: JTextField & JPasswordField
Java swing : JTextField
JTextField text=new JTextField();
text.setText("type here");
text.setBounds(50,30,100,20);
text.setForeground(Color.blue);
text.setBackground(Color.cyan);
text.setEditable(true);
c.add(text);
Notes: JTextField() is a input box
import javax.swing.JButton
import java.awt.Cursor
Java: JTextField & JPasswordField
Java swing : JButton & Cursor class
JButton btn=new JButton("Submit");
btn.setBounds(100,50,100,30);
Cursor cursor=new Cursor(Cursor.HAND_CURSOR);
btn.setCursor(cursor);
btn.setEnabled(false);
c.add(btn);
Notes when cursor moves inside button Hand cursor is
displayed. To disable the button use setEnabled() to
false
Java: JRadioButton
Java swing : JRadioButton is used to select one from many
JRadioButton rb1=new JRadioButton();
Notes ButtonGroup() : Use to
JRadioButton rb2=new JRadioButton(); create a group, in which we can
ButtonGroup bg=new ButtonGroup(); add JRadioButton. We can
select only one JRadioButton in
rb1.setBounds(400,200,70,30); a ButtonGroup.
rb1.setText("Male");
rb1.setBackground(color);
c.add(rb1);
rb2.setBounds(400,240,70,30);
rb2.setText("Female");
rb2.setBackground(color);
c.add(rb2);
bg.add(rb1);
bg.add(rb2);
Java: JCheckBox & JTextArea
Java swing : JCheckBox
JCheckBox cb=new JCheckBox(); Notes The JCheckBox class is used to
create a checkbox. It is used to turn an
cb.setBounds(400,340,70,30); option on (true) or off (false). Clicking on a
cb.setText("Agree"); CheckBox changes its state from "on" to
cb.setBackground(color); "off" or from "off" to "on ".It
inherits JToggleButton class.
c.add(cb);
Another approach
String days[]={"Sunday","Monday","Tuesday","Wednesday","Thursday"};
JComboBox cb=new JComboBox<>(days);
//inside constructor
cb.setBounds(50,50,100,30);
cb.setSelectedIndex(1);
cb.setEditable(true);
c.add(cb);
Java: inheritance
20 Java swing : ActionListener
The Java ActionListener is notified whenever you
click on the button or menu item. It is notified
against ActionEvent. The ActionListener interface is
found in java.awt.event package. It has only one
method: actionPerformed().
THANK YOU