I've just started learning Java, and after a few chapters of theory, I decided to try a bit of GUI to see if it helped drive the theory home. I think it's working a bit, but I've got a problem that I"m sure demonstrates a pretty fundamental part of Java prgramming.
The source below is from a GUI doc that I've been doing, using FlowLayout manager. For some reason, all the panels that I've created are controlled by the layout manager of the last panel, and have no individual control. As far as I"m aware, every panel is a new object with a new FlowLayout manager, so each should be individually controllable, but it isn't working. I'd really appreciate any help - and please don't laugh - it's only a play thing to try to teach me some of the basics...
public CurrencyConverter() {
super("Currency Converter");
setSize(550, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//declare all new panels
JPanel topList = new JPanel();
JPanel selectCurrencyFrom = new JPanel();
JPanel selectCurrencyTo = new JPanel();
JPanel availability = new JPanel();
JPanel exchangeDetails = new JPanel();
JPanel changeAmount = new JPanel();
JPanel actionButtons = new JPanel();
JPanel result = new JPanel();
JPanel checkBox = new JPanel();
//declare all new lists
JList convertFrom = new JList(currency);
JList convertTo = new JList(reverseCurrency);
//declare all new text fields
JTextField exchangeRate = new JTextField(8);
JTextField amountToChange = new JTextField(8);
JTextField conversionResult = new JTextField(8);
//declare all new labels
JLabel select = new JLabel("Please select the currency to convert FROM and TO in the boxes below:");
JLabel subjectToAvailability = new JLabel("* Subject to Availability - first come first served.");
JLabel rate = new JLabel("Enter Current Exchange Rate (2 Decimal Places):");
JLabel enterAmount = new JLabel("Enter the Amount of Money You Want to Change: ");
JLabel conversionSum = new JLabel("The Conversion Result is: ");
//declare all new buttons and check boxes
JButton calculate = new JButton("Calculate");
JButton cancel = new JButton("Cancel");
JCheckBox please = new JCheckBox("Oh yes please - inundate me with useless information!", true);
//add new flow layouts
FlowLayout topListFlow = new FlowLayout(FlowLayout.CENTER, 10, 10);
FlowLayout currencyFromFlow = new FlowLayout(FlowLayout.CENTER, 10, 10);
FlowLayout toListFlow = new FlowLayout(FlowLayout.CENTER, 100, 10);
FlowLayout subjectToAvailabilityFlow = new FlowLayout(FlowLayout.CENTER, 100, 10);
FlowLayout exchangeRateFlow = new FlowLayout(FlowLayout.RIGHT, 100, 10);
FlowLayout amountFlow = new FlowLayout(FlowLayout.CENTER, 100, 10);
FlowLayout buttonFlow = new FlowLayout(FlowLayout.LEFT, 10, 10);
FlowLayout answerFlow = new FlowLayout(FlowLayout.CENTER, 100, 10);
FlowLayout pleaseFlow = new FlowLayout(FlowLayout.CENTER, 100, 10);
//create select currency From list
getContentPane().setLayout(currencyFromFlow);
convertFrom.setVisibleRowCount(7);
JScrollPane scroller1 = new JScrollPane(convertFrom);
selectCurrencyFrom.add(scroller1);
getContentPane().add(selectCurrencyFrom);
setVisible(true);
//create select currency To list
getContentPane().setLayout(toListFlow);
convertTo.setVisibleRowCount(7);
JScrollPane scroller2 = new JScrollPane(convertTo);
selectCurrencyTo.add(scroller2);
getContentPane().add(selectCurrencyTo);
setVisible(true);
It'll take me some time to decipher what's going on there, but a mixture of layouts was something I'd aready looked at, so it'll be good learning for me. Thank you again, that's just great
This needs a follow up - I've just compiled it and realised all the stuff you did. That bit of source has given me enough learning to keep me busy for a week or two Really, grateful, thanks a million!
Bookmarks