Getting JFrame to appear on PC

What do I have to download to get my JFrame to appear on my PC?

The following code, taken from FrameDemo, is a typical example of the code used to create and set up a frame.
//1. Optional: Specify who draws the window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//2. Create the frame.
JFrame frame = new JFrame("FrameDemo");
//3. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//4. Create components and put them in the frame.
//...create emptyLabel...
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//5. Size the frame.
frame.pack();
//6. Show it.
frame.setVisible(true);
Tutorial at:
http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html

Similar Messages

  • Learning Swing - Can't get JLabel to appear in JFrame

    Hi,
    I'm a complete beginner to Java. I'm taking a course that's moving way too fast, in my opinion, and we're currently studying Swing. I've likely missed a lot of basic basic Java fundamentals due to the speed of this class, so if I'm making a ridiculous mistake, please let me know.
    I'm trying to create the following:
    +"Create a GUI interface using the Swing API. Use the JOptionPane class to create a dialog box to ask the+
    +user for the University Name. Create a JFrame that has a label with the University name and create your+
    +own logo. The window should have the capability of inputting first name , last name, and id for a student+
    +in text fields. Once the id is entered open another dialog box with the student information."+
    I'm pretty much done, I'm just having issues getting the output from the first JFrame to appear in the second JFrame. When I create a new JFrame, the title I specify appears, but the normal syntax for a JLabel just isn't working and I'm not sure why.
    I suspect it has something to do with the general structure of my program rather than my syntax. ...I know I'm missing a lot of fundamentals that I should have developed, thanks to the way the course I'm in is structured.
    I'm posting two .java files. The first, called GUIFrame.java is the class that basically does everything, it calls JOptionPane, opens the first and second JFrames, and uses ActionEvent and ActionListener to read text from the text fields. The second .java file is called GUITest.java and all it does is instantiate GUIFrame and sets the JFrame parameters.
    If anyone has any suggestions, at all, about how to fix this, or especially how to structure these better... Any suggestion would be greatly appreciated.
    CODE:
    import javax.swing.JFrame; //provides basic window features
    import javax.swing.JOptionPane; //simple GUI input/output
    import javax.swing.JLabel; //displays text and images
    import javax.swing.Icon; //interface used to manipulate images
    import javax.swing.ImageIcon; //loads images
    import java.awt.FlowLayout; // specifies how components are arranged
    import javax.swing.SwingConstants; //common constants used with Swing
    import javax.swing.JTextField;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    public class GUIFrame extends JFrame
         private JLabel line1;
         private JLabel line2;
         private JLabel line3;
         private JLabel line4;
         private JTextField textField1;
         private JTextField textField2;
         private JTextField textField3;
         //This method prompts the user in a JOptionPane for the university's name.
         public static String obtainUniversityName()
              //Obtain user input from JOptionPane input dialog for universityName.
              String uName = JOptionPane.showInputDialog(null,"Enter the University's name","University Name",JOptionPane.QUESTION_MESSAGE);
              return uName;     
         //This method creates an image icon if possible, or it will print an error and return null
         protected ImageIcon createImageIcon(String path,String description)
              java.net.URL imgURL = getClass().getResource(path);
              if (imgURL != null)
                   return new ImageIcon(imgURL, description);
              } else {
                   System.err.println("Couldn't find file: " + path);
                   return null;
         public GUIFrame()
              //Create a new JFrame for the university info to be placed in
              //The title of the window is also declared here
              super(obtainUniversityName());
              //Sets way things are arranged
              setLayout(new FlowLayout());
              //Grabs logo and aligns it with "Welcome to the University"
              ImageIcon logo = createImageIcon("girl.gif","This is a logo");
              line1 = new JLabel("Welcome to the University", logo, JLabel.CENTER);
              add(line1);
              //"First Name" + Text Field
              line2 = new JLabel("First Name");
              add(line2);
              textField1 = new JTextField(10);
              add(textField1);
              //"Last Name" + Text Field
              line3 = new JLabel("Last Name");
              add(line3);
              textField2 = new JTextField(10);
              add(textField2);
              //"Student ID" + Text Field
              line4 = new JLabel("Student ID");
              add(line4);
              textField3 = new JTextField(10);
              add(textField3);
              //Register event handlers
              TextFieldHandler handler = new TextFieldHandler();
              textField1.addActionListener(handler);
              textField2.addActionListener(handler);
              textField3.addActionListener(handler);
         private class TextFieldHandler implements ActionListener
              public void actionPerformed(ActionEvent event)
                   String fname = "";
                   String lname = "";
                   String stuid = "";
                   //When user presses enter in any text field, these set values for input into text field
                   if(event.getSource()==textField1)
                        fname = String.format("First Name: %s", event.getActionCommand());
                             else if(event.getSource()==textField2)
                                  lname=String.format("Last Name: %s", event.getActionCommand());
                                       else if(event.getSource()==textField3)
                                            stuid=String.format("Student ID: %s", event.getActionCommand());
                                                 else
                                                      System.out.println("ERROR! You didn't fill in atleast one of the text boxes...");
                   //Creates a new JFrame with title "Student Info"          
                   JFrame frame2 = new JFrame("Student Info");
                   //Sets way things are arranged
                   setLayout(new FlowLayout());
                   //Temporary - Tests to see that processing of text field events occurs correctly
                   System.out.println(fname);
                   System.out.println(lname);
                   System.out.println(stuid);               
                   //Puts data into JLabels for display in new JFrame
                   JLabel label1=new JLabel(fname);
                   add(label1);
                   JLabel label2=new JLabel("Can you see this!?");
                   add(label2);          
                   JLabel label3=new JLabel(stuid);
                   add(label3);
                   //Sets parameters, size, close, visibility, etc, for new JFrame
                   frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   frame2.setSize(275,210);
                   frame2.setVisible(true);
              }//end void actionPerformed
         }//end TextFieldHandler
    }//end GUIFrame
    ==================================================================================
    import javax.swing.JFrame; //provides basic window features
    public class GUITest
         public static void main(String args[])
              //Calls on GUIFrame class. Instantiates.
              GUIFrame guiFrame = new GUIFrame();
              //Set the window to exit when closed
              guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              //Size the window
              guiFrame.setSize(275,210);
              //Center the window on the screen
              guiFrame.setLocationRelativeTo(null);
              //Make the window visible
              guiFrame.setVisible(true);
    }

    Thanks so much for the quick response. Yeah, I guess I'm a bit oblivious, I guess I missed that it was supposed to be another JOptionPane.
    I fixed that and got rid of all the second JFrame stuff. Thank you.
    And you're right, now that I can see my output, it's obvious I have serious issues in the actionPerformed() method with the ActionListener. I guess it's just a simple question of how to take what's in all three boxes and output to JOptionPane. Simple logic I guess.
    I don't completely understand the ActionListener. I've read my textbook about it and looked at a few online resources, but I don't have a complete understanding.
    How then, would I be able to use the ActionListener to get the text from all three text fields?
    The way it is now, as you correctly predicted, it waits for enter to be inputted from each text field and whichever text field you hit enter in, despite what's in the other text fields, is the only thing that is outputted to the string in the JOptionPane.
    Do you have a suggestion for that?
    Thanks again.
    CODE:
    import javax.swing.JFrame; //provides basic window features
    import javax.swing.JOptionPane; //simple GUI input/output
    import javax.swing.JLabel; //displays text and images
    import javax.swing.Icon; //interface used to manipulate images
    import javax.swing.ImageIcon; //loads images
    import java.awt.FlowLayout; // specifies how components are arranged
    import javax.swing.SwingConstants; //common constants used with Swing
    import javax.swing.JTextField;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    public class GUIFrame extends JFrame
         private JLabel line1;
         private JLabel line2;
         private JLabel line3;
         private JLabel line4;
         private JTextField textField1;
         private JTextField textField2;
         private JTextField textField3;
         //This method prompts the user in a JOptionPane for the university's name.
         public static String obtainUniversityName()
              //Obtain user input from JOptionPane input dialog for universityName.
              String uName = JOptionPane.showInputDialog(null,"Enter the University's name","University Name",JOptionPane.QUESTION_MESSAGE);
              return uName;     
         //This method creates an image icon if possible, or it will print an error and return null
         protected ImageIcon createImageIcon(String path,String description)
              java.net.URL imgURL = getClass().getResource(path);
              if (imgURL != null)
                   return new ImageIcon(imgURL, description);
              } else {
                   System.err.println("Couldn't find file: " + path);
                   return null;
         public GUIFrame()
              //Create a new JFrame for the university info to be placed in
              //The title of the window is also declared here
              super(obtainUniversityName());
              //Sets way things are arranged
              setLayout(new FlowLayout());
              //Grabs logo and aligns it with "Welcome to the University"
              ImageIcon logo = createImageIcon("girl.gif","This is a logo");
              line1 = new JLabel("Welcome to the University", logo, JLabel.CENTER);
              add(line1);
              //"First Name" + Text Field
              line2 = new JLabel("First Name");
              add(line2);
              textField1 = new JTextField(10);
              add(textField1);
              //"Last Name" + Text Field
              line3 = new JLabel("Last Name");
              add(line3);
              textField2 = new JTextField(10);
              add(textField2);
              //"Student ID" + Text Field
              line4 = new JLabel("Student ID");
              add(line4);
              textField3 = new JTextField(10);
              add(textField3);
              //Register event handlers
              TextFieldHandler handler = new TextFieldHandler();
              textField1.addActionListener(handler);
              textField2.addActionListener(handler);
              textField3.addActionListener(handler);
         private class TextFieldHandler implements ActionListener
              public void actionPerformed(ActionEvent event)
                   String fname = "";
                   String lname = "";
                   String stuid = "";
                   //When user presses enter in any text field, these set values for input into text field
                   if(event.getSource()==textField1)
                        fname = String.format("First Name: %s", event.getActionCommand());
                             else if(event.getSource()==textField2)
                                  lname=String.format("Last Name: %s", event.getActionCommand());
                                       else if(event.getSource()==textField3)
                                            stuid=String.format("Student ID: %s", event.getActionCommand());
                                                 else
                                                      System.out.println("ERROR! You didn't fill in atleast one of the text boxes...");
                   String out = fname + "\n" + lname + "\n" + stuid;
                   JOptionPane.showMessageDialog(null,out);
              }//end void actionPerformed
         }//end TextFieldHandler
    }//end GUIFrame
    import javax.swing.JFrame; //provides basic window features
    public class GUITest
         public static void main(String args[])
              //Calls on GUIFrame class. Instantiates.
              GUIFrame guiFrame = new GUIFrame();
              //Set the window to exit when closed
              guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              //Size the window
              guiFrame.setSize(275,210);
              //Center the window on the screen
              guiFrame.setLocationRelativeTo(null);
              //Make the window visible
              guiFrame.setVisible(true);
    }Edited by: heathercmiller on Apr 5, 2008 8:56 AM

  • How do I only get certain questions appear in the quiz results?

    I have created a course through Captivate 6, throughout the course there are questions to guage the learners attention. There is also a quiz at the end. However, captivate includes on the questions throughout the course on the quiz results which I do not want to happen.
    Can anyone advise how I can only get certain questions appear on the quiz results please?

    Branch aware turns off the playbar in 6 (not in 7 any more, at least not for me).
    Could you explain more in detail what you want, because I don't understand it. You want the user to answer all questions, but the score should only show what? If you don't want some questions to have a score added to the Quiz total, you can indicate that in the Properties of that Question slide.
    And please, tell the exact number (3 versions of 6), and also if you have to report to a LMS?
    Lilybiri

  • Some USBs don't appear in Finder. They appear in Disk Utilities so how do I get them to appear in Finder?

    Some flash drives/USBs don't appear in Finder. But they appear in Disk Utilities so how do I get them to appear in Finder?

    If you've never used them before (they are new) then they are not formatted for Macs.
    Drive Partition and Format
    1. Open Disk Utility in your Utilities folder.
    2. After DU loads select your hard drive (this is the entry with the mfgr.'s ID and size) from the left side list. Click on the Partition tab in the DU main window.
    3. Under the Volume Scheme heading set the number of partitions from the drop down menu to one. Click on the Options button, set the partition scheme to GUID then click on the OK button. Set the format type to Mac OS Extended (Journaled.) Click on the Apply button and wait until the process has completed.
    4. Select the volume you just created (this is the sub-entry under the drive entry) from the left side list. Click on the Erase tab in the DU main window.
    5. Set the format type to Mac OS Extended (Journaled.) Click on the Security button, check the button for Zero Data and click on OK to return to the Erase window.
    6. Click on the Erase button. The format process can take up to several hours depending upon the drive size.
    When you want to remove an external device you must Eject it:
    First, select the Desktop icon of the external disk. CTRL- or RIGHT-click and select Eject from the context menu.

  • When browsing a new library that I created, the browser shows dotted lines around grey rectangles, no images. When I double click on a rectangle the image appears. How do I get images to appear in the browser rectangles?

    When browsing a new library that I created and exported onto an external hard drive, the browser shows dotted lines around grey rectangles, no images. When I double click on a rectangle, the image appears, but all the other rectangles remain empty - no image. How do I get images to appear in the browser rectangles? I am viewing this on a second computer (an older intel duo iMac), not the one I created the library on (a MacBook Pro). Both computers have Aperture 3.2.4 installed. When I return the external to the MacBook, all images appear in browser rectangles. What's happening on the iMac?

    You may have a problem with the permissions on your external volume. Probably you are not the owner of your library on the second mac.
    If you have not already done so, set the "Ignore Ownership on this Volume" flag on your external volume. To do this, select the volume in the Finder and use the Finder command "File > Get Info" (or ⌘I).
    In the "Get Info" panel disclose the "Sharing & Permissions" brick, open the padlock, and enable the "Ignore Ownership on this Volume" flag. You will have to authentificate as administrator to do this.
    Then run the "Aperture Library First Aid Tools" on this library and repair the permissions. To launch the "First Aid Tools" hold down the ⌥⌘-key combination while you double click your Aperture Library. Select "Repair Permissions" and run the repair; then repeat with "Repair Database". Do this on the omputer where you created the library and where you can see the thumbnails.
    Then check, if you now are able to read the library properly on your iMac.
    Regards
    Léonie

  • My iPhone is not being detected on my laptop itunes. How do I get it to appear under devices?

    My iPhone is not being detected on my laptop itunes. How do I get it to appear under devices?

    iPhone, iPad, or iPod touch not appearing in iTunes

  • I cannot get apps  to appear in my library menu so i can update them through iTunes. Happened since update to 11.0.2.Have tried all the suggested fixes such as making  the sidebar appear etc New update is 'nasty'

    As stated no matter how hard i try i CANNOT get apps to appear in my sidebar library menu so i can update them through itunes.Has only happened since last update of itunes .Really wish i hadn't updated as i have never had an issue b4.Spent most of day on fone to apple yesterday as the update created an issue that meant i could not sync at all.Anyway thanks to 'james' in arizona we sorted the first piece of trouble the update created .(sortof).
    Not sure why it has to be messed with.Well i spose it is progress!!!
    I've looked at lots of posts re this and tried all the suggestions but just cannot get the magic 'apps' category to appear in my sidebar in itunes???
    One suggestion was that the apps if clicked on would have an 'update ' button but all i can do is delete them
    Any thoughts would be greatly appreciated
    (running osx 10.7.5 on MBP -slightly tricky cos i also run windows in a 'bootcamped' partition.Mind u itunes etc is in mac partition and has been working fine for the last 10 years!!!!!!!!!)

    I'm going to assume that you use only one Apple ID so the two things that I can think of are to quite all processes and restart all devices and then try to sync those apps again. Quit iTunes, restart your computer and the iPad and then try to sync the apps again.
    The other thing would be to try to reset your iTunes account by signing in and out of the account on the iPad. Settings>Store>Apple ID - tap the ID and sign out. Restart the iPad and then sign in to your account and try again.

  • How do U get icloud to appear in the finder

    in Mountain Lion you can save text edit files to the icloud. How do I get iCloud to appear in the sidebar in Finder. On icloud only iwork is displayed for files.  I usedropbox and it looks just like any other folder in Finder. Does iCloud behave the same?

    Thanks. It began to dawn on me that this was the case as I try to poke around for ways to do this.  I will continue to use Dropbox which works fantastically. Just wonder why Apple would come up with such a limited option.

  • I just purchased a movie from the iTunes store using iTunes ver. 11.0. The movie downloaded correctly is there on my hard drive but does appear in the iTunes library. How do I get it to appear in the iTunes Movie Library?

    I just purchased a movie from the iTunes store using iTunes ver. 11.0. The movie downloaded correctly is there on my hard drive but does appear in the iTunes library. How do I get it to appear in the iTunes Movie Library?

    Try Handbrake (http://handbrake.fr/), it's good and free

  • I have 2 new email accounts added to my Imac. How do I get them to appear on the macbook and my iPhone. The computers are with maverick and the iPhone has the latest iOS 7.0.3. Thanks

    I have added 2 new email accounts to my Imac. How do I get them to appear on my macbook pro and my iPhone. The computers are with maverick and the iPhone has the latest iOS 7.0.3. Thanks

    Connect the device to the computer and iTunes and follow the instructions on this support document. http://support.apple.com/kb/HT1808

  • I downloaded a new APP to my iPhone it appears on my screen but can't get it to appear on my wife's iPhone screen.  Her screens were initially full of Apps, I later made room for the new APP but can't get it to appear even after  making room for the new A

    I downloaded a new APP to my iPhone it appears on my screen but can't get it to appear on my wife's iPhone screen.  Her screens were initially full of Apps, I later made room for the new APP but can't get it to appear even after  making room for the new APp.  It can be opened, but I want the APP to be on her iPhone screen as well as on mine.  Help, please.

    Is the app downloaded on her phone also?

  • When I download photos from my PC to my iPad I can't consistently get them to appear in order. Any suggestions

    When I download photos from my PC to my iPad I cannot consistently get them to appear in the order I want them even after changing their file names and arranging them in the order I want. Any suggestions would be greatly appreciated

    Maybe this will help you; http://support.apple.com/kb/HT4221
    Best way is to use another App, something like Photo Manager Pro which is an excelent app. You can do everything you can't with Apple's sofware: order pictures in the order you want, any order; copy, paste, transfer... you name it, it does it.

  • How do I get folders to appear above other documents in Finder?

    Okay, I've been using PCs and MACs for years but I only recently bought my own, personal iMac G5. I know how to use OS X pretty efficiently.
    Although, How do I get folders to appear above other files (ie. mp3, .doc, .psd). Meaning that folders have a higher authority then those 'lower' documents.
    I'm basically saying I want to make it act like a Windows Explorer view, in a way.
    It just makes sense to have folders at the top of the list.
    In final, I want to be able to do this. Have everything in a Finder window alphabetical but with folders appearing at the top, in column view.
    Thanks!

    Column view, to the annoyance of many, is strictly alphanumeric and there is no preference in Finder to opt for anything else. There is one kludgy thing you can do: add a space to the beginning of an item name to force it to the top.
    While there is no way I know to set a default width, you can have the width expand to fit the contents by double clicking the || thingies at the bottom of the separator between columns. Option double click will expand all columns to the same width as the the widest one (pretty useless actually, as it only applies to ones already visible, not new ones).
    Francine
    Francine
    Schwieder

  • When I open my video inspector, the box for image stabilization and shutter roll does not appear.  How do I get it to appear?

    When I open my video inspector and I click the Video button, no options for stabilization and rolling shutter appear.  Is there any way that I can get them to appear, so that I might use these tools?

    Tom,
    Many thanks for replying so soon.  Actually, I think I've solved my little conundrum.  I was selecting all of my clips in the timeline with the hope of running both functions.  I found that when I only selected one clip at a time, that the two options would appear and that I could select them.
    Again, Tom, thank you.
    Cheers,
    Rob

  • I upgraded to v16 and now when I go to Hotmail/Qutlook email appears in mobile mode. How do I get it to appear in full browser mode?

    How do I get it to appear in full browser mode?

    Clear the cache and the cookies from sites that cause problems.
    "Clear the Cache":
    *Tools > Options > Advanced > Network > Cached Web Content: "Clear Now"
    "Remove Cookies" from sites causing problems:
    *Tools > Options > Privacy > Cookies: "Show Cookies"
    Start Firefox in <u>[[Safe Mode|Safe Mode]]</u> to check if one of the extensions (Firefox/Tools > Add-ons > Extensions) or if hardware acceleration is causing the problem (switch to the DEFAULT theme: Firefox/Tools > Add-ons > Appearance).
    *Do not click the Reset button on the Safe mode start window or otherwise make changes.
    *https://support.mozilla.org/kb/Safe+Mode
    *https://support.mozilla.org/kb/Troubleshooting+extensions+and+themes
    You can also check your security software (firewall) to make sure that if doesn't modify the user agent or otherwise makes the website think that you use a mobile connection.
    You can check the network.http.* prefs on the about:config page and reset all bold user set network.http prefs to the default value via the right-click context menu -> Reset.
    *http://kb.mozillazine.org/about:config

Maybe you are looking for

  • Why will the front camera on my iphone 4 not work? On both the camera and snapchat.

    Why will the front camera on my iphone 4 not work? On both my camera and snapchat

  • Acrobat X Pro not opening - Part II

    I previously posted on this topic on April 19 and 20 (see http://forums.adobe.com/message/5253051#5253051), but surprisingly, received no responses. I am now reposting because the problem continues and I have been unable to find a satisfactory soluti

  • Does anybody has problem with playing youtube in Opera Mini after iOS6 update?

    After iOS6 update I can not play youtube from Opera Mini,- neither link to youtube works and embedded video as well. It works ok with Safari, but I prefer using Opera Mini instead. If there is a embedded youtube video or just a link to m.youtube, Ope

  • Page leve validation

    I have what I thought was a simple validation but it does not seem to be working. I have the following set as a page level validation, p/sql expression: :P2_ITEM_COLOR iis null AND :P2_ITEM_DESC is null Is seems to always be true however, even if I h

  • Dual boot two oems on new build

    Have built a new desktop. Asus Z97 pro mb, intel 4790k cpu, 16gb ddr3 1333 memory. 1ssd 120gb, 1ssd 240gb, 1 WD 1Tb hard drive for data Purchased two windows 7 oems. Home Premium 32bit and professional 64bit with the components all from the same supp