1 Attachment(s)
Require Help with Layout (GridBag)
Hello All,
I need help with GridBagLayout. I get stuck until this stage many times and would not know how to proceed further. I need help on laying out the components according to the Layout as attached. WIth the solution given this time I will look into it more deeply and analyse where I went wrong. Please help.
Code is Shown Below
Code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class XCurrencyNumberFormatDemo {
static JPanel jplMain = new JPanel();
static JLabel jlbCurrencyIn = new JLabel();
static JLabel jlbLocale = new JLabel();
static JTextField jtfCurrencyIn = new JTextField();
static JTextField jtfLocale = new JTextField();
static JLabel jlbCurrencyOut = new JLabel();
static JTextField jtfCurrencyOut = new JTextField();
static JButton jbnCancel = new JButton("Cancel");
static JButton jbnConvert = new JButton("Convert");
public static void main(String args[]) {
createGUI();
}
public static void createGUI() {
JFrame frame = new JFrame("XNumberFormat Demo");
frame.setLayout(new GridBagLayout());
frame.setContentPane(jplMain);
jplMain.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 0;
gc.fill = GridBagConstraints.BOTH;
gc.weightx = 0.3;
gc.weighty = 0.3;
jplMain.add(jlbCurrencyIn, gc);
gc.gridx = 1;
gc.gridy = 0;
jplMain.add(jtfCurrencyIn, gc);
gc.gridx = 0;
gc.gridy = 1;
jplMain.add(jlbLocale, gc);
gc.gridx = 1;
gc.gridy = 1;
jplMain.add(jtfLocale, gc);
gc.gridx = 2;
gc.gridy = 1;
jplMain.add(jbnConvert, gc);
gc.gridx = 0;
gc.gridy = 2;
jplMain.add(jlbCurrencyOut, gc);
gc.gridx = 1;
gc.gridy = 2;
jplMain.add(jtfCurrencyOut, gc);
jtfCurrencyOut.setEnabled(false);
gc.gridx = 2;
gc.gridy = 2;
jplMain.add(jbnCancel, gc);
frame.setVisible(true);
frame.setSize(300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Regards,
Hemanth