![]() |
|
[Java] How do you write clean GUI code? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] How do you write clean GUI code? (/showthread.php?tid=4653182) Pages: Pages
1
2
|
How do you write clean GUI code? - 3mogamer - Jan 24, 2015 Hello Guys, I been programming for a while now and all my code looks neat till you get to the GUIs. For example, I have four buttons, each with its own listeners and secondary features like designs and it looks pretty horrendous. ex. Code /*Initialization of Buttons&Textfield*/Wow, doesn't that code look beautiful. Luckily, I removed the bloat and kept the bare minimum. Usually, I would have several more Controls/Buttons and that would aesthetically ruin the code. Anyway my question here is ... "In what way do you guys write GUIs? (Just plain out instantiating or is there some sort of trick to make it neater?) * - Corios - Jan 24, 2015 Coming from .net I like the way it's done there. GUI goes in a separate file that links an event to a method that you would declare on the class you want to use it from, looks clean and keeps the ugly part away from the nice part. * - ObscureCoder - Jan 24, 2015 I don't tend to do much AWT or Swing GUI programming in Java but I do it more like this: Code import javax.swing.JButton;Some people dislike doing it that way, I prefer it for Swing. I do JavaFX mostly now. * - Trisomy 21 - Jan 24, 2015 Definitely keep any of your output code/GUI away from the actual logic and calculations of your program, but apart from that I agree with you GUI code in Java makes me want to cut myself. * - ObscureCoder - Jan 25, 2015 (Jan 24, 2015 09:35 PM)Trisomy 21 Wrote: Definitely keep any of your output code/GUI away from the actual logic and calculations of your program, but apart from that I agree with you GUI code in Java makes me want to cut myself. Then use dat dank JavaFX: Spoiler (Click to View) * - ☕⚛Austin⚛☕ - Jan 25, 2015 Some people Recccomend MVC it looks neat. Watch this and you will get it. https://www.youtube.com/watch?v=dTVVa2gfht8 300th post * - Corios - Jan 25, 2015 (Jan 25, 2015 10:01 AM)๖ۣۜ---Abyte ---๖ۣۜ Wrote: Some people Recccomend MVC it looks neat. ok, MVC concept isn't needed in a desktop application, that's overkill lol <.< * - nOrr - Jan 25, 2015 (Jan 25, 2015 12:18 PM)Corios Wrote: ok, MVC concept isn't needed in a desktop application, that's overkill lol <.< Uhm... no it's not. In fact it's perfectly suited for a desktop application, and once you get the hang of it, following the MVC design pattern is actually going to get you in a better mood. Heck, I've heard the MVC design pattern can cure depression*. *) Does not actually cure depression. * - 3mogamer - Jan 26, 2015 Thanks, Ill check out MVC out. Is there any other way that we can improve GUI code or is it just God's Wish that GUI-code should make us cut ourselves. * - nOrr - Jan 26, 2015 (Jan 26, 2015 09:46 PM)3mogamer Wrote: Thanks, Ill check out MVC out. Is there any other way that we can improve GUI code or is it just God's Wish that GUI-code should make us cut ourselves. Sure there is. Read the book Clean Code by uncle Bob and you should get an idea of how to do it. * - MasterBugPatch - Jan 27, 2015 They do have online beautifiers for various programming languages. I'm sure you can find one that interest you. * - ObscureCoder - Jan 27, 2015 Wait, so why do you think GUI code is unclean? I find it incredibly straightforward and well structured. * - 3mogamer - Jan 27, 2015 (Jan 27, 2015 01:37 AM)ObscureCoder Wrote: Wait, so why do you think GUI code is unclean? I find it incredibly straightforward and well structured. It feels like how I first started writing Java Code(All in one Class in a single Main method) and looking back at that now is quite different to how I currently write code which is much more OOP. Yes, I also read your example... it was indeed straightforward but the structuring just gives off a feel that I am back at where I started and it doesn't feel right. I will also give the book Clean Code a read but I feel like that focuses overall rather than what I am just looking for right now, GUIs. I'll do more research and ect. Thanks guys for your responses! edit: Actually MVC looks great! Thanks for the suggestion! * - nOrr - Jan 27, 2015 (Jan 27, 2015 09:03 PM)3mogamer Wrote: It feels like how I first started writing Java Code(All in one Class in a single Main method) and looking back at that now is quite different to how I currently write code which is much more OOP. Yes, I also read your example... it was indeed straightforward but the structuring just gives off a feel that I am back at where I started and it doesn't feel right. Why do you think GUI code is much different than code not related to GUI? Imagine that you need three buttons grouped together. Create a class that extends JPanel or similar and either make your class a generic "GroupedButtons" class, or a specific "StartMenuButtonGroup". Then you add buttons to that class, and the class will handle structuring the buttons for that particular area of the GUI. In your boiler plate code you stich together the bigger pieces, and voila: one pretty GUI. Also, in general, whereever in your code you feel like writing a single line comment to describe what happens at a particular place such as "//Set values to JLabels", create a method "setJlabelValues" or similar. Single Responsibility Principle. It's all in the book. Just not specifically using Swing components... Also MVC is wonderful. I love the idea of building up a skeleton and thinking "This method should do this, but I will do the implementation in the future, when it's all structured". :P * - 3mogamer - Jan 28, 2015 (Jan 27, 2015 10:21 PM)nOrr Wrote: -snip- I guess it was just myself structuring it incorrectly. We are two different people and, the way I did it was ugly. Anyway, Thanks for the examples, tips, book suggestion, and everything else! |