Results 1 to 3 of 3
  1. #1
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default Need some help explaining "this"

    hey guys can u explain what "this" does in these here examples? whats the merit to using "this"?



    Java Code:
    import javax.swing.*;
    
    import java.awt.event.*;
    import java.awt.*;
    public class makeLayout extends JFrame implements ActionListener{
    
    	private JButton lb;
    	private JButton cb;
    	private JButton rb;
    	private FlowLayout layout;
    	private Container container;
    	
    	public makeLayout(){
    		super("flowLayout test");
    		layout = new FlowLayout();
    		container = getContentPane();
    		setLayout(layout);
    		
    		lb = new JButton("leftblurgh");
    		lb.addActionListener(this);
    		add(lb);
    		rb = new JButton("RAIJTAH!");
    		rb.addActionListener(this);
    		add(rb);
    		cb = new JButton("centeruh!");
    		cb.addActionListener(this);
    add(cb);
    	}
    	public void actionPerformed(ActionEvent event){
    		if(event.getSource()==lb){
    				layout.setAlignment(FlowLayout.LEFT);
    		layout.layoutContainer(container);
    		}
    		else if(event.getSource()==rb){
    			layout.setAlignment(FlowLayout.RIGHT);
    	layout.layoutContainer(container);
    	}
    		else if(event.getSource()==cb){
    			layout.setAlignment(FlowLayout.CENTER);
    	layout.layoutContainer(container);
    	}
    
    	}
    	
    }
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class makeGraphics extends JPanel{
    	
    	public void paintComponent(Graphics g){
    		
    	    super.paintComponent(g);
    		this.setBackground(Color.GREEN);
    		
    		g.setColor(Color.BLUE);
    		g.fillRect(25, 25, 100, 30);
    		
    		g.setColor(new Color(190, 81, 215));
    		g.fillRect(100, 200, 61, 100);
    		
    		g.setColor(Color.RED);
    		g.drawString("dun diddly drawn dis", 25, 25);
    		
    	}
    	
    	
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,781
    Rep Power
    5

    Default Re: Need some help explaining "this"

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default Re: Need some help explaining "this"

    really, cuz i didn't know what google did.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •