Java 调用isVisible()后JFrame未显示,无错误
我正在为我的CS类创建一个简单的计算器,我已经完成了所有的数学运算,我正在尝试格式化我的JFrame,我制作了一个背景面板,添加了一个BoxLayout,然后添加了我的3个其他面板,然后将背景面板添加到JFrame,但是在进行更改后,JFrame永远不会打开。有人能帮我找出我做错了什么吗Java 调用isVisible()后JFrame未显示,无错误,java,swing,jframe,jpanel,boxlayout,Java,Swing,Jframe,Jpanel,Boxlayout,我正在为我的CS类创建一个简单的计算器,我已经完成了所有的数学运算,我正在尝试格式化我的JFrame,我制作了一个背景面板,添加了一个BoxLayout,然后添加了我的3个其他面板,然后将背景面板添加到JFrame,但是在进行更改后,JFrame永远不会打开。有人能帮我找出我做错了什么吗 public class Calculator implements ActionListener { private JFrame myFrame; private JPanel butto
public class Calculator implements ActionListener
{
private JFrame myFrame;
private JPanel buttonPanel;
private JPanel resultPanel;
private JPanel textPanel;
public JPanel mainPanel;
private JButton ADD;
private JButton SUB;
private JButton MULT;
private JButton DIV;
private JLabel resultLabel;
private JTextField text1;
private JTextField text2;
public Calculator()
{
// Frame to contain panels
int fx = 550;
int fy = 300;
myFrame = new JFrame("The Shittiest Calculator");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(fx, fy);
myFrame.setResizable(false);
// Buttons to add to the buttonPanel
ADD = new JButton("ADD");
SUB = new JButton("SUBTRACT");
MULT = new JButton("MULTIPLY");
DIV = new JButton("DIVIDE");
ADD.setName("addButton");
SUB.setName("subButton");
MULT.setName("multButton");
DIV.setName("divButton");
int x = 50;
int y = 25;
ADD.setSize(x, y);
SUB.setSize(x, y);
MULT.setSize(x, y);
DIV.setSize(x, y);
ADD.addActionListener(this);
SUB.addActionListener(this);
MULT.addActionListener(this);
DIV.addActionListener(this);
// Button Panel
buttonPanel = new JPanel();
buttonPanel.add(ADD);
buttonPanel.add(SUB);
buttonPanel.add(MULT);
buttonPanel.add(DIV);
// Result label
resultLabel = new JLabel("Result = ");
resultLabel.isVisible();
// Result Panel
resultPanel = new JPanel();
resultPanel.isVisible();
resultPanel.add(resultLabel);
// Textfields
text1 = new JTextField();
text2 = new JTextField();
text1.setName("leftOperand");
text2.setName("rightOperand");
text1.setColumns(10);
text2.setColumns(10);
// Textfield panel
textPanel = new JPanel();
textPanel.add(text1);
textPanel.add(text2);
//background Panel;
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.add(textPanel);
mainPanel.add(resultPanel);
mainPanel.add(buttonPanel);
//Frame start
myFrame.add(mainPanel);
myFrame.isVisible();
}
/**
*
* @return this.myFrame returns the frame.
*/
public JFrame getFrame()
{
return this.myFrame;
}
/**
*
* @param args main method
*/
public static void main(String[] args)
{
Calculator calc = new Calculator();
}
将.isVisible()替换为.setVisible(true)
.isVisible()将返回一个布尔值,说明此时它是否可见,.setVisible()将实际将其设置为可见将.isVisible()替换为.setVisible(true)以及
setVisible
(如回答中所述),您还需要调用pack
。请注意,标准教程中已经介绍了这一切:您需要纠正一个错误观念:仅仅因为您没有编译错误,并不意味着您的程序“没有错误”。程序运行不正常,因此根据定义,它有错误,逻辑错误,这些是最难修复的。