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*/
Button button1= new Button("Button 1");
Button button2= new Button("Button 2");
Button button3= new Button("Button 3");
Button button4= new Button("Button 4");
TextField field1 = new TextField("");
/*Addition of Listeners*/
button1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
/*Omitted or else this code will look crappier*/
}
});
button2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
/*Omitted or else this code will look crappier*/
}
});
button3.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
/*Omitted or else this code will look crappier*/
}
});
button4.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
/*Omitted or else this code will look crappier*/
}
});
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?)
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.
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;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class Main extends JFrame {
public Main() {
super("Sexy Title");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
getContentPane().add(panel);
JButton button = new JButton("Swag 4 lyf");
panel.add(button);
setVisible(true);
button.addActionListener(listener -> {
System.out.println("Sexy msg");
});
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(() -> new Main());
}
}
Some people dislike doing it that way, I prefer it for Swing. I do JavaFX mostly now.
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.
(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:
![[Image: yfY4kj9.png]](https://i.imgur.com/yfY4kj9.png)
Jan 25, 2015 10:01 AM
(This post was last modified: Jan 25, 2015 10:02 AM by ☕⚛Austin⚛☕.)
Some people Recccomend MVC it looks neat.
Watch this and you will get it.
https://www.youtube.com/watch?v=dTVVa2gfht8
300th post
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
(Jan 25, 2015 10:01 AM)☕⚛Austin⚛☕ Wrote: Some people Recccomend MVC it looks neat.
Watch this and you will get it.
https://www.youtube.com/watch?v=dTVVa2gfht8
300th post
ok, MVC concept isn't needed in a desktop application, that's overkill lol <.<
(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.
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.
(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.
DO NOT DEAL WITH THIS MEMBER
They do have online beautifiers for various programming languages. I'm sure you can find one that interest you.
Wait, so why do you think GUI code is unclean? I find it incredibly straightforward and well structured.
Jan 27, 2015 09:03 PM
(This post was last modified: Jan 27, 2015 09:41 PM by 3mogamer.)
(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!
Jan 27, 2015 10:21 PM
(This post was last modified: Jan 27, 2015 10:22 PM by nOrr.)
(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.
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!
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
(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!
|