How to draw a GUI that looks like a basic calculator

Hi,

I am sorry for such a basic question. I would like to create a GUI Calculator which does 4 operations. A vary basic stuff just to play around with GUI components.

But I have hard time to control the size, position, spacing of the component. Also when the whole thing is displayed, the size of the frame is totally different than the one I designed in the GUI. At runtime, the frame crops to surround all the components leaving zero margin. The buttons seems to be resized base on I don't know what rule. Granted I am new to GUI Java programming but I believe drawing a calculator GUI should not be that difficult.

Can you please show me the basic steps to draw a decent calculator?

Thanks very much in advance.

6 comments

On Fri, 05 Apr 2013 09:25:01 MSK, Ralph WreckIt
<no_reply@jetbrains.com> wrote, quoted or indirectly quoted someone
who said :

>I am sorry for such a basic question. I would like to create a GUI Calculator which does 4 operations. A vary basic stuff just to play around with GUI components.
>
>But I have hard time to control the size, position, spacing of the component. Also when the whole thing is displayed, the size of the frame is totally different than the one I designed in the GUI. At runtime, the frame crops to surround all the components leaving zero margin. The buttons seems to be resized base on I don't know what rule. Granted I am new to GUI Java programming but I believe drawing a calculator GUI should not be that difficult.

One trick is to design the calculator as several JPanels.  Each panel
will be much simpler than the overall frame.

You also need to do some experiments with GridBagLayout so you know
what visual effect to effect from fiddling each parameter.


--
Roedy Green Canadian Mind Products http://mindprod.com
Motors make noise, and that tells you about the feelings and attitudes
that went into it. Something was more important than sensory pleasure --
nobody would invent a chair or dish that smelled bad or that made horrible
noises -- why were motors invented noisy? How could they possibly be
considered complete or successful inventions with this glaring defect?
Unless, of course, the aggressive, hostile, assaultive sound actually served
to express some impulse of the owner.
~ Philip Slater (born: 1927 age: 85)
The Wayward Gate: Science and the Supernatural

0

Try Swing GUI Designer (http://www.jetbrains.com/idea/features/gui_builder.html)
It's available in both Community and Ultimate editions.

0
Try Swing GUI Designer (http://www.jetbrains.com/idea/features/gui_builder.html)

Hi,

I have of course read that page and went through the demo, which BTW seems to be outdated. The Swing GUI Designer is a total catastrophe. I have played around since a day already and yet I could do a simple thing like placing two labels side by side.

The Designer seems to split the containing panel in a grid with a very gross division so there is no way to drag a component to the exact x.y position. Each time I drop a component on the panel, the designer creates a bunch of rows and columns. Place the component in a cell, and size the cell in an arbitrary manner. Worse yet sometimes a new component added to the panel completely change the layout (change size of the form and truncate another element).

I can't believe, a simple form with 1 slider in the bottom, 2 labels just above the slider (in the form: Label1: Label2). With Visual Studio, this takes me less than 5 minutes. With Idea GUI Designer, after 3 hours, I cannot yet figure out how to place these 3 objects.

I don't understand all the different Layout properties.  What is the problem to place a component to corrdinates x1,y2 and give  the component de size x2,y2? The designer just leave the user to decide  which x,y coordinates to use and that's it. Here the Designer is trying  to be to smart, guessing a kind of grid layout and decides the placement  and size on behalf of the user. Sorry if this sound confusing but if ever you have the opportunity to play with the Form Designer in Visual Studio, you'll see what I mean. It is so straightforward that I can even have a kid design a form for me.

There is something seriously wrong with the GUI Designer in IntelliJ IDEA.

0

Hello.

IDEA designer really could be improved at a number of points. But reading this and sibling thread (http://devnet.jetbrains.com/message/5483795) I've got an impression that you blame IDEA in some drawbacks actually defined by Swing API.
In particular, many popular layouts, and layout conception in general is developed by Sun/Oracle. Please see: http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html
The appearance of components is defined by so-called "look and feel", and this is also Sun conception: http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/index.html
These are basic points which are very desirable to understand when creating GUI on java.
Of course, UI designer should provide some assitance to operate with those entities, but this is just an assistance. Maybe some advanced designers somehow hide all these stuff from developer, but layouts and look and feel are not eliminated because of that.

Sorry, if this is not news for you.

Answering your questions: yes, IDEA is pure Swing application (I don't investigate some Mac-specific aspects, though), and quite many IDEA dialogs are made in IDEA designer.

Regards,
Alexander.

0

Hi Alexander,

Thank you for your answer. Indeed, I am novice in Swing GUI design and didn't know that all the difficulties are rooted from the design of Swing. Being used with Visual Studio, my first thought was that IntelliJ UI Designer was acting on me. Since then I have read more about Layout Managers. To make it short, it appears that the best layout manager is MiGLayout:

http://www.miglayout.com/
http://jnb.ociweb.com/jnb/jnbMay2009.html

Experienced users recommend to code the GUI by hand, without using any UI Designer assistance. Have you already tried MiGLayout with IJ Idea? Even coding by hand? If so can you give me some basics instructions at least to put MiGLayout Library into IJ Idea?

Thanks

0

I agree, MigLayout is the best. To use it you should:
1. Download miglayout-swing.jar (and optionally miglayout-sources.jar) from http://www.migcalendar.com/miglayout/versions/4.0/
2. Add new library in project (It's Project Structure dialog -> Libraries -> +)
3. Add library dependency to project module

0

Please sign in to leave a comment.