3

我是一个java初学者,我刚刚制作了我的第一个计算器,但它没有编译。它在编译过程中显示了一些问题,在frame.setPreferredSize(new Dimension(200, 250));frame.setDefaultCloserOperation(JFrame.EXIT_ON_CLOSE): 这些错误显示为<identifier> expectedillegal 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));

//应用动作监听器

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(); }
}
4

3 回答 3

4

我想你需要把

frame.setPreferredSize(new Dimension(200, 250));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Cal() 构造函数内部


以及 aboutButton等变量,在构造函数之外定义,像这样:

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 
    ....

而 eActionEvent不是ActionListener这样的:

public void actionPerformed(ActionEvent e) {...}

并且在课堂上应该 没有equ方法Stringif (op.equals("+")) {}

于 2013-04-27T19:18:42.520 回答
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

于 2013-04-27T19:18:22.050 回答
-1

我认为 frame.setPreferredSize(new Dimension(200, 250)); 和 frame.setDefaultCloserOperation(JFrame.EXIT_ON_CLOSE): 需要在方法内。命令总是需要在方法内部。

于 2015-09-23T02:45:52.760 回答