Please help - JFrame, menu, another JFrame, setBackground

I am trying to make a simple program that draws a JFrame with a menu. The menu has one item, which when selected draws another JFrame. That JFrame has a button, which when pressed changes the background color. Many hours have gone into this, despite the fact that I made a similar program a few weeks ago.
The compiler complains about the method getContentPane, when I remove that, it runs and draws the first window, but the menu item does not make the second window. Also, the setDefaultCloseOperation confuses the compiler as well.
Please help.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MenuAction2
public static void main(String[] args) 
     MainWindow window = new MainWindow("Menu action example");
  //window.setDefaultCloseOperation.(JFrame.EXIT_ON_CLOSE);
     window.setVisible(true);      
class MainWindow extends JFrame
     MainWindow(String title)
     super(title);
     getContentPane.setLayout(new BorderLayout());
     Toolkit theKit = getToolkit();          
     Dimension wndSize = theKit.getScreenSize();  
     setBounds(0, 0,wndSize.width, wndSize.height);
     setJMenuBar(menuBar);
     fileMenu = new JMenu("File");
     connectItem = new JMenuItem();
     connectItem = fileMenu.add("Connect...");
     connectItem.addActionListener(menuHandler = new Handler());
     menuBar.add(fileMenu);
     getContentPane.add(toolBar, BorderLayout.NORTH);  
class Handler implements ActionListener
     public void actionPerformed(ActionEvent e)
      win = new ConnectWindow("Connect to Database");
      getContentPane().add(win, BorderLayout.NORTH);
      setVisible(true);
  private JMenu fileMenu;
  private JMenuItem connectItem;
  private JMenuBar menuBar = new JMenuBar(); 
  private JToolBar toolBar = new JToolBar();
  private Handler menuHandler;
  private ConnectWindow win;
class ConnectWindow extends JFrame
     ConnectWindow(String title)
          super(title);
          setSize(200,200);
          setLayout(new BorderLayout());
          JButton connectB = new JButton("Connect");
          connectB.addActionListener(new ActionListener()
               public void actionPerformed(ActionEvent e)
                    setBackground(Color.red);
          add(connectB,BorderLayout.SOUTH);
          setVisible(true);
}

getContentPane is a method so you should use it that way:
getContentPane().setLayout(new BorderLayout());
getContentPane.add(new Button("Ok"));

