Results 1 to 7 of 7
  1. #1
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default 30 line simple gui, simple error, need help

    I am starting on a project. I have a small problem. When i "javac" the code, it displays an "inner classes cannot have static declarations". If you could help me with this (to get it to display the GUI), that would be great. Thanks, and I'll get back to you tomorrow.
    Java Code:
    import javax.swing.*;  
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    	public class Risk extends JPanel{
    int LeftNumber = 0;
    int RightNumber = 0;
    public JButton enter;
    public Risk() {
    JFrame frame = new JFrame("Risk");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    enter = new JButton("Enter");
    add(enter);
    frame.pack();
    setVisible(true);
    }
    class ShowRisk{
    public static void main(String args[]) {
     new Risk();
    }
    }
    }
    Thanks,
    cc11rocks

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Your indentation is all over the place:

    Java Code:
    import javax.swing.*;  
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    
    public class Risk extends JPanel {
        int LeftNumber = 0;
        int RightNumber = 0;
        public JButton enter;
    
        public Risk() {
            JFrame frame = new JFrame("Risk");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            enter = new JButton("Enter");
            add(enter);
            frame.pack();
            setVisible(true);
        }
        
        class ShowRisk{
            public static void main(String args[]) {
                new Risk();
            }
        }
    }

    I have reposted it to bring out the point that you are declaring ShowRisk as an inner class - a class within a class (each instance of the Risk class will have its own ShowRisk type defined). And the compiler is telling you that such inner classes cannot have have static methods or fields. The whys and wherefores of this are probably unimportant as, almost certainly, you have no reason to declare an inner class.

    To run a program that uses a gui you would do well to follow the same technique as that employed in Oracle's Tutorial in the section Compiling and Running Swing Programs. You should also adopt the habit of using standard Java naming conventions. Variables start with a lowercase letter, so leftNumber etc.

    -------------------------------

    The only bit that need change from the example used in the Tutorial is that you would add an instance of your Risk panel, not of JLabel. Notice that you end up with two classes: the Risk panel class, and the application class with the main() method.
    Last edited by pbrockway2; 02-05-2011 at 06:13 PM.

  3. #3
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default Some things Fixed and Still need help

    New Code:
    Java Code:
    package JavaCrap;
     //java start.HelloWorldSwing
    import javax.swing.*;  
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    	public class Risk extends JPanel{
    int leftNumber = 0;
    int rightNumber = 0;
    public JButton enter;
    public Risk() {
    JFrame frame = new JFrame("Risk");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    enter = new JButton("Enter");
    add(enter);
    frame.pack();
    setVisible(true);
    }
    class ShowRisk{
    public static void main(String args[]) {
     new Risk();
    }
    }
    }
    Could you please fix the error for me? I am very new to java and do not know how to fix it.
    Thanks,
    cc11rocks

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Did you look at the HelloWorldSwing.java example?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I see from your source that you did.

    Here's the example stripped of most of its comments and with the change I suggested ("add an instance of your Risk panel, not of JLabel")

    Java Code:
    import javax.swing.*;        
    
    public class RiskApp {
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("The Risk App!");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //JLabel label = new JLabel("Hello World");
            //frame.getContentPane().add(label);
            Risk riskPanel = new Risk();
            frame.getContentPane().add(riskPanel);
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

    I have also removed the package and changed the name of the class (it would be saved in a .java file of the same name).

    The only other change would be to alter Risk.java to remove both the JFrame stuff and that bogus inner class. (and also take it out of the package). This would result in something like:

    Java Code:
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    public class Risk extends JPanel {
        int leftNumber = 0;
        int rightNumber = 0;
        public JButton enter;
    
        public Risk() {
            enter = new JButton("Enter");
            add(enter);
        }
    }

    Now compile both .java files. And run RiskApp.

    (this code is not compiled - expect errors as I've only had one coffee)

    ---------------------------------

    The intent behind the complication of having two classes is that a class should represent just one type of thing.

    * You have a Risk which is a sort of panel. It does panel-like things, like have buttons added to it. It doesn't mess with the JFrame that visually encloses it.

    * You have a RiskApp. It does application-like things, like create a frame and set its title. And create a Risk panel and put it into the frame.

  6. #6
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default Thank you very much!

    Thank you very much! It works perfectly! :D
    With MUCH thanks,
    cc11rocks

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're welcome.

    The RiskApp class is basically a piece of boilerplate that can be reused as needed. (Or extended if an application uses multiple panels). The Risk class is now cleared of the clutter and you can concentrate on its logic.

Similar Threads

  1. Error with my simple GUI code
    By Menre in forum New To Java
    Replies: 6
    Last Post: 06-28-2010, 10:49 PM
  2. No Such Method Error - simple
    By n00b in forum New To Java
    Replies: 9
    Last Post: 05-04-2010, 04:05 PM
  3. Simple Error ???
    By darkblue24 in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 08:15 AM
  4. compiling error should be a simple fix.
    By bottlecap in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 06:07 AM
  5. New simple application using a simple database
    By webbusiness23 in forum New To Java
    Replies: 9
    Last Post: 08-03-2009, 02:55 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
  •