Results 1 to 2 of 2
  1. #1
    rahul405804 is offline Member
    Join Date
    Apr 2012
    Posts
    14
    Rep Power
    0

    Default Not able to place image in Jframe in proper manner

    Here is my code:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import com.fjsynthesizer.service.DatabaseService;
    
    class Login extends JFrame implements ActionListener
    {
        JButton login,register;
        JPanel  panel;
        JLabel label1,label2;
        public JTextField  text1,text2;
        RegisterAction regAction = null;
         
         public Login()
         {
          	
        	try
        	{
        			 
            	label1 = new JLabel();
            	regAction = new RegisterAction();
            	
                label1.setText("Username:");
                label1.setOpaque(true);
                
                text1 = new JTextField(15);
                text1.setOpaque(true);
    
                label2 = new JLabel();
                label2.setText("Password:");
                label2.setOpaque(true);
                
                
                text2 = new JPasswordField(15);
                text2.setOpaque(true);
         
                login=new JButton("Login");
                login.setOpaque(true);
                
                register = new JButton("Register");
                register.setOpaque(true);
                
                JPanel mainPanel = new JPanel(new BorderLayout());
                JPanel extraPanel = new JPanel(new FlowLayout());
                
                extraPanel.setPreferredSize(new Dimension(100,100));
                
                extraPanel.add(label1);
                extraPanel.add(text1);
                extraPanel.add(label2);
                extraPanel.add(text2);
                extraPanel.add(login);
                extraPanel.add(register);
                extraPanel.setBackground(Color.CYAN);
              
                
                mainPanel.add(extraPanel,BorderLayout.CENTER);
                
                setContentPane(mainPanel);
                setTitle("File Jamming Synthesizer");
                login.addActionListener(this);
                register.addActionListener(regAction);
               // this.getContentPane().add(new ImagePanel("img1.png"));
                
                   		
        	}
           	
        	catch(Exception e)
        	{
        		
        		System.out.println(e);
        		
        	}
       }
      
         
         
         
          
     public void actionPerformed(ActionEvent ae)
     {
           String username=text1.getText();
           String password=text2.getText();
           
           if(username.length()==0 || password.length()==0)
           {
        	   
        	   JOptionPane.showMessageDialog(null, "Username/Password field should not be empty!");
        	   
           }
           
           
           else
           {
                   	   
        	   boolean userExistence = new DatabaseService().validateLogin(username, password);
               
               if(userExistence==false)
               {
            	   JOptionPane.showMessageDialog(null, "Invalid Login!");
            	   
               }
                      
               else
               { 
            	       if(username.equals("admin"))
            	       {
            	    	       	new AdminHome(username).setVisible(true);	  
                	          	setVisible(false);
            		   
            	        }
            	       	   
            	       else
            	       {
            		           new UserHome(username).setVisible(true);
            		           setVisible(false);
                	      		   
            	       }
            	       	   
               }
        	   
        	   
        	   
           }
         
      }
     
    
     class RegisterAction implements ActionListener
     {
    
     	   public void actionPerformed(ActionEvent e) 
     	   {
     		// TODO Auto-generated method stub
     		 		   
     		   new Register().setVisible(true);
     		   setVisible(false);
     	     				
     		
     	   }
     
        }
    
    
    }
    
    
    
     class LoginDemo
     {
    	 
        public static void main(String args[])
        {
           try
           {
              Login frame=new Login();
             //frame.setBounds(2, 5, 300, 300);
              frame.setSize(300,150);
              frame.setVisible(true);
           }
           
          catch(Exception e)
          {
        	 JOptionPane.showMessageDialog(null, e.getMessage());
        	  
          }
          
        }
    }

    This is the screenshot where I didn't add any image to window:
    Not able to place image in Jframe in proper manner-capture211.jpg

    And here is the screenshot if I try to add an image to it.
    (the line of code is commented out in the above code)

    Not able to place image in Jframe in proper manner-capture34.jpg

    Can anyone suggest a way so that I can put all the items in one place and make it attractive?
    I have tried out several layouts also. But nothing gives the solution.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default Re: Not able to place image in Jframe in proper manner

    What's wrong about the image placement?
    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: 04-29-2011, 03:44 AM
  2. Add an image to JFrame
    By Eranga in forum AWT / Swing
    Replies: 4
    Last Post: 02-01-2010, 03:09 PM
  3. Replies: 0
    Last Post: 03-10-2009, 09:55 AM
  4. How to place a value into a Label on another Jframe
    By kewlgeye in forum New To Java
    Replies: 23
    Last Post: 06-11-2008, 08:15 AM
  5. how to place an image in an applet
    By paty in forum Java Applets
    Replies: 2
    Last Post: 08-04-2007, 05:15 PM

Posting Permissions

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