Results 1 to 6 of 6
  1. #1
    eiramae is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Question GridBagLayout.... help!!!!

    can somebody tell me what's wrong with my codings, because everytime i run it to the browser it turns out blank....

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.BorderFactory;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    
    public class CurrencyConverter extends Applet implements ActionListener
    
    {	
    
    	JLabel header;
    	JLabel amountlabel;
    	JLabel fromlabel;
    	JLabel exchangeratelabel;
    	JLabel calcamtlabel;
    	JTextField amountfield;
    	JTextField exchangeratefield;
    	JTextField calcamtfield;
    	 Choice fromchoice;
    	 	double[] value={1.00000,43.5241,101.830,4.85908,10.1931,2980.70,29.8823,1.45021,1.45006,5.48783,0.43355,17.9677,5.52844,11.3770,4.40437,4.85908,1.44881,252.415,201.184,117.540,0.58340,1,434.32,0.52105,2.55983,29.8823,45.3683,0.31801,11.7450,5.85440,2.91617,148.510,31,434.3,29.2834,76.4078,22.3162,177.516,123.253,6.49595,0.97210,1.59249,7.85630,0.62392,76.4078};
    
    	Button calculate;
    	JPanel panel;
    	JPanel panel1;
    
    	public void init()
    	{
    	setBackground(Color.green);
    
    	JPanel panel = new JPanel(new GridBagLayout());
    	GridBagConstraints a = new GridBagConstraints();
    
    	JLabel header = new JLabel("Currency Conversion Calculator");
    	a.gridx = 0;
    	a.gridy = 0;
    	a.insets = new Insets(10,10,10,10);
    	header.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    	panel.add(header, a);
    
    	JPanel panel1 = new JPanel(new GridBagLayout());
    	GridBagConstraints b = new GridBagConstraints();
    	
    	JLabel amountlabel = new JLabel("Amount of Dollars to be Exchange");
    	b.gridx = 0;
    	b.gridy = 1;
    	b.insets = new Insets(10,10,10,10);
    	amountlabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    	panel1.add(amountlabel, a);
    
    	JTextField amountfield = new JTextField(15);
    	b.gridx = 0;
    	b.gridy = 2;
    	b.insets = new Insets(10,10,10,10);
    	amountfield.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    	panel1.add(amountfield, a);
    		
    	JLabel fromlabel = new JLabel("Convert From:");
    	b.gridx = 0;
    	b.gridy = 3;
    	b.insets = new Insets(10,10,10,10);
    	fromlabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    	panel1.add(fromlabel, a);
    
    	fromchoice = new Choice();
    	b.gridx = 0;
    	b.gridy = 4;
    	b.insets = new Insets(10,10,10,10);
    	fromlabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    	panel1.add(fromchoice,a);
    		fromchoice.addItem("United States  US Dollar          (USD)");
    		fromchoice.addItem("Philippines    Philippine Peso    (PHP)");
    		fromchoice.addItem("Albania        Albanian Lek       (ALL)");
    		fromchoice.addItem("Andorra        Andorran Franc     (ADF)");
    		fromchoice.addItem("Austria        Austrian Schilling (ATS)");
    		fromchoice.addItem("Belarus        Belarusian Ruble   (BYR)");
    		fromchoice.addItem("Belgium        Belgian Franc      (BEF)");
    		fromchoice.addItem("Bosnia         Bosnia Mark        (BAM)");
    		fromchoice.addItem("Bulagria       Bulgarian Lev      (BGN)");
    		fromchoice.addItem("Cyprus         Cypriot Pound      (CYP)");
    		fromchoice.addItem("Czech Republic Czech Koruna       (CZK)");
    		fromchoice.addItem("Denmark        Danish Krone       (DKK)");
    		fromchoice.addItem("Estonia        Estonian Kroon     (EEK)");
    		fromchoice.addItem("Findland       Finnish Markka     (FIM)");
    		fromchoice.addItem("Germany        German Mark        (DEM)");
    		fromchoice.addItem("Greece         Greek Drachma      (GRD)");
    		fromchoice.addItem("Hungary        Hungarian Forint   (HUF)");
    		fromchoice.addItem("Iceland        Iceland Krona      (ISK)");
    		fromchoice.addItem("Ireland        Irish Punt         (IEP)");
    		fromchoice.addItem("Italy          Italian Lira       (ITL)");
    		fromchoice.addItem("Latvia         Latvian Lats       (LVL)");
    		fromchoice.addItem("Lithuania      Lithuanian Litas   (LTL)");
    		fromchoice.addItem("Luxemburg      Luxembourg Franc   (LUF)");
    		fromchoice.addItem("Macedonia      Macedonian Denar   (MKD)");
    		fromchoice.addItem("Malta          Maltese Lira       (MTL)");
    		fromchoice.addItem("Moldova        Moldovan Leu       (MDL)");
    		fromchoice.addItem("Norway         Norwegian Kroner   (NOK)");
    		fromchoice.addItem("Poland         Polish Zloty       (PLN)");
    		fromchoice.addItem("Portugal       Portuguese Escudo  (PTE)");
    		fromchoice.addItem("Romania        Romanian Lei       (ROL)");
    		fromchoice.addItem("Russia         Russian Rouble     (RUB)");
    		fromchoice.addItem("Serbia         Serbian Dinar      (RSD)");
    		fromchoice.addItem("Slovakia       Slovak Koruna      (SKK)");
    		fromchoice.addItem("Slovenia       Slovenian Tolar    (SIT)");
    		fromchoice.addItem("Spain          Spanish Peseta     (ESP)");
    		fromchoice.addItem("Sweden         Swedish Krona      (SEK)");
    		fromchoice.addItem("Switzerland    Swiss Franc        (CHF)");
    		fromchoice.addItem("Turkey         Turkish Lira       (TRY)");
    		fromchoice.addItem("Ukraine        Ukraine Hryvnia    (UAH)");
    		fromchoice.addItem("United Kingdom British Pound      (GBP)");
    		fromchoice.addItem("Yugoslavia     Yugoslav Dinar     (YUN)");
    
    
    	JPanel panel2 = new JPanel(new GridBagLayout());
    	GridBagConstraints c = new GridBagConstraints();
    
    	calculate = new Button("Calculate");
    	c.gridx = 0;
    	c.gridy = 6;
    	c.insets = new Insets(10,10,10,10);
    	calculate.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    	panel2.add(calculate);
    	calculate.addActionListener(this);
    
    	JPanel panel3 = new JPanel(new GridBagLayout());
    	GridBagConstraints d = new GridBagConstraints();
    
    	JLabel exchangeratelabel = new JLabel("Exchange Rate");
    	d.gridx = 0;
    	d.gridy = 7;
    	d.insets = new Insets(10,10,10,10);
    	exchangeratelabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    	panel3.add(exchangeratelabel);
    
    	JTextField exchangeratefield = new JTextField(15);
    	d.gridx = 0;
    	d.gridy = 8;
    	d.insets = new Insets(10,10,10,10);
    	exchangeratefield.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    	panel3.add(exchangeratefield);
    
    	JLabel calcamtlabel = new JLabel("Calculated Amount");
    	d.gridx = 0;
    	d.gridy = 9;
    	d.insets = new Insets(10,10,10,10);
    	calcamtlabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));	
    	panel3.add(calcamtlabel);
    
    	JTextField calcamtfield = new JTextField(15);
    	d.gridx = 0;
    	d.gridy = 10;
    	d.insets = new Insets(10,10,10,10);
    	calcamtfield.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    	panel3.add(calcamtfield);
    		
    	panel.setVisible(true);
    	panel1.setVisible(true);
    	panel2.setVisible(true);
    	panel3.setVisible(true);
    	}
    
    	
    
    	public void actionPerformed(ActionEvent e)
    	{
    		double cal,total;
    	
    		int actval = fromchoice.getSelectedIndex();
    		total = value[actval];
    
    		int af = Integer.parseInt(amountfield.getText());
    
    		cal=af*total;
    
    		exchangeratefield.setText(""+total);		
    		calcamtfield.setText(""+cal);
    		
    
    	}
    }
    Attached Thumbnails Attached Thumbnails GridBagLayout.... help!!!!-output.jpg  

  2. #2
    eiramae is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Question

    I solve the gridbag, but my problem now is the button doesn't seems listen to the action I set..... pls. help!!!!!

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.BorderFactory;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    
    public class CurrencyConverter extends Applet implements ActionListener
    
    {	
    
    	JLabel header;
    	JLabel amountlabel;
    	JLabel fromlabel;
    	JLabel exchangeratelabel;
    	JLabel calcamtlabel;
    	JTextField amountfield;
    	JTextField exchangeratefield;
    	JTextField calcamtfield;
    	 Choice fromchoice;
    	 	double[] value={1.00000,43.5241,101.830,4.85908,10.1931,2980.70,29.8823,1.45021,1.45006,5.48783,0.43355,17.9677,5.52844,11.3770,4.40437,4.85908,1.44881,252.415,201.184,117.540,0.58340,1,434.32,0.52105,2.55983,29.8823,45.3683,0.31801,11.7450,5.85440,2.91617,148.510,31,434.3,29.2834,76.4078,22.3162,177.516,123.253,6.49595,0.97210,1.59249,7.85630,0.62392,76.4078};
    
    	JButton calculate;
    
    	public void init()
    	{
    	setBackground(Color.green);
    
    	JPanel panel = new JPanel(new GridBagLayout());
    	GridBagConstraints a = new GridBagConstraints();
    
    		JLabel header = new JLabel("Currency Conversion Calculator");
    		a.gridx = 0;
    		a.gridy = 0;
    		a.insets = new Insets(10,10,10,10);
    		header.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    		panel.add(header,a);
    
    		add(panel, BorderLayout.NORTH);
    
    	JPanel panel1 = new JPanel(new GridBagLayout());
    	GridBagConstraints b = new GridBagConstraints();
    	
    		JLabel amountlabel = new JLabel("Amount of Dollars to be Exchange");
    		b.gridx = 1;
    		b.gridy = 1;
    		b.insets = new Insets(10,10,10,10);
    		amountlabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    		panel1.add(amountlabel,b);
    
    		JLabel fromlabel = new JLabel("Convert From:");
    		b.gridx = 1;
    		b.gridy = 2;
    		b.insets = new Insets(10,10,10,10);
    		fromlabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    		panel1.add(fromlabel,b);
    
    	add(panel1, BorderLayout.WEST);
    
    	JPanel panel4 = new JPanel(new GridBagLayout());
    	GridBagConstraints f = new GridBagConstraints();
    		
    		JTextField amountfield = new JTextField(15);
    		f.gridx = 2;
    		f.gridy = 1;
    		f.insets = new Insets(10,10,10,10);
    		amountfield.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    		panel4.add(amountfield,f);
    
    	fromchoice = new Choice();
    	f.gridx = 2;
    	f.gridy = 2;
    	f.insets = new Insets(10,10,10,10);
    	fromlabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    	panel4.add(fromchoice,f);
    		fromchoice.addItem("United States  US Dollar          (USD)");
    		fromchoice.addItem("Philippines    Philippine Peso    (PHP)");
    		fromchoice.addItem("Albania        Albanian Lek       (ALL)");
    		fromchoice.addItem("Andorra        Andorran Franc     (ADF)");
    		fromchoice.addItem("Austria        Austrian Schilling (ATS)");
    		fromchoice.addItem("Belarus        Belarusian Ruble   (BYR)");
    		fromchoice.addItem("Belgium        Belgian Franc      (BEF)");
    		fromchoice.addItem("Bosnia         Bosnia Mark        (BAM)");
    		fromchoice.addItem("Bulagria       Bulgarian Lev      (BGN)");
    		fromchoice.addItem("Cyprus         Cypriot Pound      (CYP)");
    		fromchoice.addItem("Czech Republic Czech Koruna       (CZK)");
    		fromchoice.addItem("Denmark        Danish Krone       (DKK)");
    		fromchoice.addItem("Estonia        Estonian Kroon     (EEK)");
    		fromchoice.addItem("Findland       Finnish Markka     (FIM)");
    		fromchoice.addItem("Germany        German Mark        (DEM)");
    		fromchoice.addItem("Greece         Greek Drachma      (GRD)");
    		fromchoice.addItem("Hungary        Hungarian Forint   (HUF)");
    		fromchoice.addItem("Iceland        Iceland Krona      (ISK)");
    		fromchoice.addItem("Ireland        Irish Punt         (IEP)");
    		fromchoice.addItem("Italy          Italian Lira       (ITL)");
    		fromchoice.addItem("Latvia         Latvian Lats       (LVL)");
    		fromchoice.addItem("Lithuania      Lithuanian Litas   (LTL)");
    		fromchoice.addItem("Luxemburg      Luxembourg Franc   (LUF)");
    		fromchoice.addItem("Macedonia      Macedonian Denar   (MKD)");
    		fromchoice.addItem("Malta          Maltese Lira       (MTL)");
    		fromchoice.addItem("Moldova        Moldovan Leu       (MDL)");
    		fromchoice.addItem("Norway         Norwegian Kroner   (NOK)");
    		fromchoice.addItem("Poland         Polish Zloty       (PLN)");
    		fromchoice.addItem("Portugal       Portuguese Escudo  (PTE)");
    		fromchoice.addItem("Romania        Romanian Lei       (ROL)");
    		fromchoice.addItem("Russia         Russian Rouble     (RUB)");
    		fromchoice.addItem("Serbia         Serbian Dinar      (RSD)");
    		fromchoice.addItem("Slovakia       Slovak Koruna      (SKK)");
    		fromchoice.addItem("Slovenia       Slovenian Tolar    (SIT)");
    		fromchoice.addItem("Spain          Spanish Peseta     (ESP)");
    		fromchoice.addItem("Sweden         Swedish Krona      (SEK)");
    		fromchoice.addItem("Switzerland    Swiss Franc        (CHF)");
    		fromchoice.addItem("Turkey         Turkish Lira       (TRY)");
    		fromchoice.addItem("Ukraine        Ukraine Hryvnia    (UAH)");
    		fromchoice.addItem("United Kingdom British Pound      (GBP)");
    		fromchoice.addItem("Yugoslavia     Yugoslav Dinar     (YUN)");
    
    	add(panel4, BorderLayout.NORTH);
    
    	JPanel panel3 = new JPanel(new GridBagLayout());
    	GridBagConstraints d = new GridBagConstraints();
    
    		JLabel exchangeratelabel = new JLabel("Exchange Rate");
    		d.gridx = 1;
    		d.gridy = 4;
    		d.insets = new Insets(10,10,10,10);
    		exchangeratelabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    		panel3.add(exchangeratelabel,d);
    
    		JLabel calcamtlabel = new JLabel("Calculated Amount");
    		d.gridx = 1;
    		d.gridy = 5;
    		d.insets = new Insets(10,10,10,10);
    		calcamtlabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));	
    		panel3.add(calcamtlabel,d);
    
    	add(panel3, BorderLayout.EAST);
    
    	JPanel panel5 = new JPanel(new GridBagLayout());
    	GridBagConstraints e = new GridBagConstraints();
    
    
    		JTextField calcamtfield = new JTextField(15);
    		e.gridx = 2;
    		e.gridy = 5;
    		e.insets = new Insets(10,10,10,10);
    		calcamtfield.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    		panel5.add(calcamtfield,e);
    
    		JTextField exchangeratefield = new JTextField(15);
    		e.gridx = 2;
    		e.gridy = 4;
    		e.insets = new Insets(10,10,10,10);
    		exchangeratefield.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    		panel5.add(exchangeratefield,e);
    
    	add(panel5, BorderLayout.WEST);
    
    
    		JButton calculate = new JButton("Calculate");
    		c.gridx = 1;
    		c.gridy = 3;
    		c.insets = new Insets(10,10,10,10);
    		calculate.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    		calculate.addActionListener(this);
    	add(calculate);
    
    
    	}
    
    	
    
    	public void actionPerformed(ActionEvent e)
    	{
    		double cal,total;
    	
    		int actval = fromchoice.getSelectedIndex();
    		total = value[actval];
    
    		int af = Integer.parseInt(amountfield.getText());
    
    		cal=af*total;
    
    		exchangeratefield.setText(""+total);		
    		calcamtfield.setText(""+cal);
    		
    
    	}
    }
    Attached Thumbnails Attached Thumbnails GridBagLayout.... help!!!!-output4.jpg  

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    1) Add debug (println) statements to your actionPerformed method to see what does happen when it's called, what the state of the variables are.
    2) Don't mix Swing and AWT components unless you have a very good reason for doing so (you don't), and you really know what you're doing when walking this minefield.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  5. #5
    eiramae is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    i cannot use printIn because i was trying to put the answer in the textField.....

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by eiramae View Post
    i cannot use printIn because i was trying to put the answer in the textField.....
    Yes you can use println. It's not for your final program, but you can use it now to help you debug your program, and then when fixed take it out. But more importantly, are you seeing any error messages when you try to run this program?

    And again, get rid of all the AWT components in your code -- change Applet to JApplet, for instance, don't use Choice but either a JTable or a JList, ...

Similar Threads

  1. GridBagLayout Help
    By Zman3359 in forum AWT / Swing
    Replies: 0
    Last Post: 02-21-2011, 08:39 PM
  2. help with gridbaglayout
    By robertbob in forum AWT / Swing
    Replies: 5
    Last Post: 05-18-2010, 04:14 AM
  3. GridBagLayout
    By carderne in forum New To Java
    Replies: 8
    Last Post: 01-25-2009, 02:06 PM
  4. GridBagLayout
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 01-15-2009, 08:54 PM
  5. abt gridbaglayout
    By pinky in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •