帮忙编写一个JAVA的程序
编写一个应用程序,有一个标题为“计算”的窗口,窗口的布局为flowlayout布局,设计4个按钮,分别命名为,加,差,积,除…另外,窗口中还有3个文本框,单击相应的按钮,...
编写一个应用程序,有一个标题为“计算”的窗口,窗口的布局为flowlayout布局, 设计4个按钮,分别命名为,加,差,积,除…另外,窗口中还有3个文本框, 单击相应的按钮,将3个文本框的数字做运算,在第三个文本框中显示结果, 要求处理numberformatexception
393253310@qq.com 展开
393253310@qq.com 展开
展开全部
package swing;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TestResult extends JFrame implements ActionListener{
JTextField jfforward,jfcenter,jfbehind;
JButton jequals;
JComboBox list;
JLabel jl;
TestResult(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
jfforward = new JTextField(8);
jl = new JLabel();
jfcenter = new JTextField(8);
jfbehind = new JTextField(8);
jequals = new JButton("=");
list = new JComboBox();
setTitle("四则运算");
list.addItem("+");
list.addItem("-");
list.addItem("*");
list.addItem("/");
add(jfforward);
add(list);
add(jfcenter);
add(jequals);
add(jfbehind);
add(jl);
jequals.addActionListener(this);
this.setResizable(false);
this.setSize(500, 500);
this.setVisible(true);
}
public static void main(String[] args) {
new TestResult();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jequals){
switch(list.getSelectedIndex()){
case 0:try{
int n = Integer.parseInt(jfforward.getText());
int m = Integer.parseInt(jfcenter.getText());
int sum=n+m;
jfbehind.setText(""+sum);
}catch(Exception e3){
jl.setText("你输入了非法字符");
jfforward.setText("");
jfcenter.setText("");
}
break;
case 1:try{jl.setText("");
int n = Integer.parseInt(jfforward.getText());
int m = Integer.parseInt(jfcenter.getText());
int sum=n-m;
jfbehind.setText(""+sum);
}catch(Exception e2){
jl.setText("你输入了非法字符");
jfforward.setText("");
jfcenter.setText("");
}
break;
case 2:try{
jl.setText("");
int n = Integer.parseInt(jfforward.getText());
int m = Integer.parseInt(jfcenter.getText());
int sum=n*m;
jfbehind.setText(""+sum);
}catch(Exception e1){
jl.setText("你输入了非法字符");
jfforward.setText("");
jfcenter.setText("");
}
break;
case 3:
if(jfcenter.getText()!=null){
try{jl.setText("");
int n = Integer.parseInt(jfforward.getText());
int m = Integer.parseInt(jfcenter.getText());
int sum=n/m;
jfbehind.setText(""+sum);
}catch(Exception eo){
jl.setText("你输入了非法字符");
jfforward.setText("");
jfcenter.setText("");
}
}
break;
}
}
}
}
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TestResult extends JFrame implements ActionListener{
JTextField jfforward,jfcenter,jfbehind;
JButton jequals;
JComboBox list;
JLabel jl;
TestResult(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
jfforward = new JTextField(8);
jl = new JLabel();
jfcenter = new JTextField(8);
jfbehind = new JTextField(8);
jequals = new JButton("=");
list = new JComboBox();
setTitle("四则运算");
list.addItem("+");
list.addItem("-");
list.addItem("*");
list.addItem("/");
add(jfforward);
add(list);
add(jfcenter);
add(jequals);
add(jfbehind);
add(jl);
jequals.addActionListener(this);
this.setResizable(false);
this.setSize(500, 500);
this.setVisible(true);
}
public static void main(String[] args) {
new TestResult();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jequals){
switch(list.getSelectedIndex()){
case 0:try{
int n = Integer.parseInt(jfforward.getText());
int m = Integer.parseInt(jfcenter.getText());
int sum=n+m;
jfbehind.setText(""+sum);
}catch(Exception e3){
jl.setText("你输入了非法字符");
jfforward.setText("");
jfcenter.setText("");
}
break;
case 1:try{jl.setText("");
int n = Integer.parseInt(jfforward.getText());
int m = Integer.parseInt(jfcenter.getText());
int sum=n-m;
jfbehind.setText(""+sum);
}catch(Exception e2){
jl.setText("你输入了非法字符");
jfforward.setText("");
jfcenter.setText("");
}
break;
case 2:try{
jl.setText("");
int n = Integer.parseInt(jfforward.getText());
int m = Integer.parseInt(jfcenter.getText());
int sum=n*m;
jfbehind.setText(""+sum);
}catch(Exception e1){
jl.setText("你输入了非法字符");
jfforward.setText("");
jfcenter.setText("");
}
break;
case 3:
if(jfcenter.getText()!=null){
try{jl.setText("");
int n = Integer.parseInt(jfforward.getText());
int m = Integer.parseInt(jfcenter.getText());
int sum=n/m;
jfbehind.setText(""+sum);
}catch(Exception eo){
jl.setText("你输入了非法字符");
jfforward.setText("");
jfcenter.setText("");
}
}
break;
}
}
}
}
更多追问追答
追问
能不能帮忙直接做出来,谢谢、、
追答
把Exception 改为 numberformatexception
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询