I am trying to build a triangle calculator that can tell you if you can form a valid triangle, I have reach the calculation part as yet just the part of the gui and that is the part with the error. something is wrong and i cant seem to figure it . probably a logical error somewhere.

I build the gui but it is blank. what it supposed to print is sideA with a txtfield and that goes for b and c. but for some reason it is blank.

can some take a look at my codes and tell me what and where I have gone wrong.

(new Triangle_Calculator()).setVisible(true); this piece of codes here, when I add it in, this is what bring up the gui but it is blank and when i remove it, it doesnt print out. nothing is being printed out. not even the gui comes up

 * 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));

      clear.setBounds(300, 250, 200, 100);

       exit.setBounds(300, 250, 200, 100);

       //row 1
       JLabel area = new JLabel("area of Triangle");
       JTextField sideAfield = new JTextField();

       //row 2
       JLabel sideB = new JLabel("sideB");
        JTextField sideBfield = new JTextField();

       //row 3
       JLabel sideC = new JLabel("sideC");
       JTextField sideCfield = new JTextField();

        //row 5
       JButton calbtn = new JButton("Calculate Area");
       JTextField ans = new JTextField("ans");



        public static void main (String[]args){

           (new Triangle_Calculator()).setVisible(true);
    public void actionPerformed(ActionEvent ae) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

1 Week
Discussion Span
Last Post by divinity02


Yes, using (new Triangle_Calculator()).setVisible(true); java swing did what you asked of it. Knowing that you made your class extends JFrame, yet you are using another frame to get all your components. And when it was done in your class constructor that frame was not set to visible. So, you had setVisible(true) on a wrong frame.

Secondly, you might want to look at your GUI again. And see how you have arranged things.

Note that, you are not using your JPanel to get the Buttons and TextFields. You are just adding then to the frame. The issue with this is that you might just end up seeing only your last button you added even when your gui ends up showing.

And all your TextFields, were not even included.

So, check your code again.

Hope this helps.

Edited by 2teez


^ That's right.

Also, mixing layout managers with setBounds is a recipe for chaos.

And you haven't needed to use frame.getContentPane().add(stuff);for about 10 years. You can just use frame.add(stuff);, or if your class extends JFrame, just add(stuff);

JFrame frame = new JFrame("Calculator");

You are creating a new instance of JFrame to which you are adding all the components you want to show, but you only call setVisible(true) on the class itself.

You haven't added the internally created instance of JFrame (which you don't really need at all) to the layout of the class itself, so it won't show.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.