GUIs = teh suck

  • Thread starter social_outcast
  • Start date
S

social_outcast

Guest
Please excuse the lengthy back-story to this post, Im drunk on success

Well, its very early in the morning now, but Im EXTREMLY HAPPY! Almost as happy as T-Bone when he found out he could run Oblivion (on a par with orgasm)

Basically for my exemption project for programming (do a lump of code and submit, dont need to sit the final exam), Ive gotta impliment a text-adventure game into a graphical-user-interface. And this morning at about 2:50am I made the breakthrew of getting some buttons to work :P lol

so in short, I now have all the code I need to set up butons, comboboxes, whatever, to do my bidding
the problem is...

JAVA COULDNT ORGANISE A CONTENT PANE IF IT HAD A LISP AND FAILED TO LAUGH AT A CHUCK NORRIS JOKE!!! :mad:

Ive been digging through the api and, well, its the api :S
Are any of you fluent at all with Java?
Im getting there slowly, but I cant seem to get my head around configuring the look of java GUIs, just wondering if anyone knows any sites, or even general tips I could use?

basically Ive got a Jframe with a bunch of direction buttons (which I was amazingly and proudly able to lay out like a D-Pad) that substitute the actions of typing in a "go" command followed by the direction. What I need now is a way to sperate this into a region of a much larger frame and create areas for lengthy strings (so I can stop typing bloody System.out.println("blah, blah, blah, fall over and die"); every 20 seconds - lol)

sorry, this has been the rant of a tired mind, but any input shall be greatly appreciated :P

-S_outcast
 

VibroAxe

Junior Administrator
talk to elDiablo, he's a bit of a don at java

I can help with general code, but not the layout of GUI's thats my gripe too
 
E

elDiablo

Guest
Right, I love Java. I just typed you a nice long message, and then lost it. So here it is again. I'll give you a brief intro to GUIs in Java, and you can ask any questions you might have. Firstly I'll just say about different types of GUI, then I'll talk about Layouts, and finally about Components. Any questions after that, please feel free to ask.

  • General Info
    Right, there are 2 main types of GUI in Java (that I will talk about) - AWT and Swing. AWT is more concerned with events, so is a lot harder to code graphical stuff in, while Swing is more concerned with how stuff looks. Therefore, we use a mixture (as you probably already have). You are using a JFrame (from Swing) and button handling (from AWT), which is good.

    Now, everything that is a GUI is a Component (or JComponent). It can be added to things, and can have things added to it. Somethings (like Buttons) you don't want to add to, while others (like JFrames and JPanels) you definately do. You add other Components to a Component using its Container. Thing of it like lego. You can put bits on top of other things, and then put even more things on top of that! Its a bit weird, but meh =/ Now, these building blocks have to have some way of telling the items that are put on top of them how to be positioned, otherwise everything would be a mess! Enter the LayoutManager.
  • Layouts
    The LayoutManager (and the LayoutManager2) is an Interface in the API which describes ways of positions Components within a Container. The main ones I'll talk about are the BorderLayout, the GridLayout and the SpringLayout.
    • BorderLayout - The simplest of layouts (kinda). You can position items in one of five areas - each of the borders of your Container (North, South, East and West) and the Center. Looks like this. You put items in those positions be calling
      Code:
      Container.add(component, BorderLayout.POSITION);
      where POSITION is one of the five public fields in the BorderLayout class.
    • GridLayout - Forms a grid on your component with equal sized partitions. You have to construct an instance of this with the number of rows and columns you want, then set it as the container's layout manager. So
      Code:
      JFrame.setLayout(new GridLayout(3,2));
      and adding 6 JButtons numbered 1-6 will give you this look. Obiviously you can have however many rows and columns you want, but when you add items to the container, it will add them IN ORDER. You can change the addition positions from starting at the top-left and finishing at the bottom-right, to anything you like (like bottom-left to top-right), but they will appear in order.
    • SpringLayout is probably the hardest to understand, but can potentially be the nicest to use. It is my personal favourite as it lets you position items relative to each other. This means that it will not only look the same on every platform, but will adjust itself when resized.

      SpringLayout works by adding "Constraints" between items. Look at the API (link above) for full details and examples (bit complex for me to go into), but definately try and use it. One note that is VERY important with this (and always gets me out) is that when you add a contraint
      Code:
      SpringLayout.putConstraint(String e1, Component c1, Spring s, String e2, Component c2)
      the first component and second component ordering matters! If you add a constraint between c2 and c1, instead of c1 and c2, all of your components will be in the top left corner (the origin) as it doesnt understand it...
  • Components
    As I said before, everything in a GUI is a Component. There are "base" Components, like JFrames, and "top level" components like Buttons (my names, not official). Base components can be easily added together to provide separations in your GUI. There is one called a JPanel which is very useful. If you create two JPanels, and then add two buttons to one, and a password field and a button to the other, you can then add the JPanels into your JFrame, and position them as you like.

    This means that you can position the items within each JPanel as you see fit, and then position the Panels as you like. Or, alternatively (and the big thing about it) is you can create your own class which extends JPanel, and have it always add the same items to it in the same position. You can then reuse this code as much as you like. For example, you could make a PasswordPanel, which has a JPasswordField and a JButton add to it, with a method that MD5s the password on clicking the button, and saves it in a private variable, with a method to get it out. Then, in your project, you can reuse this as many times as you like, and not have to recode it each time! The point of extends JPanel means that it sorts out all the drawing of your class, and it implements the required interfaces to be a Component.

