Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseListener;
  7. import javax.swing.JButton;
  8.  
  9. public class MainPanel extends javax.swing.JPanel{
  10.  
  11.     int count=0;
  12.     MainFrame mf;
  13.    
  14.    
  15.     public MainPanel(MainFrame mf){
  16.         this.mf=mf;
  17.         setLayout(null);
  18.        
  19.         actionListenerButton(50,60);
  20.         mouseListenerButton(50,90);
  21.  
  22.         setBackground(Color.WHITE);
  23.     }
  24.    
  25.     /**create a button that implemented action listener
  26.      *
  27.      */
  28.     public void actionListenerButton(int x,int y){
  29.         final JButton btn=new JButton("Action Listener");
  30.        
  31.         //set mouse location x-axis and y-axis, width and height
  32.         //setBounds(x-axis,y-axis,width,height)
  33.         btn.setBounds(x,y,150,25);
  34.        
  35.         //implement action to button by using action listener
  36.         btn.addActionListener(new ActionListener(){
  37.             //perform an action after click(press and release) the button
  38.             public void actionPerformed(ActionEvent e){
  39.                 //write button action here
  40.                 count++;
  41.                 repaint();
  42.             }
  43.         });
  44.        
  45.         add(btn);
  46.     }
  47.    
  48.    
  49.     /**create a button that implemented mouse listener
  50.      *
  51.      */
  52.     public void mouseListenerButton(int x,int y){
  53.         final JButton btn=new JButton("Mouse Listener");
  54.        
  55.         //set mouse location x-axis and y-axis, width and height
  56.         //setBounds(x-axis,y-axis,width,height)
  57.         btn.setBounds(x,y,150,25);
  58.        
  59.         //implement action to button by using mouse listener
  60.         btn.addMouseListener(new MouseListener(){
  61.             Color defaultBG=btn.getBackground();
  62.             //perform an action when the cursor/pointer exit/move away from the button
  63.             public void mouseExited(MouseEvent e){
  64.                 btn.setText("Mouse Listener");
  65.                
  66.                 //reset button background
  67.                 btn.setBackground(defaultBG);
  68.             }
  69.            
  70.             //perform an action when the cursor/pointer enter/move into the button
  71.             public void mouseEntered(MouseEvent e){
  72.                 btn.setText("Mouse Entered");
  73.                 btn.setBackground(Color.YELLOW);
  74.             }
  75.            
  76.             //perform an action when the mouse has been released on the button
  77.             public void mouseReleased(MouseEvent e){
  78.                 btn.setText("Mouse Released");
  79.                
  80.                  //reset button background
  81.                 btn.setBackground(defaultBG);
  82.             }
  83.            
  84.             //perform an action when the mouse has been pressed on the button
  85.             public void mousePressed(MouseEvent e){
  86.                 btn.setText("Mouse Pressed");
  87.                 btn.setBackground(Color.YELLOW);
  88.             }
  89.            
  90.             //perform an action after click(press and release) the button
  91.             public void mouseClicked(MouseEvent e){
  92.                 //write button action here
  93.                 count++;
  94.                 repaint();
  95.             }
  96.         });
  97.        
  98.         add(btn);
  99.     }
  100.    
  101.    
  102.     public void paint (Graphics g){
  103.         super.paint(g);
  104.         g.drawString(String.format("Count++ = %d",count),30,45);
  105.  
  106.     }
  107. }