Results 1 to 6 of 6

Thread: Using JFrame

  1. #1
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Using JFrame

    I need some advice.
    I am preparing a small program with a user interface where:
    the user can choose between two buttons
    1- button then leads to a whole series of options
    button 2 - leads to another series.

    is correct to create a java project and immediately do:

    Java Code:
    import javax.swing.JFrame;
    public class TestInterface {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		JFrame frame = new JFrame("ExampleWrite");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		frame.setSize(1200, 600);
    		frame.setLocationRelativeTo(null);
    		
    		frame.setVisible(true);
    	}
    
    }
    and then build the buttons and reasoning on this interface?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Using JFrame

    Sure, seems reasonable to me.

    You might want to put that on the EDT, just to be strict about it:

    Java Code:
    import javax.swing.JFrame;
    public class TestInterface {
     
        public static void main(String[] args) {
            // TODO Auto-generated method stub
             
    SwingUtilities.invokeAndWait(){new Runnable(){
    public void run(){
            JFrame frame = new JFrame("ExampleWrite");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             
            frame.setSize(1200, 600);
            frame.setLocationRelativeTo(null);
             
            frame.setVisible(true);
        }
    }});
    }
    But what you have seems like a reasonable start.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Re: Using JFrame

    thank you. Now I have a question of logic that would help me a lot in understanding what I'm doing.
    a project, in general, not too complex, in java we say that owns for example:
    - 2 buttons
    now:
    - If I press the button "1" the screen changes and shows me a sum of numbers (eg 10 + 5) and I have to insert the result just below.
    - If I press the button "2", the screen changes and I can choose other buttons to press.

    leaving aside for a moment the physical code, how should I proceed to structure the program?

    create a class button and then create from that two objects (one for each button) specialized in a different way?
    and then when I pressed the button "1" and I have to make that the program go get somewhere (or create random) numbers to the tune how should I do it? in another class?
    I am not clear how we should structure things in this language, although I have read several times the basic rules for the construction.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Using JFrame

    It will work fine, but I have a recommendation. Add a JPanel to your JFrame and set the size of your JPanel, not the frame. I would use setPreferredSize(new Dimension(1200,600)) to set the panel size. Then before you set the location to null, do a frame.pack(). Then you can add the buttons to your JPanel.

    Now here is why I do it this way.

    The size of your JFrame includes the borders. So your internal frame space will be less than 1200 x 600 in all dimensions. Those are known values and can be compensated for if you choose. But I find it easier to use a JPanel and then pack the frame. Then the panel space will be exactly 1200 x 600 and the JFrame size will now be slightly larger to accommodate.

    Another reason to use JPanel is if you ever decide to do some graphics, the preferred method is to override paintComponent of a JPanel and do your painting there.

    Regards,
    Jim
    Last edited by jim829; 02-02-2015 at 06:18 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Re: Using JFrame

    excuse me jim what do yuo mean in code?
    to be clear on this window i have to had a button.
    if i write
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    
    public class provaA {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		JFrame principale = new JFrame("Improve Your Ability To Count");
    		principale.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		principale.setSize(1200, 600);
    		principale.setLocationRelativeTo(null);		
    		principale.setVisible(true);
    		
    		JButton pulsanteUTENTI = new JButton("UTENTI");
    		
    		principale.add(pulsanteUTENTI);
    	}
    }
    the name of the button will be displayed in the center of the window BUT all window (1200*600) act like a button. why?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Using JFrame

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Click in JFrame Titlebar sets the JFrame Opaque
    By tim8w in forum AWT / Swing
    Replies: 1
    Last Post: 02-02-2013, 07:29 AM
  2. Replies: 3
    Last Post: 11-27-2011, 05:18 AM
  3. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  4. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  5. Replies: 4
    Last Post: 04-22-2009, 11:02 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
  •