Results 1 to 4 of 4
  1. #1
    AryanArs is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Putting Label Inside Rectangle Problem

    Had draw rectangle and there is button with textfield in Frame...So if i write something in textfield and press button it should be displayed in rectangle...But how....i have not pasted all the codes because i want only the way ...So is there any way;? Help Please

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Putting Label Inside Rectangle Problem

    What GUI library? AWT? Swing? What have you tried? Can you show your code (with code tags)? Have you tried putting a border around the label?

  3. #3
    AryanArs is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: Putting Label Inside Rectangle Problem

    These are the codes..here when i click button the text inside textfield should go inside rectangle
    Components Class
    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    
    
    
    
    import javax.swing.JComponent;
    
    
    
    
    class Components extends JComponent
    {
    	Rectangle rect = new Rectangle(50,80,100,150);
    	private int x;
    	private int y;*
    public *Components()
    {
    
    
    
    
    	class ClickRect implements MouseListener
    	{
    		public void mouseClicked(MouseEvent e) {}
    		public void mouseEntered(MouseEvent e) {}
    		public void mouseExited(MouseEvent e) {}
    		public void mousePressed(MouseEvent e)*
    		{
    			x=e.getX();
    			y=e.getY();
    		}
    		public void mouseReleased(MouseEvent e) {}
    		}
    	
    	
    	MouseListener mln = new ClickRect();
    	addMouseListener(mln);
    	
    	
    	
    	class MovRect implements MouseMotionListener
    	{
    		
    		public void mouseDragged(MouseEvent event)*
    		{
    			int dx = event.getX()-x;
    			int dy = event.getY()-y;
    			rect.x = rect.x + dx;
    			rect.y= rect.y + dy;
    			repaint();
    			x+=dx;
    			y+=dy;
    		
    		}
    		public void mouseMoved(MouseEvent event) {}
    	}
    	
    	MouseMotionListener mml = new MovRect();
    	addMouseMotionListener(mml);
    	
    	}
    	
    * *public void paintComponent(Graphics g)
    * *{
    * * * Graphics2D g2 = (Graphics2D) g;
    * * **
    * * * g2.draw(rect);
    * *}
    }

    This is viewer class
    Java Code:
    import java.awt.BorderLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class Viewer
    {
       
       public static void main(String[] args)
       {
    	  JFrame frame = new JFrame();
    
    	      frame.setSize(600, 600);
    	      frame.setTitle("SuperEllipse Viewer");
    	      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	   
    	      Components component = new Components();
    	      frame.add(component);
    	     
    	      JPanel panel = new JPanel();
    	   
    	      frame.add(panel,BorderLayout.NORTH);
    	
    	      JButton scaleUpButton = new JButton("Click");
    	
    	      
    	      panel.add(scaleUpButton);
    	    JLabel tatti = new JLabel("");
    	      JLabel bulgeLabel   = new JLabel("Click me to put text in rect: ");
    	      JTextField txt = new JTextField(20);    
    	      panel.add(bulgeLabel);
    	      panel.add(tatti);
    	      panel.add(txt);
    	      
    	      frame.setVisible(true);
    	   
       }
      
    }
    Last edited by AryanArs; 03-01-2012 at 12:05 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Putting Label Inside Rectangle Problem

    Where's the code that's supposed to put the text into the GUI? Where do you add an ActionListener to your JButton? Have you read the ActionListener tutorial? If not you will want to read and study it. If so, you'll probably want to read and study it again, and then make an attempt to solve this.

Similar Threads

  1. Replies: 11
    Last Post: 06-28-2011, 12:47 AM
  2. Replies: 2
    Last Post: 04-10-2010, 06:35 PM
  3. centering a label inside a rectangle
    By Brain_Child in forum New To Java
    Replies: 3
    Last Post: 11-19-2009, 05:22 PM
  4. Replies: 9
    Last Post: 07-21-2009, 02:41 AM
  5. How To Add A Jbutton Inside A Rectangle
    By SANDY_INDIA in forum AWT / Swing
    Replies: 9
    Last Post: 07-06-2008, 08:06 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
  •