This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2017/10/17

class or method

1
2
3
divinity divinity

2017/10/17

#
I have a question. if you want to instantiate a class or method, I forget how they does call it like this: Triangle angle = new Triangle(); and you want to do something like this: angle.setSize(400, 400), angle.setVisible(true) but it is not showing up can someone tell me why and what make it not come up.
danpost danpost

2017/10/17

#
You need, at least, to show the code of the Triangle class.
divinity divinity

2017/10/17

#
hi here it is.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.*;

public class Triangle_Calculator extends JFrame implements ActionListener {
   
   JTextField side1, side2, side3, displayArea, displayArea2;
   
    public Triangle_Calculator(){
        
        
      this.setTitle("Triangle Calculator");
      this.setSize(400, 400);
      this.setVisible(true);
      JTextField exit = new JTextField("exit");
      this.getContentPane().add(exit);
      exit.setBounds(200, 190,100,75);
      
      JLabel lblA = new JLabel("1", SwingConstants.RIGHT);
      side1 = new JTextField(5);
      JLabel lblB = new JLabel("2", SwingConstants.RIGHT);
      side2= new JTextField(5);
      JLabel lblC = new JLabel("3", SwingConstants.RIGHT);
      side3 = new JTextField(5);
      JLabel lblresults = new JLabel("Area", SwingConstants.RIGHT);
      displayArea = new JTextField(5);
      displayArea.setEditable(false);
      JLabel lblresults2 = new JLabel("", SwingConstants.RIGHT);
      displayArea2 = new JTextField(5);
      displayArea2.setEditable(false);
      JButton compute = new JButton("compute");
      compute.addActionListener(this);
      
      Container con = new Container();
      con.setBackground(Color.BLUE);
      JPanel jp = new JPanel();
      jp.setLayout(new GridLayout(6, 4, 6, 6));
      jp.add(lblA);
      jp.add(side1);
      jp.add(lblB);
      jp.add(side2);
      jp.add(lblC);
      jp.add(side3);
      jp.add(lblresults);
      jp.add(displayArea);
      jp.add(lblresults2);
      jp.add(displayArea2);
      con.add(con, BorderLayout.CENTER);
      con.add(jp, BorderLayout.SOUTH);
      
    }
    
    
     @Override
    public void actionPerformed(ActionEvent ae) {
        
        double A = Double.parseDouble(side1.getText());
        double B = Double.parseDouble(side2.getText());
        double C = Double.parseDouble(side3.getText());
        
        double area = calculatearea(1, 2,3);
        double area2 = calculatearea2(1, 2, 3);
        DecimalFormat fmt = new DecimalFormat("0,#");
        
        
    }
   

    private double calculatearea(double  side1, double  side2, double  side3) {
        
        double sqroot_val;
        double area;
        double s ;
        
        s = ( 1 + 2 + 3)/2;
        sqroot_val = 2 * (side1 + side2 +side3);
        area = Math.sqrt(s *(s - side1) * (s - side2) * (s - side3));
        
        return area;
    }

    private double calculatearea2( double side1, double  side2, double  side3) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
     public static void main(String[]args){
     
          Triangle angle = new Triangle();
          
          
  }
danpost danpost

2017/10/17

#
The name of the class is 'Triangle_Calculator' -- not 'Triangle'. Also, when you instantiate the class, the new object will set its own size and make itself visible (see lines 18 and 19).
divinity divinity

2017/10/17

#
hi danpost well I fix that error part and thank you for pointing that out to me and now I am getting a different error. I run the program and it is printing a blank frame, what is the cause of that. Hear is the code again. when I run it, it shows a runtime error
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.*;

public class Triangle_Calculator extends JFrame implements ActionListener {
   
   JTextField side1, side2, side3, displayArea, displayArea2;
   
    public Triangle_Calculator(){
        
        
      this.setTitle("Triangle Calculator");
      this.setSize(400, 400);
      this.setVisible(true);
      
      
      
      JLabel lbl1 = new JLabel("1", SwingConstants.RIGHT);
      side1 = new JTextField(5);
      JLabel lbl2 = new JLabel("2", SwingConstants.RIGHT);
      side2= new JTextField(5);
      JLabel lbl3 = new JLabel("3", SwingConstants.RIGHT);
      side3 = new JTextField(5);
      JLabel lblArea = new JLabel("Area", SwingConstants.RIGHT);
      displayArea = new JTextField(5);
      displayArea.setEditable(false);
      JLabel lblresults2 = new JLabel("", SwingConstants.RIGHT);
      displayArea2 = new JTextField(5);
      displayArea2.setEditable(false);
      JButton compute = new JButton("compute");
      compute.addActionListener(this);
      
      
      Container con = new Container();
      con.setBackground(Color.BLUE);
      JPanel jp = new JPanel();
      jp.setLayout(new GridLayout(6, 4, 6, 6));
      jp.add(lbl1);
      jp.add(side1);
      jp.add(lbl2);
      jp.add(side2);
      jp.add(lbl3);
      jp.add(side3);
      jp.add(lblArea);
      jp.add(displayArea);
      jp.add(lblresults2);
      jp.add(displayArea2);
      con.add(con, BorderLayout.CENTER); this is an error.
      con.add(jp, BorderLayout.SOUTH);
      
    }
    
    
     @Override
    public void actionPerformed(ActionEvent ae) {
        
        double A = Double.parseDouble(side1.getText());
        double B = Double.parseDouble(side2.getText());
        double C = Double.parseDouble(side3.getText());
        
        double area = calculatearea(1, 2,3);
        double area2 = calculatearea2(1, 2, 3);
        DecimalFormat fmt = new DecimalFormat("0,#");
        
        
    }
   

    private double calculatearea(double  side1, double  side2, double  side3) {
        
        double sqroot_val;
        double area;
        double s ;
        
        s = ( 1 + 2 + 3)/2;
        sqroot_val = 2 * (side1 + side2 +side3);
        area = Math.sqrt(s *(s - side1) * (s - side2) * (s - side3));
        
        return area;
    }

    private double calculatearea2( double side1, double  side2, double  side3) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
     public static void main(String[]args){
     
          Triangle_Calculator trical = new Triangle_Calculator(); this is where one of the error is
          trical.setBounds(300, 300,350, 250);
          trical.setDefaultCloseOperation(EXIT_ON_CLOSE);
          
  }
divinity divinity

2017/10/17

#
the runtime error is showing on line 53 and line 93.
danpost danpost

2017/10/17

#
It is line 53 that is troublesome. You cannot add a container to itself.
divinity divinity

2017/10/17

#
hi danpost I fix the error but it is still giving me a blank jframe. can u tell me what is the cause.
danpost danpost

2017/10/17

#
divinity wrote...
it is still giving me a blank jframe.
You have yet to add a component to the Triangle_Calculator (JFrame) object (also referred to as 'this' in the constructor)
divinity divinity

2017/10/17

#
what component. I tried adding this.setbound() dah didnt work, I am at loss
danpost danpost

2017/10/17

#
Did you try:
this.add(jp);
at around line 53?
divinity divinity

2017/10/17

#
this getting the blank jframe even though I tried u have there
danpost danpost

2017/10/17

#
divinity wrote...
this getting the blank jframe even though I tried u have there
It is there. You can verify by manually widening the frame. Add the following line after the last one I gave you:
pack();
divinity divinity

2017/10/17

#
hi dan am getting a cannot find symbol error or it is telling me to create a field
danpost danpost

2017/10/17

#
divinity wrote...
am getting a cannot find symbol error or it is telling me to create a field
Where? What symbol cannot it find?
There are more replies on the next page.
1
2
3