-2

I'm having trouble adding my other JLabels and JTextFields in the JFrame.

In my code I am trying to calculate the area of a triangle and want the user to add in the numbers for the sides. I have inplemented the JLabel variables and JTextFields but the only one that shows up is "Side c". I have added EntryPanels and added the tag labels to them but nothing seems to work.

Any help would be appreciated:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

/**
 * Calculate the area of a triangle first finding the sides then using Heron's Formula
 * @author Chris Condreay
 */
public class TriangleCalculator extends JPanel {
    
    /* Instance Variables */
    private JButton calcButton;
    private JTextField sideAField;
    private JTextField sideBField;
    private JTextField sideCField;
    private JLabel triangleAreaLabel;

    /* Constructor */
    public TriangleCalculator()
    {
        this.calcButton = new JButton("Calculate");
        this.sideAField = new JTextField(10);
        this.sideBField = new JTextField(10);
        this.sideCField = new JTextField(10);
        this.triangleAreaLabel = new JLabel("Triangle Area: ");

        /* Setup listeners */
        ActionListener myListener = new TriangleListener();
        this.calcButton.addActionListener(myListener);
        this.sideAField.addActionListener(myListener);
        this.sideBField.addActionListener(myListener);
        this.sideCField.addActionListener(myListener);

        /* Sides Entry Panel */
        JPanel sideAEntryPanel = new JPanel();
        JPanel sideBEntryPanel = new JPanel();
        JPanel sideCEntryPanel = new JPanel();
        // sideAEntryPanel.setLayout(new BoxLayout(sideAEntryPanel, BoxLayout.X_AXIS));
        // sideBEntryPanel.setLayout(new BoxLayout(sideBEntryPanel, BoxLayout.X_AXIS));
        // sideCEntryPanel.setLayout(new BoxLayout(sideCEntryPanel, BoxLayout.X_AXIS));
        JLabel sideATagLabel = new JLabel("Side a: ");
        JLabel sideBTagLabel = new JLabel("Side b: ");
        JLabel sideCTagLabel = new JLabel("Side c: ");
        
        sideAEntryPanel.add(sideATagLabel);
        sideBEntryPanel.add(sideBTagLabel);
        sideCEntryPanel.add(sideCTagLabel);
        sideAEntryPanel.add(sideAField);
        sideBEntryPanel.add(sideBField);
        sideCEntryPanel.add(sideCField);

        /* Sides Panel */
        JPanel resultSubPanel = new JPanel();
        resultSubPanel.setLayout(new BoxLayout(resultSubPanel, BoxLayout.X_AXIS));
        resultSubPanel.add(triangleAreaLabel);

        /* Setup top-level panel */
        setPreferredSize(new Dimension(400, 150));
        setLayout(new BorderLayout());
        add(this.calcButton, BorderLayout.EAST);
        add(sideAEntryPanel, BorderLayout.WEST);
        add(sideBEntryPanel, BorderLayout.WEST);
        add(sideCEntryPanel, BorderLayout.WEST);

        add(resultSubPanel, BorderLayout.SOUTH);
    }

    private void updateCalculation(double a, double b, double c)
    {
        double trianglePerimeter = a + b + c;
        double triangleSide = trianglePerimeter / 2;
        double triangleArea = Math.sqrt(triangleSide * (triangleSide - a) * (triangleSide - b) * (triangleSide - c));

        DecimalFormat myFormat = new DecimalFormat("#,###,###.###");

        triangleAreaLabel.setText("Volume: " + myFormat.format(triangleArea));
    }

    /* Button listener */
    private class TriangleListener implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e) {

            String userSideA = sideAField.getText();
            String userSideB = sideBField.getText();
            String userSideC = sideCField.getText();
            try {
                double a = Double.parseDouble(userSideA);
                double b = Double.parseDouble(userSideB);
                double c = Double.parseDouble(userSideC);
                if (a < 0  ) {
                    JOptionPane.showMessageDialog(null, "Please enter positive values only!");
                    sideAField.setText("");
                }
                else if (b < 0) {
                    JOptionPane.showMessageDialog(null, "Please enter positive values only!");
                    sideBField.setText("");
                }
                else if (c < 0) {
                    JOptionPane.showMessageDialog(null, "Please enter positive values only!");
                    sideCField.setText("");
                } 
                else {
                    updateCalculation(a, b, c);
                }
            } catch (NumberFormatException err) {
                JOptionPane.showMessageDialog(null, "Please enter numbers only!");
                sideAField.setText("");
                sideBField.setText("");
                sideCField.setText("");
            }
            
        }
        
    }

    /* Main Method */
    public static void main(String[] args) 
    {
        JFrame myFrame = new JFrame("Triangle Calculator - Chris Condreay");

        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /* Add a panel to the frame */
        JPanel myPanel = new TriangleCalculator();
        myFrame.getContentPane().add(myPanel);

        myFrame.pack();

        myFrame.setVisible(true);
    }
}

JFrame

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 2
    `add(sideAEntryPanel, BorderLayout.WEST);` <- You can't add multiple components to the same location on a `BorderLayout`. If you really want three components on the `WEST` side, place them into a seperate container and then add that container to the position – MadProgrammer Apr 25 '23 at 21:22

1 Answers1

1
add(wrapper, BorderLayout.WEST);
add(sideBEntryPanel, BorderLayout.WEST);
add(sideCEntryPanel, BorderLayout.WEST);

You can't add multiple components to the same position in BorderLayout. BorderLayout will only manage the last component added to each position.

Instead, you need to use a seperate container to contain the components you want and then add that to the desired position, for example...

JPanel sidePane = new JPanel(new GridLayout(3, -1));
sidePane.add(sideAEntryPanel);
sidePane.add(sideBEntryPanel);
sidePane.add(sideCEntryPanel);
        
add(sidePane, BorderLayout.WEST);

This is also known as "compound layouts", which you're already doing.

I would also recommend, when you're brave enough, to have a look at How to Use GridBagLayout

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366