Как сделать так, чтобы мой JLabel появился на моем интерфейсе? - PullRequest
0 голосов
/ 25 апреля 2020

В triangleUI () {...} я обновил содержимое панели, однако, когда я попробовал ту же реализацию кода для squareUI () {...}, JLabel для этого метода не появляется? Я застрял с этой проблемой в течение часа. Я чувствовал себя уверенно, просто скопировав и вставив метод triangleUI () после того, как увидел, что он выглядит правильно, однако я не могу из-за проблемы squareUI (). Вот мой код для справки

public class Main extends JFrame {

private JFrame mainFrame;
private JPanel parentPanel;
private JPanel sidePanel;

private JLayeredPane layer;

//Side Menu
private JPanel aboutSidePanel, triangleSidePanel, squareSidePanel, rectangleSidePanel, circleSidePanel, titlePanel;
private JLabel aboutLabel, triangleLabel, squareLabel, rectangleLabel, circleLabel, titleLabel;

//Main Screens
private JPanel aboutScreenPanel, triangleScreenPanel, squareScreenPanel, rectangleScreenPanel, circleScreenPanel;
private JLabel aboutScreenLabel, triangleScreenLabel, squareScreenLabel, rectangleScreenLabel, circleScreenLabel;

private Color sidePanelColor = new Color(76, 41, 211);
private Color sidePanelTextColor = new Color(204, 204, 204);
private Color mainPanelColor = new Color(135, 112, 225);
private Color mainPanelTextColor = new Color (255, 255, 255);

private Font sidePanelText = new Font("Century Gothic", Font.BOLD, 20);
private Font titleText = new Font("Arial Narrow", Font.BOLD, 22);
private Font mainScreenText = new Font("Century Gothic", Font.BOLD, 25);


public static void main(String[] args) {
    Main show = new Main();
}

public Main() {
    mainFrame = new JFrame("Geometric Shapes Computation");

    layer = new JLayeredPane();
    layer.setBounds(0, 0, 700, 400);


    //Title Configurations
    titleLabel = new JLabel("GEOMETRIC SHAPES");
    titleLabel.setFont(titleText);
    titleLabel.setBackground(sidePanelColor);
    titleLabel.setForeground(sidePanelTextColor);

    titlePanel = new JPanel();
    titlePanel.add(titleLabel);
    titlePanel.setBackground(Color.BLACK);
    titlePanel.setBounds(0, 35, 200, 40);


    //About Configurations
    aboutLabel = new JLabel("About");
    aboutLabel.setFont(sidePanelText);
    aboutLabel.setBackground(sidePanelColor);
    aboutLabel.setForeground(sidePanelTextColor);
    aboutLabel.setVisible(true);

    aboutSidePanel = new JPanel();
    aboutSidePanel.add(aboutLabel);
    aboutSidePanel.setBackground(sidePanelColor);
    aboutSidePanel.setBounds(0, 100, 200, 40);
    aboutSidePanel.addMouseListener(new MouseEventHandler());

    aboutScreenLabel = new JLabel("ABOUT");
    aboutScreenLabel.setFont(mainScreenText);
    aboutScreenLabel.setForeground(mainPanelTextColor);
    aboutScreenLabel.setVisible(true);

    aboutScreenPanel = new JPanel();
    aboutScreenPanel.setBackground(mainPanelColor);
    aboutScreenPanel.add(aboutScreenLabel);
    aboutScreenPanel.setBounds(200, 0, 500, 50);
    aboutScreenPanel.setVisible(false);

    //Triangle Configurations
    triangleLabel = new JLabel("Triangle");
    triangleLabel.setFont(sidePanelText);
    triangleLabel.setBackground(sidePanelColor);
    triangleLabel.setForeground(sidePanelTextColor);
    triangleLabel.setVisible(true);

    triangleSidePanel = new JPanel();
    triangleSidePanel.add(triangleLabel);
    triangleSidePanel.setBackground(sidePanelColor);
    triangleSidePanel.setBounds(0, 150, 200, 40);
    triangleSidePanel.addMouseListener(new MouseEventHandler());

    triangleScreenPanel = new JPanel();
    triangleScreenPanel.setBounds(200,0,500,400);

    //Square Configurations
    squareLabel = new JLabel("Square");
    squareLabel.setFont(sidePanelText);
    squareLabel.setBackground(sidePanelColor);
    squareLabel.setForeground(sidePanelTextColor);
    squareLabel.setVisible(true);

    squareSidePanel = new JPanel();
    squareSidePanel.add(squareLabel);
    squareSidePanel.setBackground(sidePanelColor);
    squareSidePanel.setBounds(0, 200, 200, 40);
    squareSidePanel.addMouseListener(new MouseEventHandler());

    squareScreenPanel = new JPanel();
    squareScreenPanel.setBounds(200,0,500,400);

    //Rectangle Configurations
    rectangleLabel = new JLabel("Rectangle");
    rectangleLabel.setFont(sidePanelText);
    rectangleLabel.setBackground(sidePanelColor);
    rectangleLabel.setForeground(sidePanelTextColor);
    rectangleLabel.setVisible(true);

    rectangleSidePanel = new JPanel();
    rectangleSidePanel.add(rectangleLabel);
    rectangleSidePanel.setBackground(sidePanelColor);
    rectangleSidePanel.setBounds(0, 250, 200, 40);
    rectangleSidePanel.addMouseListener(new MouseEventHandler());

    rectangleScreenPanel = new JPanel();

    //Circle Configurations
    circleLabel = new JLabel("Circle");
    circleLabel.setFont(sidePanelText);
    circleLabel.setBackground(sidePanelColor);
    circleLabel.setForeground(sidePanelTextColor);
    circleLabel.setVisible(true);

    circleSidePanel = new JPanel();
    circleSidePanel.add(circleLabel);
    circleSidePanel.setBackground(sidePanelColor);
    circleSidePanel.setBounds(0, 300, 200, 40);
    circleSidePanel.addMouseListener(new MouseEventHandler());

    circleScreenPanel = new JPanel();

    //Parent Panel Configurations
    parentPanel = new JPanel();
    parentPanel.setSize(700, 400);

    //Parent Panel Configurations
    sidePanel = new JPanel();
    sidePanel.setBounds(0, 0, 200, 700);
    sidePanel.setBackground(sidePanelColor);

    triangleUI();
    squareUI();
    rectangleUI();
    circleUI();

    layer.add(parentPanel, JLayeredPane.DEFAULT_LAYER);
    layer.add(sidePanel, JLayeredPane.PALETTE_LAYER);
    layer.add(titlePanel, JLayeredPane.MODAL_LAYER);
    layer.add(aboutSidePanel, JLayeredPane.MODAL_LAYER);
    layer.add(triangleSidePanel, JLayeredPane.MODAL_LAYER);
    layer.add(squareSidePanel, JLayeredPane.MODAL_LAYER);
    layer.add(rectangleSidePanel, JLayeredPane.MODAL_LAYER);
    layer.add(circleSidePanel, JLayeredPane.MODAL_LAYER);

    layer.add(aboutScreenPanel, JLayeredPane.POPUP_LAYER);
    layer.add(triangleScreenPanel, JLayeredPane.POPUP_LAYER);
    layer.add(squareScreenPanel, JLayeredPane.POPUP_LAYER);
    layer.add(rectangleScreenPanel, JLayeredPane.POPUP_LAYER);
    layer.add(circleScreenPanel, JLayeredPane.POPUP_LAYER);

    mainFrame.add(layer);
    mainFrame.pack();
    mainFrame.setLocationRelativeTo(null);
    mainFrame.setSize(700, 400);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setResizable(false);
    mainFrame.setVisible(true);
}

private void triangleUI(){
    triangleScreenLabel = new JLabel("AREA OF A TRIANGLE");
    triangleScreenLabel.setFont(mainScreenText);
    triangleScreenLabel.setForeground(mainPanelColor);
    triangleScreenLabel.setVisible(true);
    triangleScreenLabel.setHorizontalAlignment(SwingConstants.CENTER);

    Font formulaSubDetails = new Font("Century Gothic", Font.ITALIC, 13);
    Font formulaMainDetails = new Font("Century Gothic", Font.BOLD, 18);
    Font input = new Font("Century Gothic", Font.BOLD, 15);

    JPanel showInfo = new JPanel();
    showInfo.setLayout(new GridLayout(5,1));
    showInfo.setBackground(new Color(134, 136, 138));

    JLabel line1 = new JLabel("Let the formula to solve for the area of a 3 sided Triangle be:");
    line1.setFont(formulaSubDetails);
    line1.setHorizontalAlignment(SwingConstants.LEFT);

    JLabel line2 = new JLabel("\u221As(s-a)(s-b)(s-c)");
    line2.setFont(formulaMainDetails);
    line2.setHorizontalAlignment(SwingConstants.CENTER);

    JLabel line3 = new JLabel("Where:");
    line3.setFont(formulaSubDetails);
    line3.setHorizontalAlignment(SwingConstants.LEFT);

    JLabel line4 = new JLabel("\ta, b, & c  - sides of the triangles");
    line4.setFont(formulaSubDetails);
    line4.setHorizontalAlignment(SwingConstants.LEFT);

    JLabel line5 = new JLabel("\t    s      - semi perimeter as solved by (perimeter / 2)");
    line5.setFont(formulaSubDetails);
    line5.setHorizontalAlignment(SwingConstants.LEFT);

    showInfo.add(line1);
    showInfo.add(line2);
    showInfo.add(line3);
    showInfo.add(line4);
    showInfo.add(line5);
    showInfo.setVisible(true);

    JPanel showInputField = new JPanel();
    showInputField.setLayout(new GridLayout(4,2));

    JLabel inputLabel1 = new JLabel("Enter the length of the 1st side");
    inputLabel1.setFont(input);
    JLabel inputLabel2 = new JLabel("Enter the length of the 2nd side");
    inputLabel2.setFont(input);
    JLabel inputLabel3 = new JLabel("Enter the length of the 3rd side");
    inputLabel3.setFont(input);

    JTextField inputField1 = new JTextField(10);
    JTextField inputField2 = new JTextField(10);
    JTextField inputField3 = new JTextField(10);

    JButton outputButton = new JButton("Calculate the Area");
    JTextField outputField = new JTextField(10);
    outputField.setEditable(false);

    showInputField.add(inputLabel1);
    showInputField.add(inputField1);
    showInputField.add(inputLabel2);
    showInputField.add(inputField2);
    showInputField.add(inputLabel3);
    showInputField.add(inputField3);
    showInputField.add(outputButton);
    showInputField.add(outputField);

    triangleScreenPanel.add(triangleScreenLabel);
    triangleScreenPanel.add(showInfo);
    triangleScreenPanel.add(showInputField);

    triangleScreenPanel.setVisible(false);
}

private void squareUI(){
    squareScreenLabel = new JLabel("AREA OF A SQUARE");
    squareScreenLabel.setFont(mainScreenText);
    squareScreenLabel.setForeground(mainPanelColor);
    squareScreenLabel.setVisible(true);
    squareScreenLabel.setHorizontalAlignment(SwingConstants.CENTER);

    Font formulaSubDetails = new Font("Century Gothic", Font.ITALIC, 13);
    Font formulaMainDetails = new Font("Century Gothic", Font.BOLD, 18);
    Font input = new Font("Century Gothic", Font.BOLD, 15);

    JPanel showInfo = new JPanel();
    showInfo.setLayout(new GridLayout(4,1));
    showInfo.setBackground(new Color(134, 136, 138));

    JLabel line1 = new JLabel("Let the formula to solve for the area of a Square be:");
    line1.setFont(formulaSubDetails);
    line1.setHorizontalAlignment(SwingConstants.LEFT);

    JLabel line2 = new JLabel("s * s");
    line2.setFont(formulaMainDetails);
    line2.setHorizontalAlignment(SwingConstants.CENTER);

    JLabel line3 = new JLabel("Where:");
    line3.setFont(formulaSubDetails);
    line3.setHorizontalAlignment(SwingConstants.LEFT);

    JLabel line4 = new JLabel("\ts  - length of the sides of the square");
    line4.setFont(formulaSubDetails);
    line4.setHorizontalAlignment(SwingConstants.LEFT);

    showInfo.add(line1);
    showInfo.add(line2);
    showInfo.add(line3);
    showInfo.add(line4);
    showInfo.setVisible(true);

    JPanel showInputField = new JPanel();
    showInputField.setLayout(new GridLayout(2,2));

    JLabel inputLabel1 = new JLabel("Enter the length of the side");
    inputLabel1.setFont(input);

    JTextField inputField1 = new JTextField(10);

    JButton outputButton = new JButton("Calculate the Area");
    JTextField outputField = new JTextField(10);
    outputField.setEditable(false);

    showInputField.add(inputLabel1);
    showInputField.add(inputField1);
    showInputField.add(outputButton);
    showInputField.add(outputField);

    squareScreenPanel.add(squareScreenLabel);
    squareScreenPanel.add(showInfo);
    squareScreenPanel.add(showInputField);

    squareScreenPanel.setVisible(false);

}

private void rectangleUI(){
    rectangleScreenLabel = new JLabel("AREA OF A RECTANGLE");
    rectangleScreenLabel.setFont(mainScreenText);
    rectangleScreenLabel.setForeground(mainPanelTextColor);
    rectangleScreenLabel.setVisible(true);

    rectangleScreenPanel.setBounds(200, 0, 500, 600);

    JPanel header = new JPanel();
    header.setBackground(mainPanelColor);
    header.add(squareScreenLabel);
    header.setBounds(0, 0, 500, 50);
    header.setVisible(false);

    Font formulaSubDetails = new Font("Century Gothic", Font.ITALIC, 15);
    Font formulaMainDetails = new Font("Century Gothic", Font.BOLD, 25);

    JPanel showInfo = new JPanel();
    showInfo.setBackground(new Color(134, 136, 138));
    showInfo.setBounds(15, 65, 470, 100);

    JLabel line1 = new JLabel("Let the formula to solve for the area of a 3 sided Triangle be:");
    line1.setFont(formulaSubDetails);
    line1.setHorizontalAlignment(SwingConstants.LEFT);

    JLabel line2 = new JLabel("\u221As(s-a)(s-b)(s-c)");
    line2.setFont(formulaMainDetails);
    line2.setHorizontalAlignment(SwingConstants.CENTER);

    JLabel line3 = new JLabel("Where:");
    line3.setFont(formulaSubDetails);
    line3.setHorizontalAlignment(SwingConstants.LEFT);

    JLabel line4 = new JLabel("\ta, b, & c  - sides of the triangles");
    line4.setFont(formulaSubDetails);
    line4.setHorizontalAlignment(SwingConstants.LEFT);

    JLabel line5 = new JLabel("\t    s      - semi perimeter as solved by (perimeter / 2)");
    line5.setFont(formulaSubDetails);
    line5.setHorizontalAlignment(SwingConstants.LEFT);

    showInfo.add(line1);
    showInfo.add(line2);
    showInfo.add(line3);
    showInfo.add(line4);
    showInfo.add(line5);
    showInfo.setVisible(true);

    rectangleScreenPanel.add(header);
    rectangleScreenPanel.add(showInfo);
    rectangleScreenPanel.setVisible(false);

    layer.add(rectangleScreenPanel, JLayeredPane.POPUP_LAYER);


}

private void circleUI(){
    circleScreenLabel = new JLabel("AREA OF A CIRCLE");
    circleScreenLabel.setFont(mainScreenText);
    circleScreenLabel.setForeground(mainPanelTextColor);
    circleScreenLabel.setVisible(true);

    circleScreenPanel.setBounds(200, 0, 500, 600);

    JPanel header = new JPanel();
    header.setBackground(mainPanelColor);
    header.add(squareScreenLabel);
    header.setBounds(0, 0, 500, 50);
    header.setVisible(false);

    Font formulaSubDetails = new Font("Century Gothic", Font.ITALIC, 15);
    Font formulaMainDetails = new Font("Century Gothic", Font.BOLD, 25);

    JPanel showInfo = new JPanel();
    showInfo.setBackground(new Color(134, 136, 138));
    showInfo.setBounds(15, 65, 470, 100);

    JLabel line1 = new JLabel("Let the formula to solve for the area of a 3 sided Triangle be:");
    line1.setFont(formulaSubDetails);
    line1.setHorizontalAlignment(SwingConstants.LEFT);

    JLabel line2 = new JLabel("\u221As(s-a)(s-b)(s-c)");
    line2.setFont(formulaMainDetails);
    line2.setHorizontalAlignment(SwingConstants.CENTER);

    JLabel line3 = new JLabel("Where:");
    line3.setFont(formulaSubDetails);
    line3.setHorizontalAlignment(SwingConstants.LEFT);

    JLabel line4 = new JLabel("\ta, b, & c  - sides of the triangles");
    line4.setFont(formulaSubDetails);
    line4.setHorizontalAlignment(SwingConstants.LEFT);

    JLabel line5 = new JLabel("\t    s      - semi perimeter as solved by (perimeter / 2)");
    line5.setFont(formulaSubDetails);
    line5.setHorizontalAlignment(SwingConstants.LEFT);

    showInfo.add(line1);
    showInfo.add(line2);
    showInfo.add(line3);
    showInfo.add(line4);
    showInfo.add(line5);
    showInfo.setVisible(true);

    circleScreenPanel.add(header);
    circleScreenPanel.add(showInfo);
    circleScreenPanel.setVisible(false);

    layer.add(circleScreenPanel, JLayeredPane.POPUP_LAYER);


}
//Mouse Event Handlers
public class MouseEventHandler implements MouseListener {
    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == aboutSidePanel) {
            btn_AboutPressed(e);
        }
        if (e.getSource() == triangleSidePanel) {
            btn_TrianglePressed(e);
        }
        if (e.getSource() == squareSidePanel) {
            btn_SquarePressed(e);
        }
        if (e.getSource() == rectangleSidePanel) {
            btn_RectanglePressed(e);
        }
        if (e.getSource() == circleSidePanel) {
            btn_CirclePressed(e);
        }
    }

    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}

