当前位置: 技术问答>java相关
java初学者问题,我编了一个计算器,怎么调都通不过,哪位大虾帮我调一下
来源: 互联网 发布时间:2015-10-25
本文导语: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Caculator extends JFrame{ //panel private Container contentPane; private JPanel leftPanel; private JPanel centerPanel; private JPanel rightPanel; //TextField private JTextField imp...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Caculator extends JFrame{
//panel
private Container contentPane;
private JPanel leftPanel;
private JPanel centerPanel;
private JPanel rightPanel;
//TextField
private JTextField imput1TextField;
private JTextField imput2TextField;
//label
private JLabel answerLabel;
//button
private JButton plusButton;
private JButton minusButton;
private JButton mutiplyButton;
private JButton divideButton;
public Caculator(){
super("我的计算器");
contentPane=this.getContentPane();
this.setSize(300,300);
//centering
Dimension frameSize=this.getSize();
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
//left
leftPanel=new JPanel();
leftPanel.setLayout(new GridLayout(3,1));
leftPanel.add(new JLabel("数字1"));
leftPanel.add(new JLabel("数字2"));
leftPanel.add(new JLabel("结果是"));
contentPane.add(leftPanel,BorderLayout.WEST);
//center
centerPanel=new JPanel();
centerPanel.setLayout(new GridLayout(3,1));
imput1TextField=new JTextField(10);
imput2TextField=new JTextField(10);
answerLabel=new JLabel();
centerPanel.add(imput1TextField);
centerPanel.add(imput2TextField);
centerPanel.add(answerLabel);
contentPane.add(centerPanel,BorderLayout.CENTER);
//right
rightPanel=new JPanel();
rightPanel.setLayout(new GridLayout(2,2));
plusButton=new JButton("+");
minusButton=new JButton("-");
mutiplyButton=new JButton("x");
divideButton=new JButton("/");
rightPanel.add(plusButton);
plusButton.addActionListener(new DoClick());
rightPanel.add(minusButton);
minusButton.addActionListener(new DoClick());
rightPanel.add(mutiplyButton);
mutiplyButton.addActionListener(new DoClick());
rightPanel.add(divideButton);
divideButton.addActionListener(new DoClick());
contentPane.add(rightPanel,BorderLayout.EAST);
}
//listen
class DoClick implements ActionListener{
public void actionPerformed(ActionEvent e){
Double d1=new Double(imput1TextField.getText()).doubleValue();
Double d2=new Double(imput2TextField.getText()).doubleValue();
if (e.getSource()==plusButton) answerLabel.setText(""+(d1+d2));
else if (e.getSource()==minusButton) answerLabel.setText(""+(d1-d2));
else if (e.getSource()==mutiplyButton) answerLabel.setText(""+(d1*d2));
else if (e.getSource()==divideButton) answerLabel.setText(""+(d1/d2));
}
}
public static void main(String args[]){
Caculator ca=new Caculator();
}
}
编译错误:
--------------------Configuration: j2sdk4.0 --------------------
J:javaCaculator.java:76: incompatible types
found : double
required: java.lang.Double
Double d1=new Double(imput1TextField.getText()).doubleValue();
^
J:javaCaculator.java:77: incompatible types
found : double
required: java.lang.Double
Double d2=new Double(imput2TextField.getText()).doubleValue();
^
J:javaCaculator.java:78: operator + cannot be applied to java.lang.Double,java.lang.Double
if (e.getSource()==plusButton) answerLabel.setText(""+(d1+d2));
^
J:javaCaculator.java:79: operator - cannot be applied to java.lang.Double,java.lang.Double
else if (e.getSource()==minusButton) answerLabel.setText(""+(d1-d2));
^
J:javaCaculator.java:80: operator * cannot be applied to java.lang.Double,java.lang.Double
else if (e.getSource()==mutiplyButton) answerLabel.setText(""+(d1*d2));
^
J:javaCaculator.java:81: operator / cannot be applied to java.lang.Double,java.lang.Double
else if (e.getSource()==divideButton) answerLabel.setText(""+(d1/d2));
^
6 errors
Process completed.
import java.awt.*;
import java.awt.event.*;
public class Caculator extends JFrame{
//panel
private Container contentPane;
private JPanel leftPanel;
private JPanel centerPanel;
private JPanel rightPanel;
//TextField
private JTextField imput1TextField;
private JTextField imput2TextField;
//label
private JLabel answerLabel;
//button
private JButton plusButton;
private JButton minusButton;
private JButton mutiplyButton;
private JButton divideButton;
public Caculator(){
super("我的计算器");
contentPane=this.getContentPane();
this.setSize(300,300);
//centering
Dimension frameSize=this.getSize();
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
//left
leftPanel=new JPanel();
leftPanel.setLayout(new GridLayout(3,1));
leftPanel.add(new JLabel("数字1"));
leftPanel.add(new JLabel("数字2"));
leftPanel.add(new JLabel("结果是"));
contentPane.add(leftPanel,BorderLayout.WEST);
//center
centerPanel=new JPanel();
centerPanel.setLayout(new GridLayout(3,1));
imput1TextField=new JTextField(10);
imput2TextField=new JTextField(10);
answerLabel=new JLabel();
centerPanel.add(imput1TextField);
centerPanel.add(imput2TextField);
centerPanel.add(answerLabel);
contentPane.add(centerPanel,BorderLayout.CENTER);
//right
rightPanel=new JPanel();
rightPanel.setLayout(new GridLayout(2,2));
plusButton=new JButton("+");
minusButton=new JButton("-");
mutiplyButton=new JButton("x");
divideButton=new JButton("/");
rightPanel.add(plusButton);
plusButton.addActionListener(new DoClick());
rightPanel.add(minusButton);
minusButton.addActionListener(new DoClick());
rightPanel.add(mutiplyButton);
mutiplyButton.addActionListener(new DoClick());
rightPanel.add(divideButton);
divideButton.addActionListener(new DoClick());
contentPane.add(rightPanel,BorderLayout.EAST);
}
//listen
class DoClick implements ActionListener{
public void actionPerformed(ActionEvent e){
Double d1=new Double(imput1TextField.getText()).doubleValue();
Double d2=new Double(imput2TextField.getText()).doubleValue();
if (e.getSource()==plusButton) answerLabel.setText(""+(d1+d2));
else if (e.getSource()==minusButton) answerLabel.setText(""+(d1-d2));
else if (e.getSource()==mutiplyButton) answerLabel.setText(""+(d1*d2));
else if (e.getSource()==divideButton) answerLabel.setText(""+(d1/d2));
}
}
public static void main(String args[]){
Caculator ca=new Caculator();
}
}
编译错误:
--------------------Configuration: j2sdk4.0 --------------------
J:javaCaculator.java:76: incompatible types
found : double
required: java.lang.Double
Double d1=new Double(imput1TextField.getText()).doubleValue();
^
J:javaCaculator.java:77: incompatible types
found : double
required: java.lang.Double
Double d2=new Double(imput2TextField.getText()).doubleValue();
^
J:javaCaculator.java:78: operator + cannot be applied to java.lang.Double,java.lang.Double
if (e.getSource()==plusButton) answerLabel.setText(""+(d1+d2));
^
J:javaCaculator.java:79: operator - cannot be applied to java.lang.Double,java.lang.Double
else if (e.getSource()==minusButton) answerLabel.setText(""+(d1-d2));
^
J:javaCaculator.java:80: operator * cannot be applied to java.lang.Double,java.lang.Double
else if (e.getSource()==mutiplyButton) answerLabel.setText(""+(d1*d2));
^
J:javaCaculator.java:81: operator / cannot be applied to java.lang.Double,java.lang.Double
else if (e.getSource()==divideButton) answerLabel.setText(""+(d1/d2));
^
6 errors
Process completed.
|
把Double d1=new Double(imput1TextField.getText()).doubleValue();中的Double d1 换成double d1试试看
Double(imput1TextField.getText()).doubleValue()返回的是一个double数值,不能与Double相匹配!
Double(imput1TextField.getText()).doubleValue()返回的是一个double数值,不能与Double相匹配!
|
在构造函数末尾加一句
this.setVisible(true);
actionPerformed中
Double改为double
this.setVisible(true);
actionPerformed中
Double改为double
|
Double和double不一样的
你看一下你的错误提示,都是Double和double数据类型不符引起的
你看一下你的错误提示,都是Double和double数据类型不符引起的
|
Double 是 double 的一个wrapper,他们不一样 :)
只有double int才能参与运算,而Integer Double 则不行
只有double int才能参与运算,而Integer Double 则不行