我是一个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));
//应用动作监听器
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(); }
}