姓名:钱宇坤学号:2011435112
实验6 图形用户界面设计
1.实验目的
掌握Java Swing组件的使用方法,理解委托时间处理模型,掌握多种布局方式,掌握窗口菜单和快捷菜单的设计方式,熟悉在组件上绘图的方法,设计具有图形用户界面的、能够响应时间的Java应用程序。
2.实验内容
计算器:实现计算器的加、减、乘、除等运算,并处理异常。
3.实验分析
声明一个类Caculator并实现ActionListener,实现加、减、乘、除、开平方的功能,并对异常进行处理。构造函数Caculator()中初始化界面,对控件进行布局,声明两个Jpanel,分别是j1和j2。j1中添加一个JtextFeild,j2中添加GridLayout 布局,依次添加20个JButton,并分别添加事件监听。在actionPerformed()函数中实现对相应事件的处理,并对可能出现的异常进行处理(弹出对话框)。
声明MJDialog类,实现一个对父窗体JFrame依赖对话框的功能。在MJdialog 类中声明要依赖的父窗体JFrame和一个能够显示任意字符串的Jlabel,重写show()方法设置窗体中Jlabel显示的内容。
4.计算器使用
(1)+ --- * / 功能:运算数1 +“运算符”+运算数2+“=”
(2)sqrt功能:运算数+“sqrt”
(3)CE功能:只清除最后一个运算数
(4)C功能:清除计算器所有信息
(5)+/-功能:对运算数进行取反。运算数+“+/-”
5.实验代码
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Calculator implements ActionListener {
double c, n;
String s1, s2, s3, s4, s5;
JFrame f;
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15,b16,b17,b18,b19,b20;
JTextField tf1;
public Calculator() {
f = new JFrame("Calculator");
f.setSize(250,250);
f.setDefaultCloseOperation(3);
//f.setLayout(new GridLayout(6,4));
f.setResizable(false);
b1 = new JButton("0");
//b1.setSize(20,17);
b1.addActionListener(this);
b2 = new JButton("1");
//b2.setSize(20, 17);
b2.addActionListener(this);
b3 = new JButton("2");
//b3.setSize(20, 17);
b3.addActionListener(this);
b4 = new JButton("3");
//b4.setSize(20, 17);
b4.addActionListener(this);
b5 = new JButton("4");
//b5.setSize(20, 17);
b5.addActionListener(this);
b6 = new JButton("5");
//b6.setSize(20, 17);
b6.addActionListener(this);
b7 = new JButton("6");
//b7.setSize(20, 17);
b7.addActionListener(this);
b8 = new JButton("7");
//b8.setSize(20, 17);
b8.addActionListener(this);
b9 = new JButton("8");
b9.setSize(20, 17);
b9.addActionListener(this);
b10 = new JButton("9");
// b10.setSize(20, 17);
b10.addActionListener(this);
b11 = new JButton("+");
//b11.setSize(20, 17);
b11.addActionListener(this);
b12 = new JButton("-");
//b12.setSize(20, 17);
b12.addActionListener(this);
b13 = new JButton("x");
//b13.setSize(20, 17);
b13.addActionListener(this);
b14 = new JButton("/");
//b14.setSize(20, 17);
b14.addActionListener(this);
b15 = new JButton("=");
//b15.setSize(20, 17);
b15.addActionListener(this);
b16 = new JButton("C");
//b16.setSize(20, 17);
b16.addActionListener(this);
//////////////////////////////////
b17 = new JButton("sqrt");
//b17.setSize(20,17);
b17.addActionListener(this);
b18 = new JButton("+/-");
b18.setSize(20,17);
b18.addActionListener(this);
b19 = new JButton(". ");
//b19.setSize(20,17);
b19.addActionListener(this);
b20 = new JButton("CE");
//b20.setSize(20,17);
b20.addActionListener(this);
tf1 = new JTextField(20);
JPanel j1=new JPanel();
j1.add(tf1);
JPanel j2=new JPanel();
j2.setLayout(new GridLayout(5,4,4,1)); j2.add(b17);
j2.add(b18);
j2.add(b20);
j2.add(b16);
j2.add(b8);
j2.add(b9);
j2.add(b10);
j2.add(b14);
j2.add(b5);
j2.add(b6);
j2.add(b7);
j2.add(b13);
j2.add(b2);
j2.add(b3);
j2.add(b4);
j2.add(b12);
j2.add(b1);
j2.add(b19);
j2.add(b15);
j2.add(b11);
f.setLayout(new BorderLayout());
f.add(j1,BorderLayout.NORTH);
f.add(j2,BorderLayout.CENTER);
f.setLocation(500, 300);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) { if (e.getSource() == b1) {
s3 = tf1.getText();
s4 = "0";
s5 = s3 + s4;
tf1.setText(s5);
}
if (e.getSource() == b2) {
s3 = tf1.getText();
s4 = "1";
s5 = s3 + s4;
tf1.setText(s5);
}
if (e.getSource() == b3) {
s3 = tf1.getText();
s4 = "2";
s5 = s3 + s4;
tf1.setText(s5);
}
if (e.getSource() == b4) {
s3 = tf1.getText();
s4 = "3";
s5 = s3 + s4;
tf1.setText(s5);
}
if (e.getSource() == b5) {
s3 = tf1.getText();
s4 = "4";
s5 = s3 + s4;
tf1.setText(s5);
}
if (e.getSource() == b6) {
s3 = tf1.getText();
s4 = "5";
s5 = s3 + s4;
tf1.setText(s5);
}
if (e.getSource() == b7) { s3 = tf1.getText();
s4 = "6";
s5 = s3 + s4;
tf1.setText(s5);
}
if (e.getSource() == b8) { s3 = tf1.getText();
s4 = "7";
s5 = s3 + s4;
tf1.setText(s5);
}
if (e.getSource() == b9) { s3 = tf1.getText();
s4 = "8";
s5 = s3 + s4;
tf1.setText(s5);
}
if (e.getSource() == b10) { s3 = tf1.getText();
s4 = "9";
s5 = s3 + s4;
tf1.setText(s5);
}
if (e.getSource() == b11) { s1 = tf1.getText();
tf1.setText("");
c = 1;
}
if (e.getSource() == b12) { s1 = tf1.getText();
tf1.setText("");
c = 2;
}
if (e.getSource() == b13) { s1 = tf1.getText();
tf1.setText("");
c = 3;
}
if (e.getSource() == b14) { s1 = tf1.getText();
tf1.setText("");
c = 4;
}
if(e.getSource()==b17)
{
s1=tf1.getText();
try {
tf1.setText(String.valueOf(Math.sqrt(Double.parseDouble(s1))));
} catch ( Exception exception) {
// TODO: handle exception
MJdialog jDialog=new MJdialog(f);
jDialog.Show("请先输入纯数字!然后再做sqrt运算!");
}
}
if (e.getSource()==b18)
{
try {
tf1.setText(String.valueOf(0-Double.parseDouble(tf1.getText())));
} catch (Exception e2) {
// TODO: handle exception
MJdialog jDialog=new MJdialog(f);
jDialog.Show("Error!");
}
}
if (e.getSource()==b19)
{
s3 = tf1.getText();
s4 = ".";
s5 = s3 + s4;
tf1.setText(s5);
// try {
// s1=tf1.getText();
// tf1.setText(String.valueOf(Double.parseDouble(s1)));
// } catch (Exception e2) {
// // TODO: handle exception
// MJdialog jDialog=new MJdialog(f);
// jDialog.Show("请先输入纯数字!");
// }
}
if(e.getSource()==b20)
{
tf1.setText("");
}
if (e.getSource() == b15) {
s2 = tf1.getText();
if (c == 1) {
n = Double.parseDouble(s1) + Double.parseDouble(s2);
tf1.setText(String.valueOf(n));
} else if (c == 2) {
n = Double.parseDouble(s1) - Double.parseDouble(s2);
tf1.setText(String.valueOf(n));
} else if (c == 3) {
n = Double.parseDouble(s1) *Double.parseDouble(s2);
tf1.setText(String.valueOf(n));
} else if (c == 4) {
n = Double.parseDouble(s1) / Double.parseDouble(s2);
tf1.setText(String.valueOf(n));
}
}
if (e.getSource() == b16) {
s1=null;
s2=null;
tf1.setText("");
}
}
public static void main(String args[]) {
Calculator c = new Calculator();
}
}
class MJdialog extends JDialog
{
JLabel label=null;
JFrame frame=null;
public MJdialog(JFrame frame)
{
super(frame, "警告!");
this.frame=frame;
this.setSize(300,80);
https://www.doczj.com/doc/9d10768452.html,bel=new JLabel("",JLabel.CENTER);
this.getContentPane().add(label);
this.setDefaultCloseOperation(3);
}
void Show(String s)
{
label.setText(s);
this.setLocation(frame.getX()+100, frame.getY()+100);
this.setVisible(true );
}
}