【问题标题】:JLabel wont show up on button clickJLabel 不会在按钮单击时显示
【发布时间】:2021-07-29 17:25:42
【问题描述】:

这是我的第一个 GUI,所以任何建议在这里都是很好的建议,但主要是我只是想弄清楚为什么我的 errorLabel 不会出现。我试过setVisiblesetOpaque,手动设置位置,最后(如下所示)只是让它自己的面板坐下来,它仍然不会显示在按钮按下。

有什么想法吗?

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

【问题讨论】:

    标签: java swing user-interface jlabel


    【解决方案1】:

    我添加了一个静态 main 方法,以便我可以运行 GUI。我修改了你的 errorPanelerrorMessage 方法。

    这是启动时的 GUI。

    这是左击确认JButton后的GUI。

    我没有详细查看您的代码,但您似乎为表单JPanel、按钮JPanel 和错误JPanel 发布了很多代码。

    这是完整的可运行代码。

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

    【讨论】:

    • 谢谢,这大概是我的答案。我在错误面板的创建中创建了它,并让它在点击时可见,但是同样的事情谢谢:)
    【解决方案2】:

    我试过 setVisible,

    Swing 组件默认是可见的。无需在组件上调用 setVisible(true)

    我的 errorLabel 不会出现。

    当您将组件添加到可见 GUI 时,基本代码是:

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

    这将确保调用布局管理器,以便为标签指定大小/位置。

    但是,另一种解决方案是在创建 GUI 时创建“errorLabel”并将其添加到“errorPanel”。您将设置设置为“”。所以标签占用了一些空间。然后,您只需调用 error.Label.setText(...) 即可使用各种错误消息更新标签。当然,这意味着标签需要定义为实例变量。

    【讨论】:

    • 感谢您的解释。正如你所说,我最终预先创建并添加了标签,效果很好,但让我摸不着头脑,为什么按钮不允许我在点击时创建标签
    • 让我摸不着头脑,为什么按钮不允许我在点击时创建标签 - 默认情况下,组件的大小为 0,因此没有可绘制的内容.通常调用 revalidate() 和 repaint() 将解决问题。如果没有,那么您需要调用 revalidate() 并在“errorPanel”的父面板上重新绘制。
    猜你喜欢
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    相关资源
    最近更新 更多