I've never done Java GUI's before. After much trouble I've gotten my GUI to look how I want, however it feels as if my code is very inefficient, as if I'm going about this the wrong way?
//main panels
private JPanel westPanel = new JPanel(new BorderLayout());
private JPanel eastPanel = new JPanel(new BorderLayout());
//door panels
private JPanel doorsPanel = new JPanel(new BorderLayout());
private JPanel doorsGrid = new JPanel(new GridBagLayout());
private JPanel doorButtons = new JPanel(new FlowLayout());
//light panels
private JPanel lightsPanel = new JPanel(new BorderLayout());
private JPanel lightsGrid = new JPanel(new GridBagLayout());
private JPanel lightTimerGrid = new JPanel(new GridBagLayout());
private JPanel lightButtons = new JPanel(new FlowLayout());
//list panels
private JPanel listPanel = new JPanel(new BorderLayout());
private JPanel listButtons = new JPanel(new GridBagLayout());
private JLabel itemDesc = new JLabel("Item Name - Item Price");
private JLabel totalPrice = new JLabel("Total Price: ");
doorsPanel.setBorder(BorderFactory.createTitledBorder("Manage Doors: "));
doorsPanel.add(doorsGrid, BorderLayout.NORTH);
doorsPanel.add(doorButtons, BorderLayout.SOUTH);
lightsPanel.setBorder(BorderFactory.createTitledBorder("Manage Lights: "));
lightsPanel.add(lightsGrid, BorderLayout.NORTH);
lightsPanel.add(lightButtons, BorderLayout.SOUTH);
lightsPanel.add(lightTimerGrid, BorderLayout.CENTER);
listPanel.setBorder(BorderFactory.createTitledBorder("Manage Shopping List: "));
listPanel.add(listButtons, BorderLayout.EAST);
westPanel.add(doorsPanel, BorderLayout.NORTH);
westPanel.add(lightsPanel, BorderLayout.CENTER);
eastPanel.add(listPanel, BorderLayout.SOUTH);
add(westPanel, BorderLayout.WEST);
add(eastPanel, BorderLayout.EAST);
Current GUI layout
-
4\$\begingroup\$ What's wrong with your code? It seems OK to me. \$\endgroup\$Eng.Fouad– Eng.Fouad2013年04月02日 21:34:29 +00:00Commented Apr 2, 2013 at 21:34
-
2\$\begingroup\$ If it looks like you want it, it should be ok. I like the design... \$\endgroup\$Craig Smith– Craig Smith2013年04月02日 21:39:47 +00:00Commented Apr 2, 2013 at 21:39
-
5\$\begingroup\$ Gui layouts almost always take a large amount of code. \$\endgroup\$BevynQ– BevynQ2013年04月02日 21:40:45 +00:00Commented Apr 2, 2013 at 21:40
-
2\$\begingroup\$ I would split the different parts into separate classes (at least three: Doors, Lights, and Shopping List), and a top class that contain these classes. Also if this is your first UI (not just Java UI) I would read a bit about the MVC pattern. \$\endgroup\$osundblad– osundblad2013年04月02日 21:50:01 +00:00Commented Apr 2, 2013 at 21:50
-
\$\begingroup\$ to expand on the comment by @osundblad Doors, Lights and ShoppingList might be Observer's. \$\endgroup\$Thufir– Thufir2013年04月03日 11:21:16 +00:00Commented Apr 3, 2013 at 11:21
1 Answer 1
Your code seems perfectly fine!
However...
If all of this code is in the same block (e.g., a constructor or a layout builder method), I would recommend that you break up the layout code into smaller functions. That will improve readability and code structure a bit.
Also, you should definately familiarize yourself with some GUI builders for Java (in my case it was WindowBuilder for Eclipse). Writing layout code by hand can be a pain and a source of bugs that you won't be able to resolve easily.