编写一个Java小应用程序,设计4个按钮,分别命名为“加”、“差”、“积”、“除”...
编写一个Java小应用程序,设计4个按钮,分别命名为“加”、“差”、“积”、“除”以及3个文本框。单击相应的按钮,两个文本框的数字做算术运算,在第3个文本框中显示结果。要...
编写一个Java小应用程序,设计4个按钮,分别命名为“加”、“差”、“积”、“除”以及3个文本框。单击相应的按钮,两个文本框的数字做算术运算,在第3个文本框中显示结果。要求处理NumberFormatException。
本题是Java小程序,题来自JAVA 2 实用教程(第二版) 清华大学出版社。
130页 第十章 习题3
急需这道题的完整代码。
请各位Java行家 写清楚。
不是jsp做
恩,最迟在明天下午两点之前。我就回学校了。在这先谢谢各位了。
回答者:little_goblin - 初入江湖 二级 7-7 09:19
很感谢你的回答,但这似乎不是我要的,我不要jsp的 展开
本题是Java小程序,题来自JAVA 2 实用教程(第二版) 清华大学出版社。
130页 第十章 习题3
急需这道题的完整代码。
请各位Java行家 写清楚。
不是jsp做
恩,最迟在明天下午两点之前。我就回学校了。在这先谢谢各位了。
回答者:little_goblin - 初入江湖 二级 7-7 09:19
很感谢你的回答,但这似乎不是我要的,我不要jsp的 展开
6个回答
展开全部
可以这样写:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleCalc extends JApplet implements ActionListener {
public static void main(String[] args) { // 这 main 方法只在调试时用到
SimpleCalc sc = new SimpleCalc();
sc.init();
JFrame f = new JFrame();
f.add(sc);
f.pack();
f.setVisible(true);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
private JTextField num1 = new JTextField(),
num2 = new JTextField(),
result = new JTextField(20);
public void init() {
setLayout(new GridLayout(0, 2, 2, 2));
add(num1);
add(num2);
for (String cmd: new String[]{"加", "差", "积", "除"}) {
JButton b = new JButton(cmd);
b.addActionListener(this);
add(b);
}
add(new JLabel("结果:", JLabel.TRAILING));
add(result);
携芹 result.setEditable(false);
getRootPane().setBorder(BorderFactory.createRaisedBevelBorder());
}
public void actionPerformed(ActionEvent e) {
try {
double n1 = Double.valueOf(num1.getText()),
n2 = Double.valueOf(num2.getText());
String cmd = e.getActionCommand();
兄晌 if (cmd == "加") result.setText(n1 + n2 + "");
if (cmd == "差") result.setText(n1 - n2 + "");
if (cmd == "积") result.setText(n1 * n2 + "");
羡隐锋 if (cmd == "除") result.setText(n1 / n2 + "");
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(this, "数字个数或格式错误!",
"", JOptionPane.ERROR_MESSAGE);
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//呵呵,俺又来了......
//这次是运庆启饥行过的,绝对没问题,保存为Canlulater.java就可以直接运行誉返了
//如果你不满意,还有要求直接旁码加我qq,我就不信我不行----516041982
import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Canlulater {
boolean packFrame = false;
public Jsq() {
JsqFrame frame = new JsqFrame();
if (packFrame) {
frame.pack();
} else {
frame.validate();
}
// Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}
new Jsq();
}
});
}
}
class JsqFrame extends JFrame {
JPanel contentPane;
JLabel lblFirst = new JLabel();
JLabel lblThird = new JLabel();
JTextField txtFirst = new JTextField();
JTextField txtThird = new JTextField();
JLabel lblFifth = new JLabel();
JTextField txtForth = new JTextField();
JButton btn1 = new JButton();
JButton btn2 = new JButton();
JButton btn3 = new JButton();
JButton btn4 = new JButton();
public JsqFrame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
this.setResizable(false);
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
lblFirst.setFont(new java.awt.Font("宋体", Font.PLAIN, 16));
lblFirst.setText("第一个数");
lblFirst.setBounds(new Rectangle(56, 28, 93, 34));
lblThird.setFont(new java.awt.Font("宋体", Font.PLAIN, 16));
lblThird.setText("第二个数");
lblThird.setBounds(new Rectangle(56, 91, 93, 34));
txtFirst.setBounds(new Rectangle(176, 28, 118, 34));
txtThird.setBounds(new Rectangle(176, 86, 118, 34));
lblFifth.setText("结果");
lblFifth.setBounds(new Rectangle(91, 202, 57, 34));
lblFifth.setFont(new java.awt.Font("宋体", Font.PLAIN, 18));
txtForth.setBounds(new Rectangle(179, 202, 135, 34));
btn1.setBounds(new Rectangle(36, 158, 49, 23));
btn1.setText("+");
btn1.addActionListener(new JsqFrame_btn1_actionAdapter(this));
btn2.setBounds(new Rectangle(112, 157, 48, 23));
btn2.setText("-");
btn2.addActionListener(new JsqFrame_jButton2_actionAdapter(this));
btn3.setBounds(new Rectangle(201, 158, 44, 23));
btn3.setText("*");
btn3.addActionListener(new JsqFrame_btn3_actionAdapter(this));
btn4.setBounds(new Rectangle(286, 158, 48, 23));
btn4.setText("/");
btn4.addActionListener(new JsqFrame_btn4_actionAdapter(this));
contentPane.add(lblFirst, null);
contentPane.add(txtFirst);
contentPane.add(txtThird);
contentPane.add(lblThird);
contentPane.add(btn1);
contentPane.add(btn2);
contentPane.add(btn4);
contentPane.add(btn3);
contentPane.add(txtForth);
contentPane.add(lblFifth);
}
public void btnFirst_actionPerformed(ActionEvent e) {
}
public void jButton2_actionPerformed(ActionEvent e) {
float a,b,c=0;
String m;
a = Integer.parseInt(txtFirst.getText());
b = Integer.parseInt(txtThird.getText());
c = a - b;
m = ""+c;
txtForth.setText(m);
}
public void btn1_actionPerformed(ActionEvent e) {
float a,b,c=0;
String m;
a = Integer.parseInt(txtFirst.getText());
b = Integer.parseInt(txtThird.getText());
c =a + b;
m = ""+c;
txtForth.setText(m);
}
public void btn4_actionPerformed(ActionEvent e) {
float a,b,c=0;
String m;
a = Integer.parseInt(txtFirst.getText());
b = Integer.parseInt(txtThird.getText());
c = a/b;
m = ""+c;
txtForth.setText(m);
}
public void btn3_actionPerformed(ActionEvent e) {
float a,b,c=0;
String m;
a = Integer.parseInt(txtFirst.getText());
b = Integer.parseInt(txtThird.getText());
c = a*b;
m = ""+c;
txtForth.setText(m);
}
}
class JsqFrame_btn3_actionAdapter implements ActionListener {
private JsqFrame adaptee;
JsqFrame_btn3_actionAdapter(JsqFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn3_actionPerformed(e);
}
}
class JsqFrame_btn4_actionAdapter implements ActionListener {
private JsqFrame adaptee;
JsqFrame_btn4_actionAdapter(JsqFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn4_actionPerformed(e);
}
}
class JsqFrame_btn1_actionAdapter implements ActionListener {
private JsqFrame adaptee;
JsqFrame_btn1_actionAdapter(JsqFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn1_actionPerformed(e);
}
}
class JsqFrame_jButton2_actionAdapter implements ActionListener {
private JsqFrame adaptee;
JsqFrame_jButton2_actionAdapter(JsqFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
//这次是运庆启饥行过的,绝对没问题,保存为Canlulater.java就可以直接运行誉返了
//如果你不满意,还有要求直接旁码加我qq,我就不信我不行----516041982
import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Canlulater {
boolean packFrame = false;
public Jsq() {
JsqFrame frame = new JsqFrame();
if (packFrame) {
frame.pack();
} else {
frame.validate();
}
// Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}
new Jsq();
}
});
}
}
class JsqFrame extends JFrame {
JPanel contentPane;
JLabel lblFirst = new JLabel();
JLabel lblThird = new JLabel();
JTextField txtFirst = new JTextField();
JTextField txtThird = new JTextField();
JLabel lblFifth = new JLabel();
JTextField txtForth = new JTextField();
JButton btn1 = new JButton();
JButton btn2 = new JButton();
JButton btn3 = new JButton();
JButton btn4 = new JButton();
public JsqFrame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
this.setResizable(false);
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
lblFirst.setFont(new java.awt.Font("宋体", Font.PLAIN, 16));
lblFirst.setText("第一个数");
lblFirst.setBounds(new Rectangle(56, 28, 93, 34));
lblThird.setFont(new java.awt.Font("宋体", Font.PLAIN, 16));
lblThird.setText("第二个数");
lblThird.setBounds(new Rectangle(56, 91, 93, 34));
txtFirst.setBounds(new Rectangle(176, 28, 118, 34));
txtThird.setBounds(new Rectangle(176, 86, 118, 34));
lblFifth.setText("结果");
lblFifth.setBounds(new Rectangle(91, 202, 57, 34));
lblFifth.setFont(new java.awt.Font("宋体", Font.PLAIN, 18));
txtForth.setBounds(new Rectangle(179, 202, 135, 34));
btn1.setBounds(new Rectangle(36, 158, 49, 23));
btn1.setText("+");
btn1.addActionListener(new JsqFrame_btn1_actionAdapter(this));
btn2.setBounds(new Rectangle(112, 157, 48, 23));
btn2.setText("-");
btn2.addActionListener(new JsqFrame_jButton2_actionAdapter(this));
btn3.setBounds(new Rectangle(201, 158, 44, 23));
btn3.setText("*");
btn3.addActionListener(new JsqFrame_btn3_actionAdapter(this));
btn4.setBounds(new Rectangle(286, 158, 48, 23));
btn4.setText("/");
btn4.addActionListener(new JsqFrame_btn4_actionAdapter(this));
contentPane.add(lblFirst, null);
contentPane.add(txtFirst);
contentPane.add(txtThird);
contentPane.add(lblThird);
contentPane.add(btn1);
contentPane.add(btn2);
contentPane.add(btn4);
contentPane.add(btn3);
contentPane.add(txtForth);
contentPane.add(lblFifth);
}
public void btnFirst_actionPerformed(ActionEvent e) {
}
public void jButton2_actionPerformed(ActionEvent e) {
float a,b,c=0;
String m;
a = Integer.parseInt(txtFirst.getText());
b = Integer.parseInt(txtThird.getText());
c = a - b;
m = ""+c;
txtForth.setText(m);
}
public void btn1_actionPerformed(ActionEvent e) {
float a,b,c=0;
String m;
a = Integer.parseInt(txtFirst.getText());
b = Integer.parseInt(txtThird.getText());
c =a + b;
m = ""+c;
txtForth.setText(m);
}
public void btn4_actionPerformed(ActionEvent e) {
float a,b,c=0;
String m;
a = Integer.parseInt(txtFirst.getText());
b = Integer.parseInt(txtThird.getText());
c = a/b;
m = ""+c;
txtForth.setText(m);
}
public void btn3_actionPerformed(ActionEvent e) {
float a,b,c=0;
String m;
a = Integer.parseInt(txtFirst.getText());
b = Integer.parseInt(txtThird.getText());
c = a*b;
m = ""+c;
txtForth.setText(m);
}
}
class JsqFrame_btn3_actionAdapter implements ActionListener {
private JsqFrame adaptee;
JsqFrame_btn3_actionAdapter(JsqFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn3_actionPerformed(e);
}
}
class JsqFrame_btn4_actionAdapter implements ActionListener {
private JsqFrame adaptee;
JsqFrame_btn4_actionAdapter(JsqFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn4_actionPerformed(e);
}
}
class JsqFrame_btn1_actionAdapter implements ActionListener {
private JsqFrame adaptee;
JsqFrame_btn1_actionAdapter(JsqFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn1_actionPerformed(e);
}
}
class JsqFrame_jButton2_actionAdapter implements ActionListener {
private JsqFrame adaptee;
JsqFrame_jButton2_actionAdapter(JsqFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
唉。。。k!
我的电脑有点问题!要不帮你做一做!
看吧!如果你的问题今天还没孙皮解决--我帮你!
电脑的问缓贺题我得弄会则哪差----看情况吧!估计晚上就好了--最迟明天!
楼主不着急把?
呵呵,今天晚上试一试!好久没做Java了。
最好你把你的一个邮箱告诉我,我把代码发过去!
我了个大概:
http://hi.baidu.com/solzmice/blog/item/681fdc3f4deeeec07c1e7170.html
楼住去看看吧 !
你可以自己设置,改改,嘿嘿!
我的电脑有点问题!要不帮你做一做!
看吧!如果你的问题今天还没孙皮解决--我帮你!
电脑的问缓贺题我得弄会则哪差----看情况吧!估计晚上就好了--最迟明天!
楼主不着急把?
呵呵,今天晚上试一试!好久没做Java了。
最好你把你的一个邮箱告诉我,我把代码发过去!
我了个大概:
http://hi.baidu.com/solzmice/blog/item/681fdc3f4deeeec07c1e7170.html
楼住去看看吧 !
你可以自己设置,改改,嘿嘿!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
编写一个Java小应用程序,设计4个按钮,分别命名为“加”、“悉卜差”、“积”、“除”以及3个文本框。单击相应的按钮,两个文本框的数字做算术运算,在第3个文本框中衫配显示结果。要求处理NumberFormatException。睁塌穗
我也不知道诶~~~~~~~~。。。。。。。。
我也不知道诶~~~~~~~~。。。。。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询