求JAVA高手帮忙修改一下!~

2024-12-28 20:49:38
推荐回答(2个)
回答1:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Jsq {
public static void main(String args[]) {
new JsqFrm();
}
}
class JsqFrm extends JFrame implements ActionListener {
JButton plu=new JButton("加法");
JButton sub=new JButton("减法");
JButton mul=new JButton("乘法");
JButton div=new JButton("除法");
public JsqFrm() {
super("计算器");
setBounds(250,180,360,280);
setVisible(true);
setLayout(null);
plu.setBounds(40,80,60,35);
add(plu);
sub.setBounds(110,80,60,35);
add(sub);
mul.setBounds(180,80,60,35);
add(mul);
div.setBounds(250,80,60,35);
add(div);
plu.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==plu) {
this.dispose();
new MainFrm("加法界面","加数1","加数2","算法1");
}
if (e.getSource()==sub) {
this.dispose();
new MainFrm("减法界面","减数1","减数2","算法2");
}
if (e.getSource()==mul) {
this.dispose();
new MainFrm("乘法界面","乘数1","乘数2","算法3");
}
if (e.getSource()==div) {
this.dispose();
new MainFrm("除法界面","除数1","除数2","算法4");
}
}
}
class MainFrm extends JFrame implements ActionListener { //ActionListener为实现动作监听的接口
JTextField textone=new JTextField(10);
JTextField texttwo=new JTextField(10);
JLabel numone;
JLabel numtwo;
JButton Sure=new JButton("确定");
JButton Cancel=new JButton("取消");
String num;
public MainFrm(String s,String one,String two,String i) {
super(s);
num=i;
setBounds(250,180,320,280);
setLayout(null);
numone=new JLabel(one);
numtwo=new JLabel(two);
numone.setBounds(60,40,60,40);
add(numone);
textone.setBounds(100,40,130,40);
add(textone);
numtwo.setBounds(60,80,60,40);
add(numtwo);
texttwo.setBounds(100,80,130,40);
add(texttwo);
Sure.setBounds(50,150,70,45);
add(Sure);
Cancel.setBounds(180,150,70,45);
add(Cancel);
setVisible(true);
Sure.addActionListener(this);
Cancel.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
public void actionPerformed(ActionEvent e) { //actionPerformed为ActionListener接口的一个方法,用于接受监听事件
if (e.getSource()==Sure) {
this.dispose();
if (textone.getText().trim().equals("")||texttwo.getText().trim().equals("")) {
new ErrorWindow("错误");
}
else
if (num=="算法1") {
new JsWindow("结果",textone.getText().trim(),texttwo.getText().trim(),"算法1");
}
if (num=="算法2") {
new JsWindow("结果",textone.getText().trim(),texttwo.getText().trim(),"算法2");
}
if (num=="算法3") {
new JsWindow("结果",textone.getText().trim(),texttwo.getText().trim(),"算法3");
}
if (num=="算法4") {
new JsWindow("结果",textone.getText().trim(),texttwo.getText().trim(),"算法4");
}
}
if (e.getSource()==Cancel) {
System.exit(1);
}
}
}
class JsWindow extends JFrame implements ActionListener {
JLabel jieguo;
JButton conti=new JButton("继续运算");
JButton exit=new JButton("退出");
public JsWindow(String s,String one,String two,String i) {
super(s);
setBounds(250,180,320,280);
setLayout(null);
conti.setBounds(100,100,90,30);
add(conti);
exit.setBounds(100,150,90,30);
add(exit);
double in=Double.parseDouble(one);
double jn=Double.parseDouble(two);
if (i=="算法1") {
jieguo=new JLabel("计算结果为:"+plus(in,jn));
}
if (i=="算法2") {
jieguo=new JLabel("计算结果为:"+sub(in,jn));
}
if (i=="算法3") {
jieguo=new JLabel("计算结果为:"+mul(in,jn));
}
if (i=="算法4") {
jieguo=new JLabel("计算结果为:"+div(in,jn));
}
jieguo.setBounds(90,15,200,80);
add(jieguo);
conti.addActionListener(this);
exit.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}

public void actionPerformed(ActionEvent e) {
if (e.getSource()==conti) {
this.dispose();
new JsqFrm();
}
if (e.getSource()==exit) {
System.exit(1);
}
}
public double plus(double x,double y) {
return x+y;
}
public double sub(double x,double y) {
return x-y;
}
public double mul(double x,double y) {
return x*y;
}
public double div(double x,double y) {
return x/y;
}
}
class ErrorWindow extends JFrame implements ActionListener {
JLabel errorinfo=new JLabel("输入的内容不能为空!");
JButton jreturn=new JButton("返回");
public ErrorWindow (String s) {
super(s);
setBounds(400,200,300,200);
setVisible(true);
setLayout(null);
errorinfo.setBounds(80,30,150,60);
add(errorinfo);
jreturn.setBounds(100,100,90,30);
add(jreturn);
jreturn.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==jreturn) {
this.dispose();
new JsqFrm();
}
}
}
自己写的,你看看把!

回答2:

你这操作也太复杂了吧,晕