A quick look on Swings in Java
JButton:
The above code displays :
JLabel:
The above code displays :
JTextField:
JButton:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.awt.event.*; import javax.swing.*; public class FirstButton{ FirstButton(){ JFrame f=new JFrame(); //initialising frame JButton b=new JButton("ABC"); //button's Definition b.setBounds(130,100,100, 40); //Position of button (x1, y1, x2, y2) f.setTitle("Button in Java"); // Title of the frame f.add(b); //adds button b to the frame f.setSize(300,400); // Sets size of frame f.setLayout(null); // Sets no layout f.setVisible(true); // Makes frame visible f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // To make the X button functioning } public static void main(String[] args) { new FirstButton(); } } |
The above code displays :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.awt.event.*; import javax.swing.*; import java.awt.*; public class FirstLabel{ FirstLabel(){ JFrame f=new JFrame(); //initialising frame JLabel jl=new JLabel("This is a Label", JLabel.CENTER); f.setTitle("Label in Java"); f.add(jl); f.setSize(300,300); f.setLayout(new FlowLayout()); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // To make the X button functioning } public static void main(String[] args) { new FirstLabel(); } } |
The above code displays :
JTextField:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.awt.event.*; import javax.swing.*; import java.awt.*; public class FirstTextField{ FirstTextField(){ JFrame f=new JFrame(); //initialising frame JTextField jl=new JTextField(20); f.setTitle("Text Field in Java"); f.add(jl); f.setSize(300,300); f.setLayout(new FlowLayout()); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // To make the X button functioning } public static void main(String[] args) { new FirstTextField(); } } |
The above code displays :
JRadioButton and Event Handling:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import javax.swing.*; import java.awt.*; import java.awt.event.*; class RadioExample extends JFrame implements ActionListener{ JRadioButton rb1,rb2; JButton b; RadioExample(){ rb1=new JRadioButton("Male"); rb1.setBounds(100,50,100,30); rb2=new JRadioButton("Female"); rb2.setBounds(100,100,100,30); ButtonGroup bg=new ButtonGroup(); bg.add(rb1);bg.add(rb2); b=new JButton("click"); b.setBounds(100,150,80,30); b.addActionListener(this); add(rb1);add(rb2);add(b); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300,300); setLayout(null); setVisible(true); } public void actionPerformed(ActionEvent e){ if(rb1.isSelected()){ JOptionPane.showMessageDialog(this,"You are male"); } if(rb2.isSelected()){ JOptionPane.showMessageDialog(this,"You are female"); } } public static void main(String args[]){ new RadioExample(); }} |
The above code shows :





Thanks bro!...big help!
ReplyDeleteWelcome :)
DeleteThanks a lot
ReplyDelete