Closed Thread
Results 1 to 5 of 5

Thread: JFrame help

  1. #1
    agnl666's Avatar
    agnl666 is offline Programmer
    Join Date
    Feb 2010
    Location
    Halifax
    Posts
    163
    Blog Entries
    2
    Rep Power
    0

    JFrame help

    Hi. In class we started using JOptionPane, which is fine and all but I want to be able to create my own window in java. I know you use JFrame but i don't know where to start. Could somone post a link to a tutorial or a code snipit I could look/play with? Thanks.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,879
    Rep Power
    24

    Re: JFrame help

    Code:
    public class MyClass
       JFrame frame;
       JLabel label;
       JButton button;
    
    
      public MyClass(){
        frame = new JFrame("title");
        initComponents();
        frame.getContentPane().add(label);
        frame.getContentPane().add(button, BorderLayout.SOUTH);
        frame.setSize(800,600);
        frame.pack();
        frame.setVisible(true);
      }
      
      public void initComponents(){
        button = new JButton("click me");
        button.addActionListener(new ActionListener( ){
           public void actionPerformed(ActionEvent e){
               label.setText("hello world");
           }
        });
    
        label = new JLabel("empty, click button");
       }
    the contentPane of the Jframe is by default a borderLayout:

    If you don't specify where the component has to be added it's by default the center, and it will stretch in all 4 directions.
    Adding another thing onto the center will 'overwrite' the previous. If you want to put multiple objects in the center you'll need to add the multiple objects to a JPanel, and the JPanel to the center.
    JPanel and contenPane's layout can be changed with setLayout.
    possible layouts:
    http://java.sun.com/docs/books/tutor...ut/visual.html

    Didn't test the code. But i think it's correct

    Many people often let the class "extends JFrame". But i don't see the point in extending when nothing is overridden.

  4. #3
    R3.RyozKidz Guest

    Re: JFrame help

    i saw some of my text book's codes did something like this . The code didn't use JPanel but directly add the component to the JFrame .
    Any different ?

  5. #4
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,879
    Rep Power
    24

    Re: JFrame help

    you mean isntead of frame.getContentPane().add(label); they do frame.add(label); ?

    I'd always use the contentPane as that's what it's made for. When the JFrame was released i guess it wasn't even possible but since 1.5 it seems that the 2 should do the same...
    Swing

    With the 1.4.2 release we provided two new look and feels for Swing: XP and GTK. Rather than taking a break, in 5.0 we're providing two more look and feels: Synth, a skinnable look and feel, and Ocean, a new theme for Metal. Beyond look and feels, we've added printing support to JTable, which makes it trivial to get a beautiful printed copy of a JTable. Lastly, after seven years, we've made jFrame.add equivalent to jFrame.getContentPane().add().
    Got it from this website.

    I suppose it's ok to just use JFrame.add(..) but just know that in fact you're putting it on a contentPane.


    Or did you mean that the code in your book always uses a panel, and i didn't?
    The contentPane is basicly the same as a JPanel, it's the root panel of the JFrame. As a JPanel you can also set it's layout and add components. I think you can change the default contentPane with any of your own classes as long it extends JPanel using the setContentPane() method.

    If the book first adds stuff to the JPanel and does then JFrame.getContentPane() . add(JPanel) ;
    It puts the JPanel in the center of the contentpane's borderlayout which cuases the JPanel to stretch out in all 4 directions. If they only use 1 JPanel, I don't really see a good reason why i would put a JPanel on a contentPane.

  6. #5
    agnl666's Avatar
    agnl666 is offline Programmer
    Join Date
    Feb 2010
    Location
    Halifax
    Posts
    163
    Blog Entries
    2
    Rep Power
    0

    Re: JFrame help

    Thank you for the help. I'm going to fool around with the JFrame Library and post more if I have anymore questions. I'm sorry I didn't respond with a thank you sooner but I have been working a lot lately.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How Can I Make My JFileChooser Look Like My JFrame ?
    By xxxxjayxxx in forum Java Help
    Replies: 1
    Last Post: 05-04-2011, 11:45 AM
  2. Need help with extended JFrame
    By TheCompBoy in forum Java Help
    Replies: 11
    Last Post: 04-10-2011, 01:55 AM
  3. new JFrame position
    By alix in forum Java Help
    Replies: 3
    Last Post: 01-06-2011, 02:23 PM
  4. JFrame
    By R3.RyozKidz in forum Java Help
    Replies: 4
    Last Post: 06-13-2010, 06:42 PM
  5. Topless JFrame
    By ld_pvl in forum Java Help
    Replies: 9
    Last Post: 07-19-2009, 03:46 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts