Advertisement
fromMars

JButton Example

Apr 24th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. import javax.swing.*;  
  2.  
  3. public class ButtonExample {  
  4. public static void main(String[] args) {  
  5.     JFrame f=new JFrame("Button Example");  
  6.     JButton b=new JButton("Click Here");  
  7.     b.setBounds(50,100,95,30);  
  8.     f.add(b);  
  9.     f.setSize(400,400);  
  10.     f.setLayout(null);  
  11.     f.setVisible(true);  
  12. }  
  13. }
  14.  
  15. //=======================JButton with Event Listner======================================
  16.  
  17. import java.awt.event.*;  
  18. import javax.swing.*;    
  19. public class ButtonExample {  
  20. public static void main(String[] args) {  
  21.     JFrame f=new JFrame("Button Example");  
  22.     final JTextField tf=new JTextField();  
  23.     tf.setBounds(50,50, 150,20);  
  24.     JButton b=new JButton("Click Here");  
  25.     b.setBounds(50,100,95,30);  
  26.     b.addActionListener(new ActionListener(){  
  27. public void actionPerformed(ActionEvent e){  
  28.             tf.setText("Welcome to Javatpoint.");  
  29.         }  
  30.     });  
  31.     f.add(b);f.add(tf);  
  32.     f.setSize(400,400);  
  33.     f.setLayout(null);  
  34.     f.setVisible(true);  
  35. }  
  36. }
  37.  
  38. //=========================DisPLAY IMAGE ON BUTTON ========================
  39. import javax.swing.*;      
  40. public class ButtonExample{    
  41. ButtonExample(){    
  42. JFrame f=new JFrame("Button Example");            
  43. JButton b=new JButton(new ImageIcon("D:\\icon.png"));    
  44. b.setBounds(100,100,100, 40);    
  45. f.add(b);    
  46. f.setSize(300,400);    
  47. f.setLayout(null);    
  48. f.setVisible(true);    
  49. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
  50.     }        
  51. public static void main(String[] args) {    
  52.     new ButtonExample();    
  53. }    
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement