java - 无论我尝试什么,我的框架都是空的
问题描述
我再也无法在我的框架中显示任何内容,有人可以告诉我为什么吗?
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();
}
}
解决方案
您的代码中的这个问题就是这一行。
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()
推荐阅读
- performance - 高延迟客户端会影响服务器上其他请求的处理时间吗?
- leaflet - 如何从传单中的地图中删除所有特征组?
- angular - 如何配置我的 Angular 库以依赖 Angular Material?
- javascript - 通过js文件到laravel的ajax调用
- asp.net - 尝试从视图访问模型到控制器编辑方法
- arrays - 根据另一个属性值在对象数组中查找属性值
- c - Flex 字符串识别“无法识别的规则”错误
- google-sheets - 如何将工作表查询结果折叠到单个单元格中
- spring-boot - Jhipster - 在 JHI_User 和另一个实体之间创建关系错误
- javascript - JS:检测被监视的 div 的 dom 突变(或将事件侦听器更改为 innerHTML)