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: GUI problem

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default GUI problem

    Hello everyone

    I have this code and whenever i run it i get this error message. I tried to fix it but i dont know what is wrong with it.
    HTML Code:
    package D;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class D extends JFrame implements ActionListener {
    
        JButton Exit, Name;
        String fn,ln;
        
        public D() {
        
            this.setSize(400,300);
            this.setTitle("Celsius and Fahrenheit conversion");
            this.setVisible(true);
            this.setLocationRelativeTo(null);
            this.setLayout(new FlowLayout());
            
            Container content = getContentPane();
            
            Exit = new JButton("Close Program");
            Exit.setActionCommand("Exit");
            
            
            Name = new JButton("Enter Name");
            Name.setActionCommand("Name");
            
            Exit.addActionListener(this);
            Name.addActionListener(this);
            
            content.add(Exit, BorderLayout.NORTH);
            content.add(Name, BorderLayout.WEST);
        }
        
        public void Actions(ActionEvent Action) {
            if(Action.getActionCommand().contains("Exit")) {
                System.exit(0);
            }
            
            if(Action.getActionCommand().equals("Name")) {
                
                fn = JOptionPane.showInputDialog("Enter First Name: ");
                ln = JOptionPane.showInputDialog("Enter Last Name: ");
                
                JOptionPane.showMessageDialog(null, "Hello " + fn + " " + ln);
            }
        }
        public static void main(String[] args) {
            D();
        }
    }
    Error Message:-
    HTML Code:
    run:
    java.lang.NoClassDefFoundError: d/D (wrong name: D/D)
    	at java.lang.ClassLoader.defineClass1(Native Method)
    	at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
    	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    	at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    	at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    	at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:472)
    Exception in thread "main" Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: GUI problem

    Though this probably isn't it, I thought to implement ActionListener, you had to override the method actionPerformed() and it appears that you don't. However, I've done this before and it doesn't give that error message you posted when you do it.

    I'm going to run that.

    Found another problem.

    It would be

    new D();

    not

    D();

    That also is causing a problem.

    I changed the name of the method Actions to actionPerformed as you have to have an actionPerformed method in order to have the class implement ActionListener().

    I think with you calling

    D(); It was confused as to what that meant.

    I've gotten it to work now:

    package D;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class D extends JFrame implements ActionListener {
     
        JButton Exit, Name;
        String fn,ln;
     
        public D() {
     
            this.setSize(400,300);
            this.setTitle("Celsius and Fahrenheit conversion");
            this.setVisible(true);
            this.setLocationRelativeTo(null);
            this.setLayout(new FlowLayout());
     
            Container content = getContentPane();
     
            Exit = new JButton("Close Program");
            Exit.setActionCommand("Exit");
     
     
            Name = new JButton("Enter Name");
            Name.setActionCommand("Name");
     
            Exit.addActionListener(this);
            Name.addActionListener(this);
     
            content.add(Exit, BorderLayout.NORTH);
            content.add(Name, BorderLayout.WEST);
        }
     
        public void actionPerformed(ActionEvent Action) {
            if(Action.getActionCommand().contains("Exit")) {
                System.exit(0);
            }
     
            if(Action.getActionCommand().equals("Name")) {
     
                fn = JOptionPane.showInputDialog("Enter First Name: ");
                ln = JOptionPane.showInputDialog("Enter Last Name: ");
     
                JOptionPane.showMessageDialog(null, "Hello " + fn + " " + ln);
            }
        }
        public static void main(String[] args) {
           new D();
        }
    }
    Last edited by javapenguin; October 21st, 2011 at 09:10 PM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: GUI problem

    Suggested reading:
    Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    How to write an ActionListener
    Code Conventions for the Java Programming Language