我正在为我的CS课创建一个简单的计算器,我已经完成了所有的数学,我正在尝试格式化我的JFrame,我做了一个背景面板,添加一个BoxLayout,然后添加了我的其他3个面板,然后将背景面板添加到JFrame,但是在进行更改后,JFrame永远不会打开。有人可以帮助我弄清楚我做错了什么吗?
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(( 实际上会将其设置为 visible
将 .isVisible(( 替换为 .setVisible(true(