简体   繁体   English

JLabel 不会在按钮单击时显示

[英]JLabel wont show up on button click

This is my first GUI so any advice is good advice here but mainly I'm just trying to work out why my errorLabel wont show up.这是我的第一个 GUI,所以任何建议在这里都是很好的建议,但主要是我只是想弄清楚为什么我的errorLabel不会出现。 I've tried setVisible , setOpaque , having the location set manually and finally (as you can see below) just making it its own panel to sit on and it still won't show up on button press.我试过setVisiblesetOpaque ,手动设置位置,最后(如下所示)只是让它自己的面板坐下来,它仍然不会显示在按钮按下。

Any ideas?有任何想法吗?

package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.*;


/**
 *
 * @author User
 */
public class AddChair extends JFrame {

    private final int FRAME_HEIGHT = 600;
    private final int FRAME_WIDTH = 400;
    private final String ADD_CHAIR_TITLE = "Chair";
    private final int TEXT_BOX_WIDTH = FRAME_WIDTH / 4;
    private final int TEXT_BOX_HEIGHT = 25;

    private JPanel basePanel;
    private JPanel leftPanel;
    private JPanel rightPanel;
    private JPanel lowerPanel;
    private JPanel itemIDPanel;
    private JPanel woodTypePanel;
    private JPanel armrestPanel;
    private JPanel quantityPanel;
    private JPanel itemIDInputPanel;
    private JPanel woodTypeInputPanel;
    private JPanel quantityInputPanel;
    private JPanel armrestInputPanel;
    private JPanel confirmCancelPanel;
    private JPanel errorPanel;

    private JTextField itemIDTextField;
    private JTextField quantityTextField;

    private JRadioButton walnut;
    private JRadioButton oak;
    private JRadioButton yes;
    private JRadioButton no;
    
    private JButton confirmButton;
    private JButton cancelButton;

    public AddChair() {
        frameSetup();
        add(basePanel());
    }

    private void frameSetup() {   // sets up base frame
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setVisible(true);
        setAlwaysOnTop(true);
        setTitle(ADD_CHAIR_TITLE);
        setLocationRelativeTo(null);
    }

    private JPanel basePanel() {   //sets up a base panel to arrange other panels on
        basePanel = new JPanel(new BorderLayout());
        basePanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
        basePanel.add(leftPanel(), BorderLayout.WEST);
        basePanel.add(rightPanel(), BorderLayout.EAST);
        basePanel.add(lowerPanel(), BorderLayout.SOUTH);
        return basePanel;
    }

