gpt4 book ai didi

java - 无论我尝试什么,我的框架都是空的

转载 作者:行者123 更新时间:2023-11-30 05:29:14 26 4
gpt4 key购买 nike

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

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

或在initializeComponents()中初始化resultLable

关于java - 无论我尝试什么,我的框架都是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57913664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com