我是一个java初学者,我刚刚制作了我的第一个计算器,但它没有编译。在frame.setPreferredSize(new Dimension(200, 250));
和frame.setDefaultCloserOperation(JFrame.EXIT_ON_CLOSE):
编译期间,它显示了一些问题
这些错误显示为<identifier> expected
和illegal start of expression
。有什么问题?
import java.sql.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
public class Cal extends JFrame implements ActionListener
{
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(200, 250));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public Cal(){
Button btn_1 = new Button("1");
btn_1.setSize(5, 5);
Button btn_2 = new Button("2");
btn_2.setSize(5, 5);
Button btn_3 = new Button("3");
btn_3.setSize(5, 5);
Button btn_4 = new Button("4");
btn_4.setSize(5, 5);
Button btn_5 = new Button("5");
btn_5.setSize(5, 5);
Button btn_6 = new Button("6");
btn_6.setSize(5, 5);
Button btn_7 = new Button("7");
btn_7.setSize(5, 5);
Button btn_8 = new Button("8");
btn_8.setSize(5, 5);
Button btn_9 = new Button("9");
btn_9.setSize(5, 5);
Button btn_0 = new Button("0");
btn_0.setSize(5, 5);
Button btn_dot = new Button(".");
btn_dot.setSize(5, 5);
Button btn_div = new Button("/");
btn_div.setSize(5, 5);
Button btn_mult = new Button("*");
btn_mult.setSize(5, 5);
Button btn_sub = new Button("-");
btn_sub.setSize(5, 5);
Button btn_addd = new Button("+");
btn_addd.setSize(5, 5);
Button btn_equ = new Button("=");
btn_equ.setSize(5, 5);
JTextField jt = new JTextField();
jt.setHorizontalAlignment(JTextField.RIGHT);
jt.setEditable(false);
double fnum, snum, total;
String op = null;
JPanel players = new JPanel();
players.setLayout(new GridLayout(1, 1));
players.add(jt, BorderLayout.NORTH);
players.setPreferredSize(new Dimension(10, 50));
JPanel players1 = new JPanel(new GridLayout(4, 3)); // adding buttons
players1.add(btn_7);
players1.add(btn_8);
players1.add(btn_9);
players1.add(btn_div);
players1.add(btn_4);
players1.add(btn_5);
players1.add(btn_6);
players1.add(btn_mult);
players1.add(btn_1);
players1.add(btn_2);
players1.add(btn_3);
players1.add(btn_sub);
players1.add(btn_0);
players1.add(btn_dot);
players1.add(btn_equ);
players1.add(btn_addd);
players1.setPreferredSize(new Dimension(150, 150));
//应用actionlistener
btn_1.addActionListener(this);
btn_2.addActionListener(this);
btn_3.addActionListener(this);
btn_4.addActionListener(this);
btn_5.addActionListener(this);
btn_6.addActionListener(this);
btn_7.addActionListener(this);
btn_8.addActionListener(this);
btn_9.addActionListener(this);
btn_0.addActionListener(this);
btn_dot.addActionListener(this);
btn_addd.addActionListener(this);
btn_mult.addActionListener(this);
btn_div.addActionListener(this);
btn_sub.addActionListener(this);
btn_equ.addActionListener(this);
//设置内容可用
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
frame.setContentPane(content);
content.add(players, BorderLayout.NORTH);
content.add(players1, BorderLayout.SOUTH);
frame.setTitle("Calculator");
frame.pack();
frame.setVisible(true);
}
//应用要执行的操作
public void actionPerformed(ActionListener e){
String input = jt.getText();
if(e.getSource()==btn_1)
{jt.setText(input + "1");}
else if(e.getSource()==btn_2)
{jt.setText(input + "2");}
else if(e.getSource()==btn_3)
{jt.setText(input + "3");}
else if(e.getSource()==btn_4)
{jt.setText(input + "4");}
else if(e.getSource()==btn_5)
{jt.setText(input + "5");}
else if(e.getSource()==btn_6)
{jt.setText(input + "6");}
else if(e.getSource()==btn_7)
{jt.setText(input + "7");}
else if(e.getSource()==btn_8)
{jt.setText(input + "8");}
else if(e.getSource()==btn_9)
{jt.setText(input + "9");}
else if(e.getSource()==btn_0)
{jt.setText(input + "0");}
else if(e.getSource()==btn_dot)
{jt.setText(input + ".");}
else if(e.getSource()==btn_addd)
{
fnum = Double.parseDouble(jt.getText());
op = "+";
jt.setText(" ");
}
else if(e.getSource()==btn_sub)
{
fnum = Double.parseDouble(jt.getText());
op = "-";
jt.setText(" ");
}
else if(e.getSource()==btn_div)
{
fnum = Double.parseDouble(jt.getText());
op = "/";
jt.setText(" ");
}
else if(e.getSource()==btn_equ)
{
snum = Double.parseDouble(jt.getText());
if(op.equ("+"))
{
total = fnum + snum;
}
if(op.equ("-"))
{
total = fnum - snum;
}
if(op.equ("*"))
{
total = fnum * snum;
}
if(op.equ("/"))
{
total = fnum / snum;
}
jt.setText(" " + total);
}
}
public static void main(String args[]){
Cal obj = new Cal(); }
}
答案 0 :(得分:4)
我认为你需要把
frame.setPreferredSize(new Dimension(200, 250));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
在Cal
()构造函数
和大约Button
以及其他变量,在构造函数之外定义它,就像那样:
class Cal extends JFrame implements ActionListener {
JFrame frame = new JFrame();
JTextField jt = new JTextField();
Button btn_1 = new Button("1");
Button btn_2 = new Button("2");
.....
JPanel players1 = new JPanel(new GridLayout(4, 3)); // adding buttons
....
和e是ActionEvent
而不是ActionListener
像这样:
public void actionPerformed(ActionEvent e) {...}
并且equ
类中没有String
方法应该是if (op.equals("+")) {}
答案 1 :(得分:2)
您正尝试在可执行上下文的上下文之外进行修改或执行语句。
您只能进行声明并将默认值分配给方法和构造函数的变量
移动
frame.setPreferredSize(new Dimension(200, 250));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
在类构造函数
的上下文中public Cal(){
frame.setPreferredSize(new Dimension(200, 250));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
<强>更新强>
您有很多参考问题。您在构造函数中定义了对象,然后您想要访问代码中的其他位置。这些变量没有上下文构造函数的上下文。相反,您应该在类级别声明它们,以便它们对整个类都可见。
public class Cal extends JFrame implements ActionListener
{
private JFrame frame = new JFrame();
private Button btn_1 = new Button("1");
private Button btn_2 = new Button("2");
private Button btn_3 = new Button("3");
private Button btn_4 = new Button("4");
private Button btn_5 = new Button("5");
private Button btn_6 = new Button("6");
private Button btn_7 = new Button("7");
private Button btn_8 = new Button("8");
private Button btn_9 = new Button("9");
private Button btn_0 = new Button("0");
private Button btn_dot = new Button(".");
private Button btn_div = new Button("/");
private Button btn_mult = new Button("*");
private Button btn_sub = new Button("-");
private Button btn_addd = new Button("+");
private Button btn_equ = new Button("=");
private JTextField jt = new JTextField();
public Cal(){
frame.setPreferredSize(new Dimension(200, 250));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// This is a bad idea, the layout manager will make these calls redudent...
btn_1.setSize(5, 5);
btn_2.setSize(5, 5);
btn_3.setSize(5, 5);
btn_4.setSize(5, 5);
btn_5.setSize(5, 5);
btn_6.setSize(5, 5);
btn_7.setSize(5, 5);
btn_8.setSize(5, 5);
btn_9.setSize(5, 5);
btn_0.setSize(5, 5);
btn_dot.setSize(5, 5);
btn_div.setSize(5, 5);
btn_mult.setSize(5, 5);
btn_sub.setSize(5, 5);
btn_addd.setSize(5, 5);
btn_equ.setSize(5, 5);
jt.setHorizontalAlignment(JTextField.RIGHT);
jt.setEditable(false);
double fnum, snum, total;
String op = null;
JPanel players = new JPanel();
players.setLayout(new GridLayout(1, 1));
players.add(jt, BorderLayout.NORTH);
players.setPreferredSize(new Dimension(10, 50));
JPanel players1 = new JPanel(new GridLayout(4, 3)); // adding buttons
players1.add(btn_7);
players1.add(btn_8);
players1.add(btn_9);
players1.add(btn_div);
players1.add(btn_4);
players1.add(btn_5);
players1.add(btn_6);
players1.add(btn_mult);
players1.add(btn_1);
players1.add(btn_2);
players1.add(btn_3);
players1.add(btn_sub);
players1.add(btn_0);
players1.add(btn_dot);
players1.add(btn_equ);
players1.add(btn_addd);
players1.setPreferredSize(new Dimension(150, 150));
你也在混合重量轻和重量轻的组件,这绝不是一个好主意。使用JButton
代替Button
答案 2 :(得分:-1)
我认为frame.setPreferredSize(new Dimension(200,250));和frame.setDefaultCloserOperation(JFrame.EXIT_ON_CLOSE):需要在方法内部。命令总是需要在方法内部。