Results 1 to 2 of 2
  1. #1
    Mikki is offline Member
    Join Date
    Jun 2012
    Posts
    16
    Rep Power
    0

    Default The text and the buttons are overlapping

    Hi ,
    Can someone help me here. Please find my code below for your reference. When I run this program, the Buttons are on top of the "Welcome New Hire"text. What am I doing wrong. I am not using Swings but awt.

    Java Code:
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Container;;
    
    
    public class NewHireApplet extends Applet implements ActionListener
    {
    	public Button setup = new Button("SETUP");
    	public Button training = new Button("TRAINING");
    	private String s = "Welcome New Hire";
    		
    	public void init()
    	{
    		setBackground(Color.blue.darker());
    		setSize(300, 300);
    		
    		add(setup);
    		setup.addActionListener(this);
    		add(training);
    		training.addActionListener(this);
    		
    		
    		
    	}//end of init()
    	
    	public void paint(Graphics g)
    	{
    		Font myFont1 = new Font("Serif",Font.BOLD,24);
    		Font myFont2 = new Font("Serif",Font.BOLD,12);
    		Dimension d = getSize();
    		
    		//determine x and y co-ordinates
    		FontMetrics fm = g.getFontMetrics();
    		int x = d.width/2 - fm.stringWidth(s)/2;
    		int y = d.height/2 - fm.getHeight();
    		g.setColor(Color.WHITE);
    		g.setFont(myFont1);
    		g.drawString(s , x, 20);
    		g.setFont(myFont2);
    		g.drawString("Welcome new hire to UB. This applet will help you get started.Have fun", x, 280);
    	}//end of paint()
    
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    }
    Last edited by Mikki; 10-04-2012 at 07:17 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,690
    Rep Power
    8

    Default Re: The text and the buttons are overlapping

    I'm not sure what you expect to happen differently. If the text was drawn on top of the button, so would the background of the Applet, and the button would not be visible.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 2
    Last Post: 08-11-2011, 05:02 PM
  2. Replies: 0
    Last Post: 11-30-2010, 05:04 PM
  3. AWT: Painting buttons and text areas in a canvas
    By chappa in forum AWT / Swing
    Replies: 6
    Last Post: 01-09-2010, 02:37 PM
  4. Help with overlapping JPanels
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 03-09-2009, 10:19 PM
  5. Searching a string from a text file using Swing Buttons
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 2
    Last Post: 09-15-2008, 09:50 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
  •