如何在 JFrame 中添加多个 JLabel [重复]

问题描述 投票:0回答:1

我在 JFrame 中添加其他 JLabel 和 JTextFields 时遇到问题。

在我的代码中,我试图计算三角形的面积并希望用户添加边的数字。我已经安装了 JLabel 变量和 JTextFields,但唯一出现的是“C 面”。我已经添加了 EntryPanels 并向它们添加了标签标签,但似乎没有任何效果。

任何帮助将不胜感激:

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

java swing jframe jlabel jtextfield
1个回答
0
投票
add(wrapper, BorderLayout.WEST);
add(sideBEntryPanel, BorderLayout.WEST);
add(sideCEntryPanel, BorderLayout.WEST);

您不能在

BorderLayout
中的同一个位置添加多个组件。
BorderLayout
将只管理添加到每个位置的最后一个组件。

相反,您需要使用一个单独的容器来包含您想要的组件,然后将其添加到所需的位置,例如...

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

这也称为“复合布局”,您已经在做。

我还建议,当你足够勇敢时,看看如何使用 GridBagLayout

© www.soinside.com 2019 - 2024. All rights reserved.