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

2017/10/3

triangle calculator - MigLayout error

divinity divinity

2017/10/3

#
hi danpost this is another one of the calculator that i have to build. this one is the triangle. I am getting 2 errors with it. I am trying to use the MigLayout api but it is giving me an error saying: it is telling me to create a class with a constructor do I have to create a class called MigLayout with a constructor and why another error am getting is. am trying to do this: frame.getContentPane().add(clear) and I am getting this error: no suitable method found for add(String) method Component.add(PopupMenu) is not applicable (argument mismatch; String cannot be converted to PopupMenu) method Container.add(Component) is not applicable (argument mismatch; String cannot be converted to Component) can u tell me what does that error meant and how can i fix it. here is the codes that I use. and also can u tell me if the calculation is correct. the question is to find the area of a triangle, using a java gui and it is valid triangle. here is the codes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quadratric;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
 
public class Triangle_Calculator extends JFrame implements ActionListener {
     
     
     
    public Triangle_Calculator(){
         
        JButton clear = new JButton("clear");
        JButton exit = new JButton("exit");
         
        Tools tls = new Tools();
         
    }
 
    
    public static void main(String[]args ){
         
        JFrame frame = new JFrame("Testing MigLayout");
        JPanel contentPane = new JPanel(new MigLayout("fillx")); the error is right now
         
        //row 1
        JLabel areaLabel = new JLabel("Area  of Triangle");
        areaLabel.setFont(areaLabel.getFont().deriveFont(16.0f));
        areaLabel.setHorizontalAlignment(JLabel.CENTER);
        contentPane.add(areaLabel,"spanx, growx, wrap");
         
        frame.setTitle("Triangle Calculator");
        frame.getContentPane().add("clear"); the another error is right here
         
        
        //row 2
        JLabel sideA = new JLabel("sideA");
        contentPane.add(sideA, "alignx trailing");
        JTextField sideAfield = new JTextField();
        sideAfield.setColumns(6);
        contentPane.add(sideAfield,"alignx leading wrap");
         
         
        //row3
        JLabel sideB = new JLabel("Side B");
        contentPane.add(sideB, "alignx trailing");
        JTextField sideBfield = new JTextField();
        sideBfield.setColumns(6);
        contentPane.add(sideBfield, "alignx leading, wrap");
         
        //row 4
        JLabel sideC = new JLabel("Side C");
        contentPane.add(sideC, "alignx trailing");
        JTextField sideCfield = new JTextField();
        sideCfield.setColumns(6);
        contentPane.add(sideCfield,"alignx leading, wrap");
         
        //row 5
        JButton calbtn = new JButton("Calculator Area");
        contentPane.add(calbtn, "spanx, grows");
        JTextField ans = new JTextField("ans");
        calbtn.addActionListener(null);
         
         
        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        frame.setVisible(true);
         
    }
 
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equalsIgnoreCase("exit"))
            System.exit(0);
         
        if(e.getActionCommand().equalsIgnoreCase("clear"))
           
             
        if(e.getActionCommand().equalsIgnoreCase("ans"))
             
    }
    private double areaTriangle(double sideA,double sideB,double sideC){
          
        if(isTriangleValid(sideA, sideB,sideC) == true){
             
            System.out.println(areaTriangle(sideA,sideB, sideC));
        }
        return 0;
        
    }
    public boolean isTriangleValid(double sideA,  double sideB, double sideC){
         
         
        if((sideA + sideB) > sideC && (sideA + sideC) > sideB && (sideB + sideC) > sideA){
            return true;
        }
        else{
             
        }
        return false;
    }
     
}
Super_Hippo Super_Hippo

2017/10/4