Similar Messages

  • Please help with menu links

    Hello. I'm a new user to CS3. I started building a new site
    and everything looks good except I have no idea how to link the
    menu buttons to their URL's. Every time I enter code in the actions
    I get an error message.
    Background: I created text and then converted them into
    Buttons (i.e., home, about us...). These buttons will link to other
    pages on my site. I'm using a combination of flash and html.
    Thanks

    Mlipkin, an easy way to have buttons link to a url is to use
    "Behavoir's" under the window drop down.
    Select your button pick window, behavoirs, (plus sign) add
    behavoir, go to web page, and type in your url.
    Hope this helps.

  • Can you please help with yet "another" Sync problem?

    I have recently changed from windows xp. I have a new computer with windows 7 and I loaded Itunes 10. There have been nothing but problems with my 5th gen nano since I have changed to my new computer.
    I am unable to sync new songs; In the device section, if I click on
    MUSIC; I get an error; "are you sure you want to sync music? all existing content on the Ipod will be removed and replaced with songs and playlists from your Itunes library.
    It has been frustrating, because I would like to add my playlists and sync automatically and for some reason, I am not able to. Thank you for any light you may be able to shed on this situation; Tawney.

    To Contact iTunes Customer Service and request assistance
    Use this Link  >  Apple  Support  iTunes Store  Contact
    Note: iTunes Gift Cards are only Valid in Country of issue

  • I lost my iPhone device, how can I get my data back on another one without using an iCloud backup just back up on i Tunes, Please Help.

    I lost my iPhone device, how can I get my data back on another one without using an iCloud backup just back up on i Tunes, Please Help.??

    You can find the backup files and then copy them to a safe place if you are worrying about this.
    iTunes places the backup files in these places:
    Mac: ~/Library/Application Support/MobileSync/Backup/
    The "~" represents your Home folder. If you don't see Library in your Home folder, hold Option and click the Go menu.
    Windows Vista, Windows 7, and Windows 8: \Users\(username)\AppData\Roaming\Apple Computer\MobileSync\Backup\
    To quickly access the AppData folder, click Start. In the search bar, type %appdata%, then press Return.
    Windows XP: \Documents and Settings\(username)\Application Data\Apple Computer\MobileSync\Backup\
    To quickly access the Application Data folder, click Start, then choose Run. In the search bar, type %appdata%, then click OK.

  • Pretty Please Help me - Context Menu

    PS... this is not only a AS3 question, but also AS2 would work just fine...
    This is going to be a hard one to communicate, so I will have example files attached...
    How can I right click on a MC and have a custom menu, WHEN another movieclip is on a higher layer and overlapping that original MC (and this second movieclip does Not have a context menu)
    Thank you so very much for your time and help on this issue,
    two files are attached, first is the most basic way I can show this issue.
    The second is a better example of Why I NEED to figure this out.
    http://rapidshare.com/files/380919381/contextexample.fla.html
    http://rapidshare.com/files/380919537/contextexample2.fla.html
    Thanks so very much for your time on this issue... in the mean time I will try and help others while I wait for an answer...
    -GK

    bump... looked all over the internet and back, been to a dozen forums...
    I just cant belive this is impossible....
    pretty please help!
    -GK

  • PLEASE HELP!! Drop Down Menu Using Photoshop Images

    I have hit a brick wall in trying to work this out, and it is
    killing my productivity, I just need to get through this hurdle on
    this site, and I can finish it.
    Quick rundown on where I'm at:
    I created the website to look how I wanted in Photoshop.
    After I've gotten it looking how I want, I bring it into fireworks
    and slice it up (I'm sure this step can be skipped, but thats how
    I'm used to it), and then I export it into Dreamweaver.
    All of that is working fine, except for this drop down menu.
    All I'm wanting to happen is when they roll their mouse over "firm
    profile" for example, for there then to be a drop down menu that
    drops below it (no animation necessary) that has 2 additional
    options they can select.
    I have created what the page would look like after the drop
    down menu would come up (in photoshop). I've tried doing swap
    images and menu bars and some scripts from the net etc etc but
    nothing works. It either tries to load the drop down menu WITHIN
    the slice where it says "firm profile" instead of below it, or just
    messes up completely.
    In fireworks, I'm able to setup a swap image where when you
    put your mouse over "firm profile" it asks which slice you want the
    new swap image to come up in, which would work fine EXCEPT that
    when you go to select one of the bottom submenu options, the
    rollover disapears. If you do it where the whole slice is switched,
    then that submenu appears when you rollover where it would be, even
    if you haven't first rolled over "firm profile"
    So to summerize: All I want is to be able to tell it when you
    put your mouse over "FIRM PROFILE" that underneath it , a drop down
    menu image that I have already created pops up, and you are able to
    1) select the sub menu items (urls). I feel that this should not be
    anywhere near this hard.
    Please help.

    cdeatherage wrote:
    I just realized--and Murray correct me where I stray--I may have intimated what you want to do is not very complicated. It will be a bit involved, actually. You are probably going to have to set up an id for each menu item so that you can apply the applicable image to the link state for each button. That is to say, your images for Menu Item 1 are going to be different than for Menu Item 2, so you are going to have to code individual link states for each button rather than rely on a global state. I think you can still do what you have designed, but you will likely have much frustration getting there if you are not comfortable writing code. Perhaps what might be easier for you is to use text in the html code for each menu item and then globally apply one background image for the a: state, another for the hover state, etc.
    Hope I'm making sense.
    Chris
    Yes that does make sense - and is exactly the thought I was coming round to.

  • OT: Need help on menu, please (JavaScript)

    Hi there and thanx for your help so long.
    My question is about this menu:
    http://www.byscripts.info/scripts/javascript-dynamic-accordion-menu
    I must say that I really need this to work.
    My effort is here:
    www.energywa.co.za/layout/
    It works beautifully in IE but not as beautifully in FF and Chrome.
    Problem:
    By just putting in the adress as above, the coloured boxes do not appear at all. They only appear when I click on the Home link at the top of the page or I type www.energywa.co.za/layout/index.php in the address bar.
    It does this on my local machine as well as the server where it is hosted on.
    What bugs me is that it works perfect from the writer's site in all 3 browsers. Unfortunately there is no support forum on the site and I have alrwady mailed the writer but had no luck yet.
    Can someone please help me? I am in the process of learning JavaScript but can only copy & paste at the moment. ;-)
    Thank you so long.
    Deon

    Hi Gary.
    I eventually got it to work. There is still a lot of clutter in the script which I need to sif through, but it works! I am so relieved. I used the files another person modified (from the link I posted) and changed them here and there.
    I also still need to get my menu to the left, but that should be easy.
    You can again have a look at www.energywa.co.za/layout
    It works in IE, FF and Chrome.
    Thanx very much again for all your help. I don't know what exactly fixed it, but it is there somewhere.
    Regards,
    Deon

  • Help -- JFrames won't appear

    Hello, I am currently taking a course in Java, and I have no problems at school, however, when coding anything at home, with any editor JFrame won't do anything for me. The only things that I get are console msgs. Even with the simple code of:
    import javax.swing.JFrame;
    public class test extends JFrame{
         public static void main(String[] args)
         test te = new test();
         te.setDefaultCloseOperation(EXIT_ON_CLOSE);
              System.out.println("this is a test");
         public test()
              setSize(300,100);
              setTitle("Hello there");
    I get nothing with this code except: 'this is a test' in the console. Please help!

    try to call
    te.show();;)

  • HT4527 My itouch is synced to the itunes in my compaq laptop.However,I have bought a new vaio laptop.How to I transfer mt itunes from one laptop to another? Please help me!

    My itouch is synced to the itunes in my compaq laptop.However,I have bought a new vaio laptop.How to I transfer mt itunes from one laptop to another? Please help me!

    Authorization and Deauthorization
    Macs:  iTunes Store- About authorization and deauthorization.
    Windows: How to Authorize or Deauthorize iTunes | PCWorld.
    In iTunes you use the Authorize This Computer or De-authorize This Computer option under the Store menu in iTunes' menubar. For Windows use the ALT-S keys to access it. Or turn on Windows 7 and 8 iTunes menus: iTunes- Turning on iTunes menus in Windows 8 and 7.
    More On De-authorizing Computers (contributed by user John Galt)
    You can de-authorize individual computers, but only by using those computers. The only other option is to "de-authorize all" from your iTunes account.
      1. Open iTunes on a computer
      2. From the Store menu, select "View my Account..."
      3. Sign in with your Apple ID and password.
      4. Under "Computer Authorizations" select "De-authorize All".
      5. Authorize each computer you still have, as you may require.
    You may only do this once per year.
    After you "de-authorize all" your authorized computers, re-authorize each one as required.
    If you have de-authorized all computers and need to do it again, but your year has not elapsed, then contact: Apple - Support - iTunes - Contact Us.
    As for transferring your iTunes Library: iTunes- How to move the library to an EHD. Once you have done this take the external drive to your new computer and restore the iTunes data on the new computer.

  • My iphone 5 doesn't sync with itunes on my pc. it says this computer has  previously been synced to another io6 device or iphone? please help what to do?

    My iphone 5 doesn't sync with itunes on my pc. it says this computer has  previously been synced to another io6 device or iphone? please help what to do?

    The error message you're seeing is likely the other way around -- i.e. something like "This iPhone has previously been synced with a different computer".   There's no problem syncing multiple iOS devices to the same computer.
    However, the reverse is not true for the iPhone --  you cannot connect an iPhone to another computer without it being erased, even if you manually manage music and video.  So, it sounds like you've been syncing your iPhone to a different computer?
    Also, 5.1 (not 5.0.1) is the latest iOS.

  • My ipod says it was et up with another itunes library, it wasn't, and when I add songs to my library when I go back into it they are all gone, please help im going in circles

    Please help, I am constanly re importing songs, I have to re sync my ipod to my library each time i plug it in, and I am unable to import songs from my media player, any ideas?

    When I plug it in I get the **'s ipod is already synced with another itunes library so do I want to erase & sync message
    Even though it is the same music in iTunes, iTunes does not see it as the exact same library because you had to re-add all the songs to it.
    You will need to erase and sync and you iPod will simply end up as it is now.
    It's like of your house burns down and you rebuild it exactly the same, it is still not the "same" house it was.

  • I want to bring over music from my iTunes library on iMac to an ipod touch with another apple id. please help

    I want to bring over music from my iTunes library on iMac to an ipod touch with another apple id. please help. Is this possible. In former versions of itunes this was posible. I can't find the solution is iTunes 11

    Perhaps those apps require a newer version of iOS than is compatible with your iPod.

  • Trying to sync iPod with library, but receive this message, "The iPod is synced with another itunes library.  Do you want to erase this ipod and sync with this itunes library".  I do not want to erase my iPod... Any suggestions??  Please help!!

    Trying to sync ipod with itunes library, but I keep getting this message, "  The ipod is synced with another itunes library.  Do you want to erase this ipod and sync with this i tunes library?" 
    I do not want to erase my ipod!!!  Any suggestions?? please help!

    Another way. You can use a USB flash drive & the camera connection kit.
    Plug the USB flash drive (works the same with an SD card) into your computer & create a new folder titled DCIM. Then put your movie/photo files into the folder. The files must have a filename with exactly 8 characters long (no spaces) plus the file extension (i.e., my-movie.mov; DSCN0164.jpg).
    Now plug the flash drive into the iPad using the camera connection kit. Open the Photos app, the movie/photo files should appear & you can import. (You can not export using the camera connection kit.)
    Using The iPad Camera Connection Kit
    http://support.apple.com/kb/HT4101
     Cheers, Tom

  • How do I get rid of the Microsoft Setup Assistant loop? I migrated my software/documents from another laptop so don't have the disk to reinstall. Please help! Can't open any Microsoft Office software, like Word, and stuck in a loop?

    I migrated my software/documents from another laptop so don't have the disk to reinstall. Please help! Can't open any Microsoft Office software, like Word (for 2008), and stuck in a loop?
    Whenever I select Word Microsoft Setup Assistant appears, asks for feedback, then after selecting okay (both on saying yes or no to feedback) goes on to a registration page. When I click on this it says I've already registered so I just click okay, and then move on to a update page. After this, if I click on Word, the process repeats itself.
    As I said, I don't have the disk to reinstall, and can't find the Office Settings to delete as many pages have suggested I should try. Safe Boot restarting also hasn't worked... Really stuck and need Word very soon for work.
    If you can help, that would be great, and feel free to ask any questions about the situation as I'm not an expert here.
    Cheers,
    Jack

    First, export your contact from iCloud.com and save them on your computer in a safe spot some where (like you desktop).  Use this to help you do this: http://support.apple.com/kb/PH3606
    Next, on both of your devices, go to Settings > iCloud and turn on contacts and select Merge. Then turn off contacts and select 'Delete form my [device]' when prompted.
    Now go back to iCloud.com and select a contact (yes they will all be messed up again) and select Command+A on a Mac or Control+A on a PC to select all of the contacts.  Tap the delete key on your keyboard (or right click /control click a contact and select delete).
    You iPhone, iPad and iCloud.com should not be empty for contacts.
    Go back to Settings > iCloud on both devices and turn on contacts again (you should not see merge this time).
    Next, go back to iCloud.com and import your contacts (those exported .vcards).  You can either drag and drop them into the empty contacts list in your web browser, or you can use the gear icon to import.
    You cleaned up contacts should import correctly into iCloud.com and sync to both of your devices.
    Good luck.

  • IPad 2 deleted all of my contacts, when I run the Dr. Fone software it shows they are still on my iPad 2 but it costs 60-70 dollars. Is there another way to retrieve them? They were not backed up on Itunes like I thought they were. Please help!!

    When I run the Dr. Fone software it shows they are still on my iPad 2 but it costs 60-70 dollars. Is there another way to retrieve them? They were not backed up on Itunes like I thought they were. Please help!!

    Recover software as you have described is pretty much your only option - keep in mind that if wait, some of the currently recoverable data may well be over-written and lost.

Maybe you are looking for