private void btn_AboutPressed(MouseEvent event) {
    setColor(aboutSidePanel);
    resetColor(triangleSidePanel);
    resetColor(squareSidePanel);
    resetColor(rectangleSidePanel);
    resetColor(circleSidePanel);

    /**
     * separate the methods to their gui's
     */
    showShapeScreen(aboutScreenPanel);
    hideShapeScreen(triangleScreenPanel);
    hideShapeScreen(squareScreenPanel);
    hideShapeScreen(rectangleScreenPanel);
    hideShapeScreen(circleScreenPanel);


}

private void btn_TrianglePressed(MouseEvent event) {
    setColor(triangleSidePanel);
    resetColor(aboutSidePanel);
    resetColor(squareSidePanel);
    resetColor(rectangleSidePanel);
    resetColor(circleSidePanel);

    showShapeScreen(triangleScreenPanel);
    hideShapeScreen(aboutScreenPanel);
    hideShapeScreen(squareScreenPanel);
    hideShapeScreen(rectangleScreenPanel);
    hideShapeScreen(circleScreenPanel);
}

private void btn_SquarePressed(MouseEvent event) {
    setColor(squareSidePanel);
    resetColor(aboutSidePanel);
    resetColor(triangleSidePanel);
    resetColor(rectangleSidePanel);
    resetColor(circleSidePanel);

    showShapeScreen(squareScreenPanel);
    hideShapeScreen(aboutScreenPanel);
    hideShapeScreen(triangleScreenPanel);
    hideShapeScreen(rectangleScreenPanel);
    hideShapeScreen(circleScreenPanel);
}