    private JPanel leftPanel() {   //sets up the left panel which will contain labels
        leftPanel = new JPanel();
        leftPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4));
        leftPanel.setVisible(true);
        labelPanels();
        labels();
        return leftPanel;
    }

    private void labelPanels() {//sets up the panels for the labels
        itemIDPanel = new JPanel();
        woodTypePanel = new JPanel();
        armrestPanel = new JPanel();
        quantityPanel = new JPanel();

        itemIDPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        woodTypePanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        armrestPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        quantityPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));

        itemIDPanel.setVisible(true);
        woodTypePanel.setVisible(true);
        armrestPanel.setVisible(true);
        quantityPanel.setVisible(true);

        leftPanel.add(itemIDPanel);
        leftPanel.add(woodTypePanel);
        leftPanel.add(armrestPanel);
        leftPanel.add(quantityPanel);
    }

    private void labels() {   //sets up the and adds the labels for the user 
        JLabel itemIDLabel = new JLabel("Item ID:");
        JLabel woodTypeLabel = new JLabel("Wood Type:");
        JLabel armrestLabel = new JLabel("Armrest:");
        JLabel quantityLabel = new JLabel("Quantity:");

        itemIDLabel.setVisible(true);
        woodTypeLabel.setVisible(true);
        armrestLabel.setVisible(true);
        quantityLabel.setVisible(true);

        itemIDPanel.add(itemIDLabel);
        woodTypePanel.add(woodTypeLabel);
        armrestPanel.add(armrestLabel);
        quantityPanel.add(quantityLabel);
    }

    private JPanel rightPanel() {   // sets up the right panel which will contain the user input objects
        rightPanel = new JPanel();
        rightPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4));
        rightPanel.setVisible(true);
        userInputPanels();
        return rightPanel;
    }

    private void userInputPanels() {//sets up the panels for the labels
        itemIDInputPanel = new JPanel();
        woodTypeInputPanel = new JPanel();
        armrestInputPanel = new JPanel();
        quantityInputPanel = new JPanel();

        itemIDInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        woodTypeInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        armrestInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        quantityInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));

        itemIDInputPanel.setVisible(true);
        woodTypeInputPanel.setVisible(true);
        armrestInputPanel.setVisible(true);
        quantityInputPanel.setVisible(true);

        userInputs();

        rightPanel.add(itemIDInputPanel);
        rightPanel.add(woodTypeInputPanel);
        rightPanel.add(armrestInputPanel);
        rightPanel.add(quantityInputPanel);
    }

    private void userInputs() {   // sets up and adds to the right panel the user input objects
        itemIDTextField = new JTextField("");
        walnut = new JRadioButton("Walnut");
        oak = new JRadioButton("Oak");
        yes = new JRadioButton("Yes");
        no = new JRadioButton("No");
        quantityTextField = new JTextField("");
        
        itemIDTextField.setPreferredSize(new Dimension(TEXT_BOX_WIDTH, TEXT_BOX_HEIGHT));
        itemIDInputPanel.add(itemIDTextField);

        ButtonGroup woodTypeGroup = new ButtonGroup();
        woodTypeGroup.add(oak);
        woodTypeGroup.add(walnut);
        woodTypeInputPanel.add(walnut);
        woodTypeInputPanel.add(oak);

        ButtonGroup armrestGroup = new ButtonGroup();
        armrestGroup.add(yes);
        armrestGroup.add(no);
        armrestInputPanel.add(yes);
        armrestInputPanel.add(no);

        quantityTextField.setPreferredSize(new Dimension(TEXT_BOX_WIDTH, TEXT_BOX_HEIGHT));
        quantityInputPanel.add(quantityTextField);
    }

    private JPanel lowerPanel() {   // sets up the lower panel for confirm and cancel buttons
        lowerPanel = new JPanel(new BorderLayout());
        lowerPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 4));
        lowerPanel.setVisible(true);
        confirmCancelPanel();
        errorPanel();
        confirmCancelButtons();
        return lowerPanel;
    }
    
    private void confirmCancelPanel(){
        confirmCancelPanel = new JPanel();
        confirmCancelPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 8));
        confirmCancelPanel.setBackground(Color.blue);
        confirmCancelPanel.setVisible(true);
        lowerPanel.add(confirmCancelPanel, BorderLayout.NORTH);
    }
    
    private void errorPanel(){
        errorPanel = new JPanel();
        errorPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 8));
        errorPanel.setBackground(Color.yellow);
        errorPanel.setVisible(true);
        lowerPanel.add(errorPanel, BorderLayout.SOUTH);
    }
    
    private void confirmCancelButtons(){
        confirmButton = new JButton("Confirm");
        cancelButton = new JButton("Cancel");
        
        confirmButton.setVisible(true);
        cancelButton.setVisible(true);
        
        confirmCancelPanel.add(confirmButton);
        confirmCancelPanel.add(cancelButton);
        
        cancelButtonLogic();
        confirmButtonLogic();
    }
    
    private void cancelButtonLogic(){
        cancelButton.addActionListener((ActionEvent ae) -> {
            this.dispose();
        });
    }
    
    private void confirmButtonLogic(){
        confirmButton.addActionListener((ActionEvent ae) -> {
            errorMessage();
            
        });
    }
    
    private void errorMessage(){
        JLabel errorLabel = new JLabel("Error");
        errorLabel.setOpaque(true);
        errorLabel.setVisible(true);
        errorLabel.setForeground(Color.BLACK);
        errorPanel.add(errorLabel);
    }
}

I added a static main method so I could run the GUI.我添加了一个 static 主要方法,这样我就可以运行 GUI。 I modified your errorPanel and errorMessage methods.我修改了你的errorPanelerrorMessage方法。

Here's the GUI on startup.这是启动时的 GUI。

椅子 1

Here's the GUI after left-clicking the Confirm JButton .这是左键单击 Confirm JButton后的 GUI。

在此处输入图像描述

I didn't go through your code in detail, but it seems like you posted a lot of code for a form JPanel , button JPanel , and error JPanel .我没有详细介绍您的代码 go ,但似乎您为表单JPanel 、按钮JPanel和错误JPanel发布了很多代码。