#
I can't find the MigLayout api (https://docs.oracle.com/javase/8/docs/api/allclasses-noframe.html) so I don't know what that class is. For the second problem, you are trying to use method 'add' with a string as a parameter in line 38, but this method doesn't exist in the class java.awt.Container. If you want to add the JButton with the name 'clear' which you created in the constructor, you need to save the field in the class (not in the constructor, maybe in the main-method but I never understood the main method anyway) and use clear instead of "clear" in line 38.
danpost danpost

2017/10/4

#
About the only thing the main method should be doing is creating a Triangle_Calculator object:
1
2
3
4
public static void main(String[] args)
{
    (new Triangle_Calculator()).setVisible(true);
}
The construction of that object -- everything else you have in the main method -- should be in the constructor of the class (line 16 above is where the constructor starts). Everything else includes from line 31 to line 73 -- move them to replace line 21 (keep lines 18 and 19). That change should also fix the second error as 'clear' would then be defined (after following Hippo's advise on this problem). What is a MigLayout? I could not find any class with that name anywhere? It apparently simply does not exist and you should not be using it. Maybe it was the creating of the Triangle_Calculator object you were trying to do there -- I don't know.
divinity divinity

2017/10/4

#
hi danpost and Super_Hippo since I cant seem to get rid of the errors, I have decided to do over the whole thing but I have add back a few codes from it but now when I am trying to run it just to see how it look, am not getting any output, the frame is not popping up at all. I am getting any kind of output. plzz help. I am almost to the point of frustration. can u tell me what is wrong because before i was getting the gui output here is the codes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quadratric;
 
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
 
public class Triangle_Calculator extends JFrame implements ActionListener {
     
     JButton clear = new JButton("clear");
     JButton exit = new JButton("exit");
     
    
     
    public Triangle_Calculator(){
         
         
        JFrame frame = new JFrame("Calculator");
        frame.setBounds(300, 200, 220, 250);
        frame.setTitle("Triangle Carlculator");
        JPanel jp = new JPanel(new GridLayout(2, 2));
         
       frame.getContentPane().add(clear);
      clear.setBounds(300, 250, 200, 100);
         
       frame.getContentPane().add(exit);
       exit.setBounds(300, 250, 200, 100);
        
       JLabel area = new JLabel("area of Triangle");
       area.setFont(area.getFont().deriveFont(16.0f));
       area.setHorizontalAlignment(JLabel.CENTER);
        
       //row 1
       JLabel sideA = new JLabel("sideA");
       frame.add(sideA);
       JTextField sideAfield = new JTextField();
       sideAfield.setColumns(5);
        
       //row 2
       JLabel sideB = new JLabel("sideB");
       frame.add(sideB);
        JTextField sideBfield = new JTextField();
        sideBfield.setColumns(5);
        
       //row 3
       JLabel sideC = new JLabel("sideC");
       frame.add(sideC);
       JTextField sideCfield = new JTextField();
       sideCfield.setColumns(5);
        
        //row 5
       JButton calbtn = new JButton("Calculate Area");
       frame.add(calbtn);
       JTextField ans = new JTextField("ans");
       calbtn.addActionListener(this);
       this.setVisible(true);
        
        
    }
 
        public static void main (String[]args){
             
        }
    @Override
    public void actionPerformed(ActionEvent ae) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
 
     
}
Super_Hippo Super_Hippo

2017/10/4

#
Your main method is empty, so nothing happens. Create a Triangle_Calculator object as danpost showed you.
divinity divinity

2017/10/4

#
I create it and now am getting an empty frame
danpost danpost

2017/10/5

#
On line 23, you should be creating a JPanel -- not a JFrame; and then you need to add the panel to the Triangle_Calculator JFrame.
divinity divinity

2017/10/5

#
how do you do that? because I am getting a lot of errors, and when I put pan.getContentPane() it is not coming up for some reason
danpost danpost

2017/10/5

#
divinity wrote...
how do you do that? because I am getting a lot of errors, and when I put pan.getContentPane() it is not coming up for some reason
Just add to panel -- not to content pane.
You need to login to post a reply.