首页 > 解决方案 > 无论我尝试什么,我的框架都是空的

问题描述

我再也无法在我的框架中显示任何内容,有人可以告诉我为什么吗?

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator
{
    private final static int DIMENSION = 400;
    JFrame calcFrame;
    JPanel buttonPanel;
    JButton addButton;
    JButton subButton;
    JButton multButton;
    JButton divButton;
    JPanel resultPanel;
    JLabel resultLable;
    JLabel resultLabel;
    JTextField leftOperand;
    JTextField rightOperand;
    JLabel jlResult;
    JPanel textPanel;

    public Calculator()
    {
        calcFrame = new JFrame();
        calcFrame.setLocation(100, 100);
        calcFrame.setSize(DIMENSION, DIMENSION);
        calcFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        calcFrame.setTitle("Simple Calculator");

        initializeComponents();

        calcFrame.pack();
        calcFrame.setVisible(true);
    }
    public void initializeComponents()
    {
        addButton = new JButton("ADD");
        addButton.setName("addButton");

        subButton = new JButton("SUB");
        subButton.setName("subButton");

        multButton = new JButton("MULT");
        multButton.setName("multButton");

        divButton = new JButton("DIV");
        divButton.setName("divButton");

        resultLabel = new JLabel(" ");
        resultLabel.setName("resultLabel");

        jlResult = new JLabel("Result = ");
        jlResult.setName("jlResult");

        leftOperand = new JTextField(10);
        leftOperand.setName("leftOperand");

        rightOperand = new JTextField(10);
        rightOperand.setName("rightOperand");     


        Panel();
        addActionListeners();
    }
    public void Panel()
    {
        buttonPanel = new JPanel();
        resultPanel = new JPanel();
        textPanel = new JPanel();

        buttonPanel.add(addButton);
        buttonPanel.add(subButton);
        buttonPanel.add(multButton);
        buttonPanel.add(divButton);
        resultPanel.add(jlResult);
        resultPanel.add(resultLable);
        textPanel.add(leftOperand);
        textPanel.add(rightOperand);

        calcFrame.add(buttonPanel, BorderLayout.PAGE_END);
        calcFrame.add(resultPanel, BorderLayout.WEST);
        calcFrame.add(textPanel, BorderLayout.PAGE_START);

    }

    public void addActionListeners()
    {
        multButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) 
            {
                multClicked();
            }
        });
        divButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) 
            {
                divClicked();
            }
        });
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) 
            {
                addClicked();
            }
        });
        subButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) 
            {
                subClicked();
            }
        });
    }

    public JFrame getFrame()
    {
        return calcFrame;
    }

    public static void main(String[] args)
    {
        Calculator calc = new Calculator();
    }
}

标签: javaswinguser-interfacecalculator

解决方案


您的代码中的这个问题就是这一行。

resultPanel.add(resultLable);

在您的 initializeComponents() 方法中。你没有初始化resultLable。相反,您已经初始化

 resultLabel = new JLabel(" ");
 resultLabel.setName("resultLabel");

// You can also do this to initialize a label with a string
// resultLabel = new JLabel("resultLabel");

您可以更改resultPanel.add(resultLable);resultPanel.add(resultLabel);

resultLable或初始化initializeComponents()


推荐阅读