private void btn_RectanglePressed(MouseEvent event) {
    setColor(rectangleSidePanel);
    resetColor(aboutSidePanel);
    resetColor(triangleSidePanel);
    resetColor(squareSidePanel);
    resetColor(circleSidePanel);

    rectangleUI();

    showShapeScreen(rectangleScreenPanel);
    hideShapeScreen(aboutScreenPanel);
    hideShapeScreen(squareScreenPanel);
    hideShapeScreen(triangleScreenPanel);
    hideShapeScreen(circleScreenPanel);
}

private void btn_CirclePressed(MouseEvent event) {
    setColor(circleSidePanel);
    resetColor(aboutSidePanel);
    resetColor(triangleSidePanel);
    resetColor(squareSidePanel);
    resetColor(rectangleSidePanel);

    circleUI();

    showShapeScreen(circleScreenPanel);
    hideShapeScreen(aboutScreenPanel);
    hideShapeScreen(squareScreenPanel);
    hideShapeScreen(rectangleScreenPanel);
    hideShapeScreen(triangleScreenPanel);
}

//Screen Toggles

private void showShapeScreen(JPanel panel) {
    panel.setVisible(true);
}

private void hideShapeScreen(JPanel panel) {
    panel.setVisible(false);
}

//Attribute Toggles

private void setColor(JPanel panel) {
    panel.setBackground(new Color(135, 112, 225));
}

private void resetColor(JPanel panel) {
    panel.setBackground(new Color(76, 41, 211));
}

}

...