这个问题在这里已经有答案了:
“找不到符号”或“无法解析符号”错误是什么意思(15个答案)
上个月关门了。
如果我编译这个,我会得到setdefaultcloseoperation、setsize和setvisible的“找不到符号”错误。我的问题是我不明白为什么。这是我家庭作业的一部分。但因为这个问题,我甚至不能开始下一部分
//AdderSubtracterFrame.java
//This class displays a Frame which can add or subtract two numbers
import javax.swing.JFrame;
public class AdderSubtracterProgram
{
public static void main(String [] args)
{
AdderSubtracterFrame frame1 = new AdderSubtracterFrame("Adder and Subtracter");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(400,100);
frame1.setVisible(true);
}
}
我添加addersubtracterframe代码两个类在同一个项目中。这个项目是做一个计算器
//AdderSubtracterFrame.java
//This class displays a Frame which can add or subtract two numbers
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.FlowLayout;
public class AdderSubtracterFrame
{
private JTextField num1TextField;
private JTextField num2TextField;
private JTextField resultTextField;
private JButton clearButton;
private JButton addButton;
private JButton subtractButton;
private JButton buttonPanel;
//Constructor
public AdderSubtracterFrame(String title)
{
//Set the title of the AdderSubtracterFrame by
//using the superclass JFrame constructor
super();
//Set the JFrame to be a 2*2 Grid
//Set a gap of 5pixels between each row and column.
setLayout(new GridLayout(2,2,5,5));
//Create and instance of the components appearing in the 2*2 grid
num1TextField = new JTextField("0",5);//Begin with 0
num2TextField = new JTextField("0",5);//Begin with 0
resultTextField = new JTextField((5));//Begin empty
buttonPanel = getButtonPanel();
//Add components to 2*2 Grid
add(num1TextField);
add(num2TextField);
add(resultTextField);
add(buttonPanel);
}
//Create and return a panel containing the buttons
private JPanel getButtonPanel()
{
JPanel myPanel = new JPanel();
myPanel.setLayout(new FlowLayout());
//Create an instance of each button
addButton = new JButton("+");
subtractButton = new JButton("-");
clearButton = new JButton("CLEAR");
//Add the 3button to myPanel
myPanel.add(addButton);
myPanel.add(subtractButton);
myPanel.add(clearButton);
return myPanel;
}
}
2条答案
按热度按时间drkbr07n1#
主要问题是
AdderSubtracterFrame
不延伸JFrame
. 因此,您不能使用JFrame
功能如setVisible()
.申报您的
AdderSubtracterFrame
像这样来解决:我还发现了其他一些东西:
你打电话来
super()
在你的构造器中,但是你没有传递标题。更改为super(title)
.不要在JavaSwing gui中使用固定大小。使用布局管理器(就像你做的那样)并调用
pack()
在框架上,根据框架内的组件布局框架。JavaSwingGUI应该在事件调度线程(edt)上运行。请参见swing中的并发。
vmpqdwk32#
制造
AdderSubtracterFrame
延伸JFrame
,只需将这一行代码从到