在eclipse中编辑关于JOptionPane.showMessageDialog的问题,求大虾帮助

时间:2022-11-01 12:52:53
在下根据视频教程内容编辑一个简单的计算器代码如下
package ly007.ly001;

import java.awt.Dimension;
import java.awt.GridLayout;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.*;

public class calculateFrame extends JFrame {
      private JTextField dataText1;
      private JTextField dataText2;
      private JTextField result;
      private JLabel label1;
      private JLabel label2;
      private JLabel label3;
      private JButton button;
      private JRadioButton radio1;
      private JRadioButton radio2;
      private JRadioButton radio3;
      private JRadioButton radio4;
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
        calculateFrame frame =new calculateFrame();
}

public calculateFrame(){
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int width =d.width;
int height =d.height;
//设置窗体的标题
setTitle("简单计算器");
//设置窗体的初始位置
setLocation((width-200)/2,(height-200)/2);
//设置窗体大小
this.setSize(200,200);
    //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//添加一个window事件,使用到了JAVA中的内部类概念
//内部类在事件处理中是用得非常多的

this.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
System.exit(0);
}

/*public void windowClosed(WindowEvent e) {
System.exit(0);
}*/
});

    //设置窗体大小不可变
    //this.setResizable(false);
    dataText1 =new JTextField();
    dataText2 =new JTextField();
    result = new JTextField();
    label1 = new JLabel("第一个操作数:");
    label2 =new JLabel("第二个操作数");
    label3 =new JLabel("计算结果");
    button =new JButton("计算");
   radio1 = new JRadioButton("+"); 
    radio2 = new JRadioButton("-"); 
   radio3 = new JRadioButton("*"); 
    radio4 = new JRadioButton("/"); 
    //让每一行的控件在一个组中
    JPanel p1=new JPanel(new GridLayout(1, 2));
    p1.add(label1);
    p1.add(dataText1);
    JPanel p2 = new JPanel(new GridLayout(1, 4));
    p2.add(radio1);
    p2.add(radio2);
    p2.add(radio3);
    p2.add(radio4);
    JPanel p3 = new JPanel(new GridLayout(1, 2));
    p3.add(label2);
    p3.add(dataText2);
    JPanel p4 =new JPanel(new GridLayout(1, 2));
    p4.add(label3);
    p4.add(result);
    JPanel p5 =new JPanel();
    p5.add(button);
    
    
    //处理一组按钮,只能选中一个的问题
    ButtonGroup group = new ButtonGroup();
    group.add(radio1);
    group.add(radio2);
    group.add(radio3);
    group.add(radio4);
    
    //布局Layout
    this.setLayout(new GridLayout(5,1));
    //将控件加入到窗体中
    
    this.add(p1);
    this.add(p2);
    this.add(p3);
    this.add(p4);
    this.add(p5);
    
    
    
    
//显示窗体
setVisible(true);
//给Button设置监听事件
button.addActionListener(new ButtonListener());

}
class ButtonListener implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
//保存第一个操作符
String data1 = dataText1.getText();
//保存第二个操作符
String data2 =dataText2.getText();
//保存操作符
String opertion ="";
if (radio1.isSelected()){
opertion = radio1.getText();

}else if(radio2.isSelected()){
opertion =radio2.getText();

}else if(radio3.isSelected()){
opertion =radio3.getText();

}else if(radio4.isSelected()){
opertion = radio4.getText();

}

//得到它的数据之后,我们应该判断数据的合法性

//进行计算
double d = calculate(data1,data2,opertion);
    //把结果显示在result中
result.setText(String.valueOf(d));
}



       public double calculate(String data1, String data2, String opertion){
        double result=Double.MAX_VALUE;
        double d1=0,d2=0;
        //第一步,怎么把字符串转成数字
        
        try {
         d1 =Double.parseDouble(data1);
         d2 =Double.parseDouble(data2);
        }catch (Exception e) {
// TODO: handle exception
       JOptionPane.showMessageDialog(this, "输入的数据有错误");
        
}
        
      
        
        //第二步,根据操作符进行计算
        if (opertion == "+"){
        result = d1+d2;
        
        }else if (opertion =="-"){
        result = d1-d2;
        
        }else if (opertion =="*"){
       result=d1 * d2;
        
        }else if(opertion =="/"){
      result = d1 / d2 ;
        
        }
        
        return result;
       }
}
}
如上红色代码,它提示The method showMessageDialog(Component, Object) in the type JOptionPane is not applicable for the arguments (calculateFrame.ButtonListener, String) 
运行之后出现这个Description Resource Path Location Type
The method showMessageDialog(Component, Object) in the type JOptionPane is not applicable for the arguments (calculateFrame.ButtonListener, String) calculateFrame.java /简单计算器/src/ly007/ly001 line 162 Java Problem

不知道哪里出错了和怎么解决,希望大家能帮我一把,在下感激不尽。ps本人刚刚菜鸟入门

4 个解决方案

#1


可以这样改:
JOptionPane.showMessageDialog( null, "输入的数据有错误");

#2


引用 1 楼  的回复:
可以这样改:
JOptionPane.showMessageDialog(null, "输入的数据有错误");


  谢谢解答,后面我发觉还可以这么改JOptionPane.showMessageDialog(calculateFrame.this, "输入的数据有错误");   就是不知道为毛null可以通过编译呢,求解疑!

#3


这里null顶替的是当前的Frame,作为第一个参数使用,类型是匹配的。
如果你用this,则作为第一个参数送进去的是当前Listener对象,类型是不匹配的。

#4


引用 3 楼  的回复:
这里null顶替的是当前的Frame,作为第一个参数使用,类型是匹配的。
如果你用this,则作为第一个参数送进去的是当前Listener对象,类型是不匹配的。


谢谢

#1


可以这样改:
JOptionPane.showMessageDialog( null, "输入的数据有错误");

#2


引用 1 楼  的回复:
可以这样改:
JOptionPane.showMessageDialog(null, "输入的数据有错误");


  谢谢解答,后面我发觉还可以这么改JOptionPane.showMessageDialog(calculateFrame.this, "输入的数据有错误");   就是不知道为毛null可以通过编译呢,求解疑!

#3


这里null顶替的是当前的Frame,作为第一个参数使用,类型是匹配的。
如果你用this,则作为第一个参数送进去的是当前Listener对象,类型是不匹配的。

#4


引用 3 楼  的回复:
这里null顶替的是当前的Frame,作为第一个参数使用,类型是匹配的。
如果你用this,则作为第一个参数送进去的是当前Listener对象,类型是不匹配的。


谢谢