• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Can anyone help with GUI stuff in Java?

LuDaCriS66

Platinum Member
Sorry but I've been working on this work all day and I'm 95% done but I just can't get my GUI looking right. That's all I need to do.. is to get the button on the right spot.

Basically, I need my GUI to look like this
But instead, it looks like this with the button to the right

But this is my code here and it's not coming out right.
The calculate button is acting like it's in the same object as the text fields. Any ideas?

initialAmountLabel = new JLabel("Initial investment amount", LEFT);
initialAmountField = new InputJTextField("", 10);
interestRateLabel = new JLabel("Annual interest rate in %", LEFT);
interestRateField = new InputJTextField("", 5);
output = new JTextArea(20,60); // 10 rows and 20 columns
output.setEditable(false);
output.setFont(new Font("Courier", Font.PLAIN, 11));

JPanel p = new JPanel(new GridLayout(2,2));
p.add(initialAmountLabel);
p.add(initialAmountField);
p.add(interestRateLabel);
p.add(interestRateField);

Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(p);
cp.add(calculate);
cp.add(new JScrollPane(output));


If I make the calc button and the TextArea the same object, the calc button is the same size as the TextArea....

any help is appreciated
 
I just touched on this a little bit, but why don't you try a different layout? Instead of the GridLayout, why don't you try the N, S, E, W one? Or another one?
 
I would use a BorderLayout for the main frame content pane. Add p at BorderLayout.NORTH. Your choice of GridLayout is appropriate for p.

Create a new panel to contain the button and the scroll pane. This panel should be added at BorderLayout.CENTER.

Within this panel, I would also use the BorderLayout layout manager, with the button at BorderLayout.WEST and the scroll pane at BorderLayout.CENTER.
 
Originally posted by: manly
I would use a BorderLayout for the main frame content pane. Add p at BorderLayout.NORTH. Your choice of GridLayout is appropriate for p.

Create a new panel to contain the button and the scroll pane. This panel should be added at BorderLayout.CENTER.

Within this panel, I would also use the BorderLayout layout manager, with the button at BorderLayout.WEST and the scroll pane at BorderLayout.CENTER.

yeah, that's what I meant.
 
Back
Top