【问题标题】:My frame is empty regardless of what I try无论我尝试什么,我的框架都是空的
【发布时间】:2023-04-02 02:51:01
【问题描述】:

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

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

【问题讨论】:

  • 在你的方法Panel()。将 resultPanel.add(resultLable); 更改为 resultPanel.add(resultLabel);resultLable 未初始化,因此它抛出了 nullpointerexception。
  • 您好,欢迎来到 StackOverflow。 1) 请使用tour 并阅读How to Ask。 2)不要复制粘贴相同的文本4-5次以消除文本代码比率错误来发布您的问题,而是描述错误是什么,在它不再显示任何内容之前您究竟做了什么,等等,您提供的信息越多,就越容易为您提供帮助。 3)正确缩进你的代码(我已经修复了它),这有助于我们更容易地检测错误(并且你可以防止它们)。
  • 4) pack() 覆盖了 setSize(...) 返回的大小,删除其中的 1 个。 5) 方法名称应以动词开头,第一个单词lowerCased 请查看Java naming conventions 了解更多信息。这一点连同 (3) 将帮助您提高代码质量并减少您犯的错误数量以及代码的可读性。
  • @Frakcool "4) pack() 正在覆盖 setSize(...) 返回的大小,删除其中的 1 个。" 删除第二个。应避免设置组件的大小,而应使用更大的字体、插图等使组件(实际上)更大。
  • @AndrewThompson 我发誓我已经写过“删除其中的 1 个(建议第二个),可能我被自己完成编译的代码分心并忘记了这一点,感谢您补充我的评论 :)跨度>

标签: java swing user-interface calculator


【解决方案1】:

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

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

或在initializeComponents()中初始化resultLable

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 2017-10-27
    • 2020-08-11
    • 1970-01-01
    • 2016-11-28
    • 2017-01-12
    相关资源
    最近更新 更多