Results 1 to 3 of 3
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Can't create JFrame

    EDIT: I was being stupid. I wrote JFrame rather than frame -_-


    It's been a while since I've done any programming and although what I have written at the moment is extremely simple, for some reason I cannot figure out why it doesn't work. Could somebody take a quick look please?

    Eventually I am making a decimal to binary converter, but at the moment I am just trying to build up the basic interface without any calculations.


    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class BinaryConverter{
    
    public static void main(String[] args){
    	JFrame frame = new JFrame();
    	JFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    	JFrame.setVisible(true);
    	JFrame.setLayout(new GridLayout(2, 2));
    	JLabel decimalLabel = new JLabel("Decimal (Base 10)");
    	JFrame.add(decimalLabel);
    	JTextField decimalField = new JTextField();
    	JFrame.add(decimalField);
    	JLabel binaryLabel = new JLabel("Binary (Base 2)");
    	JFrame.add(binaryLabel);
    	JTextField binaryField = new JTextField();
    	JFrame.add(binaryField);
    }
    
    }


    Error:
    BinaryConverter.java:16: error: cannot find symbol
    JFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    ^
    symbol: variable EXIT_ON_CLOSE
    location: class Interface
    BinaryConverter.java:16: error: non-static method setDefaultCloseOperation(int)
    cannot be referenced from a static context
    JFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    ^
    BinaryConverter.java:17: error: non-static method setVisible(boolean) cannot be
    referenced from a static context
    JFrame.setVisible(true);
    ^
    BinaryConverter.java:18: error: non-static method setLayout(LayoutManager) canno
    t be referenced from a static context
    JFrame.setLayout(new GridLayout(2, 2));
    ^
    BinaryConverter.java:20: error: non-static method add(Component) cannot be refer
    enced from a static context
    JFrame.add(decimalLabel);
    ^
    BinaryConverter.java:22: error: non-static method add(Component) cannot be refer
    enced from a static context
    JFrame.add(decimalField);
    ^
    BinaryConverter.java:24: error: non-static method add(Component) cannot be refer
    enced from a static context
    JFrame.add(binaryLabel);
    ^
    BinaryConverter.java:26: error: non-static method add(Component) cannot be refer
    enced from a static context
    JFrame.add(binaryField);
    ^
    8 errors
    Last edited by kkid; 10-02-2013 at 05:03 PM.

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Can't create JFrame

    I think you're just trying to access JFrame in a static way when you should be calling those methods via the object you created:
    Java Code:
    public static void main(String[] args)
    {
    	JFrame frame = new JFrame();
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setVisible(true);
    	frame.setLayout(new GridLayout(2, 2));
    	JLabel decimalLabel = new JLabel("Decimal (Base 10)");
    	frame.add(decimalLabel);
    	JTextField decimalField = new JTextField();
    	frame.add(decimalField);
    	JLabel binaryLabel = new JLabel("Binary (Base 2)");
    	frame.add(binaryLabel);
    	JTextField binaryField = new JTextField();
    	frame.add(binaryField);
    }

  3. #3
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    100
    Rep Power
    0

    Default Re: Can't create JFrame

    you need to say JFrame.EXIT_ON_CLOSE not EXIT_ON_CLOSE

Similar Threads

  1. How can I create a JPanel inside a JFrame?
    By dredd in forum New To Java
    Replies: 1
    Last Post: 04-05-2013, 09:59 PM
  2. Replies: 7
    Last Post: 05-07-2012, 04:38 PM
  3. How to create a JFrame window and create a circle.
    By tehsumo in forum AWT / Swing
    Replies: 3
    Last Post: 02-27-2012, 04:06 PM
  4. Replies: 3
    Last Post: 07-14-2010, 10:34 PM
  5. Trying to create a map for a Zuul game using JFrame
    By alpdog14 in forum New To Java
    Replies: 1
    Last Post: 04-18-2009, 12:24 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
  •