Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with my jFrame code, it wont run

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with my jFrame code, it wont run

    Hello guys, I really hope you can help me with this one, right now I created a new project at eclipse with this code pasted inside a domApli container in a java file, I tried to run it but it wont work, any help you could give me to see what's wrong? Please try to be very specific as I am new at Java and I am still trying to catch some concepts:

     
    package domApli;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Prg2 extends JFrame implements ActionListener{
    	JLabel lblNom;
    	JLabel lblEdad;
    	JTextField txtNom;
    	JTextField txtEdad;
    	JTextArea txtS;
    	JScrollPane scpScroll;
    	JButton btnProcesar;
     
    	public static void main(String[] args) {
    		new Prg2();
    	}
     
    	public Prg2(){
    		getContentPane().setLayout(null);
    		getContentPane().setBackground(Color.orange);
     
    		lblNom=new JLabel("Nombre");
    		lblNom.setBounds(10,10,80,20);
    		getContentPane().add(lblNom);
     
    		lblEdad=new JLabel("Edad");
    		lblEdad.setBounds(10,40,80,20);
    		getContentPane().add(lblEdad);
     
    		txtNom=new JTextField();
    		txtNom.setBounds(100,10,80,20);
    		getContentPane().add(txtNom);
     
    		txtEdad=new JTextField();
    		txtEdad.setBounds(100,40,80,20);
    		getContentPane().add(txtEdad);
     
    		btnProcesar=new JButton("Procesar");
    		btnProcesar.setBounds(190,10,100,20);
    		btnProcesar.addActionListener(this);
    		getContentPane().add(btnProcesar);
     
    		txtS=new JTextArea();
     
    		scpScroll=new JScrollPane(txtS);
    		scpScroll.setBounds(10,70,280,180);
    		getContentPane().add(scpScroll);
     
    		setSize(315,310);
    		setTitle("Prg2");
    		setVisible(true);
    	}
     
    	public void actionPerformed(ActionEvent e){
    		if(e.getSource()==btnProcesar){
    			procesar();
    		}		
    	}
     
    	void imprimir(String cad){
    		txtS.setText("\n"+cad);
    	}
     
    	void borrar(){
    		txtNom.setText("");
    		txtEdad.setText("");
    		txtS.setText("");
    		txtNom.requestFocus();
    	}
     
    	public void procesar(){
    		String nom;
    		int edad;
    		String email;
    		String pwd;
     
    		nom=getNombre();
     
    		if(nom.length()<3){
    			JOptionPane.showMessageDialog(this,"ERROR: Ingrese un nombre con por lo menos 3 letras");
    			borrar();
    		}
    		else{
    			edad=getEdad();
     
    			email=generarEmail(nom,edad);
    			pwd=generarPwd(nom);
     
    			imprimir("Email:\t"+email
    					+"\nPass:\t"+pwd);
    		}
     
    	}
    	public String generarEmail(String nom, int ed){
    		return nom+ed+nom.charAt(0)+"@usmp.pe";
    	}
     
    	int generarAleatorio(int min,int max){
    		return (int)((max-min+1)*Math.random()+min);
    	}
     
    	public String generarPwd(String nom){
    		int n1=generarAleatorio(0,9);
    		int n2=generarAleatorio(0,9);
    		int n3=generarAleatorio(0,9);
    		String letra1=(""+nom.charAt(0)).toUpperCase();
    		String letra2=(""+nom.charAt(1)).toUpperCase();
    		String letra3=(""+nom.charAt(2)).toUpperCase();
     
    		return letra1+n1+letra2+n2+letra3+n3;
    	}
     
    	public String getNombre(){
    		return txtNom.getText();
    	}
     
    	public int getEdad(){
    		return Integer.parseInt(txtEdad.getText());
    	}
     
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Problem with my jFrame code, it wont run

    Our crystal balls are at the cleaners. You will need to provide more information as to what "wont work" means. Copy and paste full error messages.
    Improving the world one idiot at a time!

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problem with my jFrame code, it wont run

    What resource are you using as a guide to learn Java/Swing? The code you've posted uses outdated techniques, and beginners and experts alike should not be using null layouts as a standard practice.

Similar Threads

  1. Replies: 4
    Last Post: April 27th, 2013, 05:10 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. My program will compile but wont run
    By joshp1993 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 23rd, 2012, 07:45 AM
  4. Replies: 6
    Last Post: July 21st, 2011, 07:45 AM
  5. Compliing fine but wont run--please help!
    By Nova in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 12th, 2010, 07:51 PM