Java applet help

Discussion in 'OT Technology' started by Corp, Sep 9, 2004.

  1. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    hey, this is really the first applet i've made and i dont know why the first JLabel is showing up on the left(thats where i want it) and all the others are showing up on the right one spot higher than i want them to be. heres a screenshot:
    [​IMG]

    and the code:
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Excercise4 extends JApplet implements ActionListener
    {
    	JLabel Num1Label, Num2Label, SumLabel, DiffLabel, ProdLabel,
    	AvgLabel, DisLabel, MaxLabel, MinLabel;
    
    	JTextField Num1Field, Num2Field,
    	SumField, DiffField, ProdField,AvgField, DisField, MaxField, MinField;
    
    	JButton Calc;
    
    	public void init()
    	{
    		Container container = getContentPane();
    		container.setLayout(new FlowLayout());
    		Num1Label = new JLabel("Num 1");
    		container.add(Num1Label);
    		Num1Field = new JTextField(20);
    		Num1Field.setEditable(true);
    		container.add(Num1Field);
    		Num1Field.addActionListener(this);
    
    		Num2Label = new JLabel("Num 2");
    		container.add(Num2Label);
    		Num2Field = new JTextField(20);
    		Num2Field.setEditable(true);
    		container.add(Num2Field);
    		Num2Field.addActionListener(this);
    
    		SumLabel = new JLabel("Sum");
    		container.add(SumLabel);
    		SumField = new JTextField(20);
    		SumField.setEditable(false);
    		container.add(SumField);
    
    		container.setLayout(new FlowLayout());
    		DiffLabel = new JLabel("Difference");
    		container.add(DiffLabel);
    		DiffField = new JTextField(20);
    		DiffField.setEditable(false);
    		container.add(DiffField);
    
    		ProdLabel = new JLabel("Product");
    		container.add(ProdLabel);
    		ProdField = new JTextField(20);
    		ProdField.setEditable(false);
    		container.add(ProdField);
    
    		AvgLabel = new JLabel("Avgerage");
    		container.add(AvgLabel);
    		AvgField = new JTextField(20);
    		AvgField.setEditable(false);
    		container.add(AvgField);
    
    		DisLabel = new JLabel("Distance");
    		container.add(DisLabel);
    		DisField = new JTextField(20);
    		DisField.setEditable(false);
    		container.add(DisField);
    
    		MaxLabel = new JLabel("Max");
    		container.add(MaxLabel);
    		MaxField = new JTextField(20);
    		MaxField.setEditable(false);
    		container.add(MaxField);
    
    		MinLabel = new JLabel("Min");
    		container.add(MinLabel);
    		MinField = new JTextField(20);
    		MinField.setEditable(false);
    		container.add(MinField);
    
    	}
    
    	public void actionPerformed(ActionEvent a)
    	{
    		int num1 = Integer.parseInt(Num1Field.getText());
    		int num2 = Integer.parseInt(Num2Field.getText());
    		int sum = num1 + num2;
    		int difference = num1 - num2;
    		int prod = num1 * num2;
    		int avg = (num1 + num2) / 2;
    		int dis = Math.abs(num1 - num2);
    		int max;
    		int min;
    
    		if(num1 > num2)
    		{
    			max = num1;
    			min = num2;
    		}
    
    		else
    		{
    			max = num2;
    			min = num1;
    		}
    
    		SumField.setText("" + sum);
    		DiffField.setText("" + difference);
    		ProdField.setText("" + prod);
    		AvgField.setText("" + avg);
    		DisField.setText("" + dis);
    		MaxField.setText("" + max);
    		MinField.setText("" + min);
    	}
    }
    
    thanks for any help in advance. :)
     
    Last edited: Sep 9, 2004
  2. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    how to get the rest of the JLabels on the left like the first one
     
  3. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    how do i do that?
     
  4. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    got it working, never thought of it being the JTextField sizes.
    final code:
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Excercise4 extends JApplet implements ActionListener
    {
    	JLabel Num1Label, Num2Label, SumLabel, DiffLabel, ProdLabel,
    	AvgLabel, DisLabel, MaxLabel, MinLabel;
    
    	JTextField Num1Field, Num2Field,
    	SumField, DiffField, ProdField,AvgField, DisField, MaxField, MinField;
    
    	JButton Calc;
    
    	public void init()
    	{
    		Container container = getContentPane();
    		container.setLayout(new FlowLayout());
    		Num1Label = new JLabel("Num 1");
    		Num1Field = new JTextField(30);
    		Num1Field.setEditable(true);
    		container.add(Num1Label);
    		container.add(Num1Field);
    		Num1Field.addActionListener(this);
    
    		Num2Label = new JLabel("Num 2");
    		Num2Field = new JTextField(30);
    		Num2Field.setEditable(true);
    		container.add(Num2Label);
    		container.add(Num2Field);
    		Num2Field.addActionListener(this);
    
    		SumLabel = new JLabel("Sum");
    		SumField = new JTextField(30);
    		SumField.setEditable(false);
    		container.add(SumLabel);
    		container.add(SumField);
    
    		DiffLabel = new JLabel("Difference");
    		DiffField = new JTextField(27);
    		DiffField.setEditable(false);
    		container.add(DiffLabel);
    		container.add(DiffField);
    
    		ProdLabel = new JLabel("Product");
    		ProdField = new JTextField(30);
    		ProdField.setEditable(false);
    		container.add(ProdLabel);
    		container.add(ProdField);
    
    		AvgLabel = new JLabel("Avgerage");
    		AvgField = new JTextField(30);
    		AvgField.setEditable(false);
    		container.add(AvgLabel);
    		container.add(AvgField);
    
    		DisLabel = new JLabel("Distance");
    		DisField = new JTextField(30);
    		DisField.setEditable(false);
    		container.add(DisLabel);
    		container.add(DisField);
    
    		MaxLabel = new JLabel("Max");
    		MaxField = new JTextField(32);
    		MaxField.setEditable(false);
    		container.add(MaxLabel);
    		container.add(MaxField);
    
    		MinLabel = new JLabel("Min");
    		MinField = new JTextField(30);
    		MinField.setEditable(false);
    		container.add(MinLabel);
    		container.add(MinField);
    
    	}
    
    	public void actionPerformed(ActionEvent a)
    	{
    		int num1 = Integer.parseInt(Num1Field.getText());
    		int num2 = Integer.parseInt(Num2Field.getText());
    		int sum = num1 + num2;
    		int difference = num1 - num2;
    		int prod = num1 * num2;
    		int avg = (num1 + num2) / 2;
    		int dis = Math.abs(num1 - num2);
    		int max;
    		int min;
    
    		if(num1 > num2)
    		{
    			max = num1;
    			min = num2;
    		}
    
    		else
    		{
    			max = num2;
    			min = num1;
    		}
    
    		SumField.setText("" + sum);
    		DiffField.setText("" + difference);
    		ProdField.setText("" + prod);
    		AvgField.setText("" + avg);
    		DisField.setText("" + dis);
    		MaxField.setText("" + max);
    		MinField.setText("" + min);
    	}
    }
    
     

Share This Page