I hope this is an ok introduction for you, and if you have any other questions, please feel free to ask! :)
 

thatbloke

Junior Administrator
elDiablo said:
Java ftw! :P

i'm a Java programmer too. elDiablo will kill me for posting this, but get IBM's Eclipse IDE and then get the Jigloo plug-in for it. This is a graphical method for creating your GUI which simply allows you to drag and drop components onto a panel in a very similar way to Visual Basic (if you have ever used that)
 
E

elDiablo

Guest
EUGH! Don't use Eclipse! Horrid horrid thing. And its not IBMs any more. The last 2 versions were community built after IBM gave all the source to the world for free.

JetBrains's IDEA is an uber IDE. Its got a 30 day trail, and then an academic license is $99. The people there are very nice, and answer any questions you have, and its got a HUGE plugin database that you can download and install too. Even a GUI editor :)
 
S

social_outcast

Guest
lol - thanks guys
elDiablo, thank you, that actually really helped - Id been wondering about jPanels after seeing a small example yet couldnt get a good idea of how they worked from the api. Ill try implimenting them tonight and redefining my current jFrame so it becomes a much smaller componetent, from there all I gotta do is set up labels and collect a shit load of Strings :P lol
thanks again

and thatbloke, Ive got eclipse and am actually tempted to just do that :P but Im feeling I need to do some hard coding on this one cause I really need to know how every line of code works for a presentation on it after easter, but hey, I may take a look
I dont actually use eclipse, I use Blue J, which is a kinda training interface for getting your head around class based in programs and their interactions with each other


-social_outcast
 

thatbloke

Junior Administrator
dude it makes all the code up for you so you can look at exactly how it does it. And though it's not necessarily the way I would code a GUI, it works, and makes it nice and easy to modify any of the properties so its all cool :) And by editing it in the GUI it makes it a hell of a lot easier to use the AbsoluteLayout layout... which allows you to place everything EXACTLY where you want it according to the JFrame's co-ordinate system.

And using Jigloo you can use it both ways... by adding a box in the top GUI editor, you see the code in the bottom. By adding code in at the bottom it dynamically changes the GUI view so you can see what changes your code has made... which makes it nice and easy for getting the sizes right as you can put them on the change the actual numbers in teh code to get it spot on :)
 
S

social_outcast

Guest
lol -well thats a fortnight of code development now - jigloo came in real handy for making the components (unfortunatly my program wouldnt fit into the eclipse system as blu j uses a much more 'lets not bother having a static void main' system to ease you in, but I was able to make a good template and copy the meathod code), thanks guys - well see if this thing gets me through my first year now :P

-S_Outcast
 
E

elDiablo

Guest
Glad to hear you managed to do it :) Any other questions (especially Java), I (and I'm sure others) would be happy to help!
 
S

social_outcast

Guest
w00t!!!
Got my result back and I scored 75%!!!
thats a pass, meaning I dont have to sit the exam this year!!!


Thanks guys, you really helped me there - now I wont fail in life as badly as those who shagged the deputy-PM did :D
 

thatbloke

Junior Administrator
social_outcast said:
Thanks guys, you really helped me there - now I wont fail in life as badly as those who shagged the deputy-PM did :D

OMG I can't stop laughing...
 
Top