(1)Frame example:
import javax.swing.*;
public class swing1
{
public static void main(String []args)
{
MyFrame jf=new MyFrame("MYFRAME");
}
}
class MyFrame extends JFrame
{
MyFrame(String s)
{
super(s);
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
save above code as swing1.java
& comiple it by following way
D:\>javac swing1.java
then run it by following way
D:\>java swing1
output:-
import javax.swing.*;
public class swing1
{
public static void main(String []args)
{
MyFrame jf=new MyFrame("MYFRAME");
}
}
class MyFrame extends JFrame
{
MyFrame(String s)
{
super(s);
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
save above code as swing1.java
& comiple it by following way
D:\>javac swing1.java
then run it by following way
D:\>java swing1
(2)Label example:
import javax.swing.*;
public class swingex1
{
public static void main(String []args)
{
MyFrame jf=new MyFrame("FRAME");
}
}
class MyFrame extends JFrame
{
JLabel l1;
MyFrame(String s)
{
l1=new JLabel("HELLO");
add(l1);
setVisible(true);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
save above code as swingex1.java
& compile & run it
D:\>javac swingex1.java
D:\>java swingex1
(3) JTextField ,JTextArea , PasswordField:
import javax.swing.*;
public class Txt
{
public static void main(String [] args)
{
myframe f=new myframe();
}
}
class myframe extends JFrame
{
JTextField tf1,tf2,tf3;
JTextArea ta1,ta2;
JPasswordField pwd;
JPanel p;
myframe()
{
p=new JPanel();
tf1=new JTextField();
p.add(tf1);
tf2=new JTextField("it is text");
p.add(tf2);
tf3=new JTextField("it is second text filed", 20);
p.add(tf3);
add(p);
ta1=new JTextArea("it is text area",10,10);
p.add(ta1);
ta2=new JTextArea("second text area",20,20);
p.add(ta2);
pwd=new JPasswordField(20);
p.add(pwd);
pwd.setEchoChar('@');
setVisible(true);
setSize(700,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
D:\>javac Txt.java
D:\>java Txt
Java swing Event handling Examples :-
(3) JTextField ,JTextArea , PasswordField:
import javax.swing.*;
public class Txt
{
public static void main(String [] args)
{
myframe f=new myframe();
}
}
class myframe extends JFrame
{
JTextField tf1,tf2,tf3;
JTextArea ta1,ta2;
JPasswordField pwd;
JPanel p;
myframe()
{
p=new JPanel();
tf1=new JTextField();
p.add(tf1);
tf2=new JTextField("it is text");
p.add(tf2);
tf3=new JTextField("it is second text filed", 20);
p.add(tf3);
add(p);
ta1=new JTextArea("it is text area",10,10);
p.add(ta1);
ta2=new JTextArea("second text area",20,20);
p.add(ta2);
pwd=new JPasswordField(20);
p.add(pwd);
pwd.setEchoChar('@');
setVisible(true);
setSize(700,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
D:\>javac Txt.java
D:\>java Txt
Java swing Event handling Examples :-
import javax.swing.*;
import java.awt.event.*;
public class Main
{
public static void main(String[] args)
{
JFrame frame=new JFrame("Button Click Example");
final JTextField text_field=new JTextField(); //JTextField object
text_field.setBounds(50,100, 150,20);
JButton click_button=new JButton("Click Me!!!"); //JButton object
click_button.setBounds(20,50,75,30);
click_button.addActionListener(new ActionListener()
{ //add an event and take action
public void actionPerformed(ActionEvent e){
text_field.setText("You Clicked the button");
}
});
//add button and textfield to the frame
frame.add(click_button);frame.add(text_field);
frame.setSize(400,400);
frame.setLayout(null);
frame.setVisible(true);
}
}
output:-
Sign up here with your email
ConversionConversion EmoticonEmoticon