What is the problem here?

Hello,
First of all I am a beginner. But I thought it would be better to write this topic here rather than Beginner.
I have written a simple calculator program. But I have a problem with the interface. I created a Text Field (JTextField actually). And below this text field, buttons are placed.
The buttons consist of 4 rows and 4 colums (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, C, OK, *, +, -, /).
The problem is: the buttons (0, 4, 8, *) are in the width of the textField. Whatever length you enter for the textField, the length of the buttons changes.
I have used the GridBagLayout. Which one is more suitable for a job like this?
GridBagLayout or GridLayout?
I tried GridLayout also, which didn't work either.
In the code below, for the JLabel I used: fill constraint. as HORIZONTAL.
Does it make any sense? I also tried to use the fill constraint also for the textField. I used:
cons.fill = GridBagConstraints.FIRST_LINE_START
But it also didn't work. I am confused, I almost tried everything and couldn't get a satisfying result.
When I use GridLayout this time all the buttons take the length of textField area.
I used GridLayout(0,4,1,1).
Also while using the GridLayout if the windows is maximized then the shape of buttons and the textField also changes. Is there a way to stop this?
Here is the code:
  public void addComponentsToPane(Container pane) {
    if(LEFT_TO_RIGHT) {
      pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    pane.setLayout(new GridBagLayout());
    GridBagConstraints cons = new GridBagConstraints();
    subtotal = 0;
    operator = new String("");
    stringvalue = new String("");
    label = new JLabel("Simple Calculator");
    cons.fill = GridBagConstraints.HORIZONTAL;
    cons.gridx = 0;
    cons.gridy = 0;
    pane.add(label, cons);
    textField = new JTextField(15);
    cons.gridx = 0;
    cons.gridy = 1;
    pane.add(textField, cons);
    zero = new JButton("0");
    zero.addActionListener(this);
    cons.gridx = 0;
    cons.gridy = 2;
    pane.add(zero, cons);
    one = new JButton("1");
    one.addActionListener(this);
    cons.gridx = 1;
    cons.gridy = 2;
    pane.add(one, cons);
    two = new JButton("2");
    two.addActionListener(this);
    cons.gridx = 2;
    cons.gridy = 2;
    pane.add(two, cons);
    three = new JButton("3");
    three.addActionListener(this);
    cons.gridx = 3;
    cons.gridy = 2;
    pane.add(three, cons);
    four = new JButton("4");
    four.addActionListener(this);
    cons.gridx = 0;
    cons.gridy = 3;
    pane.add(four, cons);
    five = new JButton("5");
    five.addActionListener(this);
    cons.gridx = 1;
    cons.gridy = 3;
  }Thanks in advance for your helps.

Although the GridBagLayout is extremely flexible it is also more complicated to use.
I prefer to use multiple LayoutManagers to get the effect I want. Maybe something like this:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=609795

Similar Messages

  • Since upgrading to iphoto 11 I,m having difficulty viewing my photos in edit mode. On the quick scan of info mode the photos appear sharp, however in edit mode the edges blur up making it very difficult to edit properly, What is the problem here?  I never

    Since upgrading to iphoto 11 I,m having difficulty viewing my photos in edit mode. On the quick scan of info mode the photos appear sharp, however in edit mode the edges blur up making it very difficult to edit properly, What is the problem here?  I never

    Marisa, you stand a better chance of getting an answer concerning iPhoto by posting this question in the iPhoto forum
    https://discussions.apple.com/community/ilife/iphoto

  • I have downloaded the Creative Cloud and when it finished, it opened up and was completely blank. Nowhere for me to sign in and no apps for me to click to download. I need this to be fixed ASAP. What is the problem here?

    I have downloaded the Creative Cloud and when it finished, it opened up and was completely blank. Nowhere for me to sign in and no apps for me to click to download. I need this to be fixed ASAP. What is the problem here?

    BLANK Cloud Screen http://forums.adobe.com/message/5484303 may help
    -and step by step http://forums.adobe.com/thread/1440508?tstart=0
    -and http://helpx.adobe.com/creative-cloud/kb/blank-white-screen-ccp.html

  • Hi Guys, I have a problem with Importing Pictures onto my Mac mini with Lion installed by using a Card Reader, What is the problem here so I can get Pictures onto my Computer. ,

    Hi Guys,
    I have a problem with Importing Pictures onto my Mac mini with Lion installed by using a Card Reader,
    What is the problem here so I can get Pictures onto my Computer. ,

    Hi Noob Søren
    There are a few things that are confusing in your question.
    As far as I know, you dont have to install Time Machine on this OS as it is already installed for you. You only need to connect a hard drive to your computer via firewire or usb, click on the Time Machine icon, Open Time Machine Preference in the drop down menu and select a disk: your connected hard drive.
    You can of course reformat this connected device, partition it into a few volumes to organise data if you so wish.
    I find it strange that your mac's hard drive is divided into two volumes... perhaps this was created through bootcamp?
    You can access the configuration of your hd through Applications/Utilities/Disk Utilities.
    Clicking on one of the icons on the right hand panel will bring the details of the contents of your hardDrive and volumes. From there you can decide to erase a partition, reformat etc....
    If your hd contains more than one volume, and one of them is empty, you could decide to remove it. Back up all your important data before doing so.
    Hope this helps
    WN

  • HT1473 if my song is repeated 3 times in my iPad, what's the problem here?

    if my song is repeated 3 times in my iPad, what's the problem here?

    I am also encountering this issue and I am running 10.7.4.
    Here's a link to a .dmg installer for iTunes 10.7.0.21 (which is compatible with Snow leopard or above):
    http://www.apple.com/itunes/download/
    Are you able to update using that?

  • AppleScript, what is the problem here?

    Hello,
    I am making my way through this book and I have come across something that is not making sense to me.
    I entered this script from a book;
    tell app "Finder"
    set these_items to sort (get every document file of the front finder window) by creation date
    repeat with i from 1 to count of these_items
    set this_item to item i of these_items
    set the name of this_item to ((i as string) & "-" & (the name of this_item))
    end repeat
    end tell
    After running it, I receive the error message Can't get i (document file <name of file> of folder <name of folder>) etc. etc.
    The exercise is supposed to add a numerical prefix to every file in the folder by creation date so even if the view option is changed to sort by name they will still sort by creation date.
    The book is a little out-dated and I have been able to make most things work but this has me stumped. So, what is wrong with this and am I correct in assuming i is just a variable?
    Thank you for your time and I hope you have a great day!
    dofromon
    t
    tel

    Actually I have tried a few different windows/folders but the one in the book deals with jpg images in the folder. I created and downloaded all the images on the same day so I looked for another folder with different creation dates. Anyway that was useless info, but I have tried more than one folder and get the same response, just with different paths in the error message of course. Note always jpg images in the folder.
    After running the script, the line;
    set this_item to i of these_items
    has the i highlighted in blue, hence me wondering if that was the problem.

  • My 2008 MacBook Pro screen has now got interference across the whole screen. It looks similar to a pre-cable interference from bad reception. What is the problem here?

    My 2008 MacBook Pro has interference across the full screen. It looks similar to a pre-cable TV screen with bad reception. Any idea of what the problem might be?

    Took it to the Apple Store, they determined it's my screen that's messed up (they connected to another monitor, and since the lines didn't mirror onto the monitor it couldn't have been my GPU making the lines but rather the screen itself), so not connected to my wifi issue or a potential logic board issue, so I'm going to get my screen, wifi card, and trackpad (it's been failing too but that wasn't urgent at all) replaced for $330 using their flat-rate repair service which is much better than I imagined in terms of price.

  • Trying to use VS on my Surface Pro, what is the problem here?

    so I am taking a programming class and using my SP gen 1 and the only C++ program I have been able to use on my SP is VS 2013, however, after 30 days it shuts off. I thought this was good for a year? As is so typical for M$, trying to find a solution
    is next to impossible without wading thru a ton of documentation or other nonsense on the VS site.  I have reloaded the program multiple times with the same effect, so that is no good.  I am at an end with this and do not know how to proceed
    with getting this or another C++ programming app to use on my SP, regretting I ever bought the damn thing. If there is a worthwile suggestion to make this work, I would be interested, thank you...

    Hello,
    Visual Studio 2013 Community version is totally free for users. You only need to register with your account to use it. Please notice that even in a free version, user account is required.
    Based on your scenario, I recommend you uninstall your current version and reinstall the Community version. And remember to use the /force command when uninstall:
    For how to use /force command, please see here
    https://support.microsoft.com/en-us/kb/2771441/
    I also have a guess that your account may have problem here. You may also try create a new Microsoft account and test the Community version.
    If you still doubt about VS2013 license, please check here
    https://www.visualstudio.com/support/legal/dn877550
    "If you are an individual working on your own applications to sell or for any other purpose, you may use the software to develop and test those applications."
    This version is free for individual working developer.
    Best regards,
    Barry
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • I can't open Itunes on my laptop. "Itunes has stopped working. A problem causes the program to stop working correctly. windows will close the program and notify you if a solution is available" is the message. what is the problem here?

    I can't open my Itunes program in my laptop computer with windows 8. the message is "Itunes has stopped working. A problem causess the program to stop working properly. windows will close the program and notify you if a solution is available". I reboot  the machine, Uninstall and re install the  Itune program, stilll the same message pops up.

    The first thing to try is System Restore to take you back to a point in time when it did work. If that is not successful, create a new user on the computer. log in with that and see if ID will start. This will tell us if the problem is in the system/program or the user account.
    If it works in a new user account, see Replace Your Preferences and in addition to replacing the prefs, empty the InDesign Recovery folder that will be in the same location and the InDesign SavedData file.

  • Ipod nano (6th gen), appears under devices in itunes and all songs play properly. Upon ejecting from PC, ipod says 0 songs but still 14gb/14.88 have been used up. What is the problem here?

    Pretty much what I asked in the question. Plugged the ipod back into the PC and it still works fine under Devices in itunes and all the songs re-appear here again. Havent been syncing with any different computers

    I requested that your post by moved to the iPod Nano forum. This is the iPod touch forum.

  • When I copy an email message and paste it into a document, the font turns into all symbols.  What's the problem here?

    When I copy an email message and paste it into a document, the font becomes all symbols. What is wrong?

    To use images from the media browser, you must generate previews of the images first. It sounds like the "automatically generate previews for every new project" is turned off under Aperture >Preferences>Previews. To generate a preview for a photo to use in the media browser, right click it and select "update preview". The quality of the preview photo is determined by the settings in the Previews tab found under the Aperture>Preferences menu.

  • My camera doesn't work + it doesn't recognize external cameras. what's the problem here?

    I set up a new windows 7 version for my laptop (probook 4510s) & the camera doesn't work + it doesn't recognize any external camera. So how to fix my camera & get it back to work?

    What kind of external camera is this? I am assuming it is a USB camera? Make sure that you aren't using any USB hubs. Also try connecting it to another USB port. Have you installed the drivers from the camera manufacturer yet?
    Thanks,
    Sean
    -------------How do I give Kudos? | How do I mark a post as Solved? --------------------------------------------------------

  • Iphone 4s has vertical lines all across the screen and will not power down unless it is charging. What is the problem here?

    My iphone 4s recently stopped turning on. I left it alone on the charger for a few days and now will power on just fine. However I have vertical lines going across the entire screen. I also noticed that it will not power down and stay off unless it is connected to the charger. Is this a hardware problem?

      Try to Reset your device. 
    Press and hold both the Sleep/Wake button and the Home button for at least ten seconds, until the Apple logo appears (ignore the Slide to Power Off option that shows up first).

  • Non of the viedos in ipod is syncing to my computer, and only photo streaming will syn to the computer. what is the problem here

    I have tryed changing in ituned to sync wifi, and have selectedSync photos from ipod photos cache, all folders including viedos and pressed sync.  But it has not happened

    No I can't. The iPod is nearly full and I want to free up space by putting vid n photos onto computer.  It use to do it but for some reason this has changed and hasn't been syncing for the last 6 mths or so.

  • I'm getting a message that firefox 4.0.1 is incompatable with a security download, Modulo de Seguranca Banco do Brasil 1.0.18.2. What's the problem here?

    I don't have anymore details.

    http://www.guj.com.br/java/237073-firefox-4-e-site-banco#1222913
    I don't read Spanish, but based upon the code I see in that posting, I guess that extension hasn't been updated yet for Firefox 4.

Maybe you are looking for