Here's the complete runnable code.这是完整的可运行代码。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


/**
 *
 * @author User
 */
public class AddChair extends JFrame {
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new AddChair();
            }
        });
    }

    private final int FRAME_HEIGHT = 600;
    private final int FRAME_WIDTH = 400;
    private final String ADD_CHAIR_TITLE = "Chair";
    private final int TEXT_BOX_WIDTH = FRAME_WIDTH / 4;
    private final int TEXT_BOX_HEIGHT = 25;
    
    private JLabel errorLabel;

    private JPanel basePanel;
    private JPanel leftPanel;
    private JPanel rightPanel;
    private JPanel lowerPanel;
    private JPanel itemIDPanel;
    private JPanel woodTypePanel;
    private JPanel armrestPanel;
    private JPanel quantityPanel;
    private JPanel itemIDInputPanel;
    private JPanel woodTypeInputPanel;
    private JPanel quantityInputPanel;
    private JPanel armrestInputPanel;
    private JPanel confirmCancelPanel;
    private JPanel errorPanel;

    private JTextField itemIDTextField;
    private JTextField quantityTextField;

    private JRadioButton walnut;
    private JRadioButton oak;
    private JRadioButton yes;
    private JRadioButton no;
    
    private JButton confirmButton;
    private JButton cancelButton;

    public AddChair() {
        frameSetup();
        add(basePanel());
    }

    private void frameSetup() {   // sets up base frame
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setVisible(true);
        setAlwaysOnTop(true);
        setTitle(ADD_CHAIR_TITLE);
        setLocationRelativeTo(null);
    }

    private JPanel basePanel() {   //sets up a base panel to arrange other panels on
        basePanel = new JPanel(new BorderLayout());
        basePanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
        basePanel.add(leftPanel(), BorderLayout.WEST);
        basePanel.add(rightPanel(), BorderLayout.EAST);
        basePanel.add(lowerPanel(), BorderLayout.SOUTH);
        return basePanel;
    }

    private JPanel leftPanel() {   //sets up the left panel which will contain labels
        leftPanel = new JPanel();
        leftPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4));
        leftPanel.setVisible(true);
        labelPanels();
        labels();
        return leftPanel;
    }

    private void labelPanels() {//sets up the panels for the labels
        itemIDPanel = new JPanel();
        woodTypePanel = new JPanel();
        armrestPanel = new JPanel();
        quantityPanel = new JPanel();

        itemIDPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        woodTypePanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        armrestPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        quantityPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));

        itemIDPanel.setVisible(true);
        woodTypePanel.setVisible(true);
        armrestPanel.setVisible(true);
        quantityPanel.setVisible(true);

        leftPanel.add(itemIDPanel);
        leftPanel.add(woodTypePanel);
        leftPanel.add(armrestPanel);
        leftPanel.add(quantityPanel);
    }

    private void labels() {   //sets up the and adds the labels for the user 
        JLabel itemIDLabel = new JLabel("Item ID:");
        JLabel woodTypeLabel = new JLabel("Wood Type:");
        JLabel armrestLabel = new JLabel("Armrest:");
        JLabel quantityLabel = new JLabel("Quantity:");

        itemIDLabel.setVisible(true);
        woodTypeLabel.setVisible(true);
        armrestLabel.setVisible(true);
        quantityLabel.setVisible(true);

        itemIDPanel.add(itemIDLabel);
        woodTypePanel.add(woodTypeLabel);
        armrestPanel.add(armrestLabel);
        quantityPanel.add(quantityLabel);
    }

    private JPanel rightPanel() {   // sets up the right panel which will contain the user input objects
        rightPanel = new JPanel();
        rightPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4));
        rightPanel.setVisible(true);
        userInputPanels();
        return rightPanel;
    }

    private void userInputPanels() {//sets up the panels for the labels
        itemIDInputPanel = new JPanel();
        woodTypeInputPanel = new JPanel();
        armrestInputPanel = new JPanel();
        quantityInputPanel = new JPanel();

        itemIDInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        woodTypeInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        armrestInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        quantityInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));

        itemIDInputPanel.setVisible(true);
        woodTypeInputPanel.setVisible(true);
        armrestInputPanel.setVisible(true);
        quantityInputPanel.setVisible(true);

        userInputs();

        rightPanel.add(itemIDInputPanel);
        rightPanel.add(woodTypeInputPanel);
        rightPanel.add(armrestInputPanel);
        rightPanel.add(quantityInputPanel);
    }

    private void userInputs() {   // sets up and adds to the right panel the user input objects
        itemIDTextField = new JTextField("");
        walnut = new JRadioButton("Walnut");
        oak = new JRadioButton("Oak");
        yes = new JRadioButton("Yes");
        no = new JRadioButton("No");
        quantityTextField = new JTextField("");
        
        itemIDTextField.setPreferredSize(new Dimension(TEXT_BOX_WIDTH, TEXT_BOX_HEIGHT));
        itemIDInputPanel.add(itemIDTextField);

        ButtonGroup woodTypeGroup = new ButtonGroup();
        woodTypeGroup.add(oak);
        woodTypeGroup.add(walnut);
        woodTypeInputPanel.add(walnut);
        woodTypeInputPanel.add(oak);

        ButtonGroup armrestGroup = new ButtonGroup();
        armrestGroup.add(yes);
        armrestGroup.add(no);
        armrestInputPanel.add(yes);
        armrestInputPanel.add(no);

        quantityTextField.setPreferredSize(new Dimension(TEXT_BOX_WIDTH, TEXT_BOX_HEIGHT));
        quantityInputPanel.add(quantityTextField);
    }

    private JPanel lowerPanel() {   // sets up the lower panel for confirm and cancel buttons
        lowerPanel = new JPanel(new BorderLayout());
        lowerPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 4));
        lowerPanel.setVisible(true);
        confirmCancelPanel();
        errorPanel();
        confirmCancelButtons();
        return lowerPanel;
    }
    
    private void confirmCancelPanel(){
        confirmCancelPanel = new JPanel();
        confirmCancelPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 8));
        confirmCancelPanel.setBackground(Color.blue);
        confirmCancelPanel.setVisible(true);
        lowerPanel.add(confirmCancelPanel, BorderLayout.NORTH);
    }
    
    private void errorPanel(){
        errorPanel = new JPanel();
        errorPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 8));
        errorPanel.setBackground(Color.yellow);
        errorPanel.setVisible(true);
        
        errorLabel = new JLabel(" ");
        errorLabel.setOpaque(true);
        errorLabel.setVisible(true);
        errorLabel.setForeground(Color.BLACK);
        errorPanel.add(errorLabel);
        
        lowerPanel.add(errorPanel, BorderLayout.SOUTH);
    }
    
    private void confirmCancelButtons(){
        confirmButton = new JButton("Confirm");
        cancelButton = new JButton("Cancel");
        
        confirmButton.setVisible(true);
        cancelButton.setVisible(true);
        
        confirmCancelPanel.add(confirmButton);
        confirmCancelPanel.add(cancelButton);
        
        cancelButtonLogic();
        confirmButtonLogic();
    }
    
    private void cancelButtonLogic(){
        cancelButton.addActionListener((ActionEvent ae) -> {
            this.dispose();
        });
    }
    
    private void confirmButtonLogic(){
        confirmButton.addActionListener((ActionEvent ae) -> {
            errorMessage();
            
        });
    }
    
    private void errorMessage(){
        errorLabel.setText("Error message");
    }
    
}

I've tried setVisible,我试过setVisible,

Swing components are visible by default. Swing 组件默认可见。 There is no need to invoke setVisible(true) on the components无需在组件上调用 setVisible(true)

my errorLabel wont show up.我的 errorLabel 不会出现。

When you add a component to a visible GUI the basic code is:当您将组件添加到可见 GUI 时,基本代码是:

errorPanel.add(errorLabel);
errorPanel.revalidate();
errorPanel.repaint();

This will make sure the layout manager is invoked so the label is given a size/location.这将确保调用布局管理器,以便为 label 指定大小/位置。

However, another solution is to create and add the "errorLabel" to the "errorPanel" when the GUI is created.但是,另一种解决方案是在创建 GUI 时创建“errorLabel”并将其添加到“errorPanel”。 You would set the set to " ".您将设置设置为“”。 so the label takes up some space.所以 label 占用了一些空间。 Then you can update the label with various error messages simply by invoking error.Label.setText(...) .然后,您只需调用error.Label.setText(...)即可使用各种错误消息更新 label。 Of course this would mean the label would need to be defined as an instance variable.当然,这意味着需要将 label 定义为实例变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM