Results 1 to 9 of 9
- 12-01-2011, 10:16 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 17
- Rep Power
- 0
Java GUI
Hello,
I've had trouble placing my JButtons and JTextArea exactly where I want them. So I was wondering, is there any good GUI Builders out there?
I'm trying to make a button appear in the SouthEast corner and besides that to the west another button. But it's hard positioning them exactly where I want them.
All the different layouts does not seem to do that (GridBagLayout might, but it's way to complicated) So either is there any good GUI Builders out there? Or what could I do?
Thanks,
kris5228
-
Re: Java GUI
You may wish to simply nest simple layouts to get your desired effect, for example a BorderLayout with another JPanel on the bottom that holds the buttons and uses a BoxLayout.
- 12-01-2011, 10:22 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 17
- Rep Power
- 0
Re: Java GUI
Let me just get that right. I make a panel that uses the BorderLayout.SOUTH and then add another layout type inside that layout? How? A link to the docs would be nice
Thanks,
-
Re: Java GUI
If you want one button on the bottom right of your GUI and one on the bottom left, you could create a main JPanel that uses BorderLayout that holds most of the GUI in its BorderLayout.CENTER position, then add another JPanel to the BorderLayout.SOUTH position of the main JPanel that uses BoxLayout (line orientation), Then you could add one JButton, add a Box.createHorizontalGlue() (to create a gap between the two buttons), and then the other JButton.
for example,
A link to the tutorial on this is here: Visual Guide to the Layout ManagersJava Code:import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.*; public class LayoutEg extends JPanel { JButton eastButton = new JButton("East Button"); JButton westButton = new JButton("West Button"); public LayoutEg() { JPanel southPanel = new JPanel(); southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.LINE_AXIS)); southPanel.add(westButton); southPanel.add(Box.createHorizontalGlue()); southPanel.add(eastButton); setLayout(new BorderLayout()); // just to fill in the middle add(Box.createRigidArea(new Dimension(400, 400)), BorderLayout.CENTER); add(southPanel, BorderLayout.SOUTH); } private static void createAndShowGui() { JFrame frame = new JFrame("LayoutEg"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new LayoutEg()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } }
- 12-02-2011, 03:14 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Re: Java GUI
If you need to put things exactly where you need them you could try something like this:
Plan = new JFrame();
Plan.setTitle("Myapp");
Plan.setBounds(100, 100, 1102, 768);
Plan.setIconImage(Toolkit.getDefaultToolkit().getI mage("Files/Logo.jpg"));
Plan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setBackground(new Color(173, 216, 230));
Plan.getContentPane().add(desktopPane, BorderLayout.CENTER);
// Row 1
JLabel lbl1 = new JLabel("Label 1);
lbl1.setFont(new Font("Arial", Font.BOLD, 14));
lbl1.setHorizontalAlignment(JLabel.CENTER);
lbl1.setForeground(Color.white);
lbl1.setBackground(Color.black);
lbl1.setOpaque(true);
lbl1.setBounds(5, 18, 640, 26);// This bit places the field where you want it
desktopPane.add(lbl1);
Obviously this is only for a label but the same goes for buttons text areas etc.
I will proablby be shot at for suggesting this but if you are a control freak like me and dont mind there being lots and lots of code then it works well.
-
Re: Java GUI
If I were the one who had to maintain your code after you left or were fired, yeah, I'd shoot you. Imagine for instance that you've created a complex GUI with a stack of JRadioButtons somewhere in its midsection. Now in your program's upgrade someone tells you that you need to add one more JRadioButton to the stack and remove a component here and there. You'll have to re-arrange not only the radiobuttons, but all other comopnents in the GUI. Next someone complains that your GUI is unusable on their machine because they are required to have their monitor set at such-and-such resolution, and at this settings, your components run together,... I could go on, but what you are suggesting is a potential nightmare.
To the original poster, just don't listen to Paul.
- 12-02-2011, 12:33 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 17
- Rep Power
- 0
Re: Java GUI
Fubarable, I'm not trying to make it be west and east, just 2 buttons to the east of the center at the bottom, but one in the corner and another one just a little bit to the left of it.
Though I understand the basics now, Thanks.
- 12-02-2011, 01:40 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Java GUI
In that case use Fubarables basic structure and instead of Button-Glue-Button do Glue-Button-Button.
That should force the two buttons to the right (glue pushes things away from it).
- 12-02-2011, 02:53 PM #9
Member
- Join Date
- Nov 2011
- Posts
- 17
- Rep Power
- 0


5Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks