这段java程序哪里错了? - 爱问答

(爱问答)

这段java程序哪里错了?

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

 

public class Fwl {

public static void main(String[] args) {

new MyFrame("计算器",1920/2,1080/2,320,532);

}

}

 

class MyFrame extends JFrame {

JTextField t;

 

MyFrame(String s,int x,int y,int w,int h) {

super(s);

setBounds(x,y,w,h);

 

JPanel p1 = new JPanel();

t = new JTextField(20);

t.setSize(20,20);

p1.add(t);

 

JPanel p2 = new JPanel();

p2.setSize(300,500);

p2.setLayout(new GridLayout(4,4));

String z = "1,2,3,+,4,5,6,-,7,8,9,*,0,.,/,=";

String[] f = z.split(",");

for(int i=0;i<f.length;i++) {

JButton b = new JButton(f[i]);

b.setSize(30,30);

b.addActionListener(new MyEvent(this));

b.setActionCommand(f[i]);

p2.add(b);

}

 

add(p1,BorderLayout.NORTH);

add(p2,BorderLayout.CENTER);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

}

 

class MyEvent implements ActionListener {

int x,y,s,j,c,u;

MyFrame a;

MyEvent(MyFrame a) {

this.a = (MyFrame)a;

}

public void actionPerformed(ActionEvent e) {

if(e.getActionCommand().equals("+")) {

x = Integer.parseInt(a.t.getText());

      a.t.setText(a.t.getText()+e.getActionCommand());

      s = 1;

}

 

else if(e.getActionCommand().equals("-")) {

x = Integer.parseInt(a.t.getText());

      a.t.setText(a.t.getText()+e.getActionCommand());

      j = 1;

}

 

else if(e.getActionCommand().equals("*")) {

x = Integer.parseInt(a.t.getText());

      a.t.setText(a.t.getText()+e.getActionCommand());

      c = 1;

}

 

else if(e.getActionCommand().equals("/")) {

x = Integer.parseInt(a.t.getText());

      a.t.setText(a.t.getText()+e.getActionCommand());

      u = 1;

}

 

else if(e.getActionCommand().equals("=")) {

y = Integer.parseInt(a.t.getText());

     if(s==1) {

     int sum = x+y;

     System.out.print(x);

     }

}

 

else {

a.t.setText(a.t.getText()+e.getActionCommand());

}

}

}

这段java程序哪里错了?


你程序的设计方法完全不对,就拿你的加法来说

(程序76行)

           y = Integer.parseInt(a.t.getText());

你若输入

1+2

那么

a.t.getText()的值为字串的"1+2"

它不能通过 Integer.parseInt变成整形的

且你的

int sum = x+y; 中的x又是什么?

相关标签:java

下一篇:谁有VC++的编程软件啊急需啊!!!

上一篇:vasp用gw计算带隙的赝势库用的是哪一个

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?