您的位置:首页 > 编程语言

第4次作业类测试代码+101+谢艳敏

2017-05-03 18:10 288 查看
类测试代码的具体要求如下:

界面操作说明补充:

点击OK,如果输入有效,进行相应的数值计算;如果数值不满足约束,则弹出错误说明,统一为“输入有误,请重新输入”,然后回到初始输入状态。

点击Cancle,表示重置,清空前面的数据,回到初始状态。

(2)NextDate函数问题

String nextdate(int m,int d,int y)

建立界面,至少包含以下元素,但不限于此:

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.JOptionPane;
import javax.swing.JTextField;

public class NextDateUI {

public static void main(String[] args) {
// TODO Auto-generated method stub

// 创建标题为“日期计算程序”的窗口
JFrame frame = new JFrame("日期计算程序");
frame.setLayout(null);
// 标签
JLabel lab_prompt = new JLabel("请输入需要计算的年月日:");
JLabel lab_year = new JLabel("年:");
JLabel lab_month = new JLabel("月:");
JLabel lab_day = new JLabel("日:");
JLabel lab_weekday = new JLabel("这天是星期:");
JLabel lab_nextday = new JLabel("下一天是:");
JLabel lab_lastday = new JLabel("上一天是:");
// 文本框
JTextField txt_year = new JTextField();
JTextField txt_month = new JTextField();
JTextField txt_day = new JTextField();
JTextField txt_weekday = new JTextField();
JTextField txt_nextday = new JTextField();
JTextField txt_lastday = new JTextField();
// 按钮
JButton btn_submit = new JButton("Ok");
JButton btn_cancel = new JButton("Cancel");
// 将组件添加到frame
frame.add(lab_prompt);
frame.add(lab_year);
frame.add(lab_month);
frame.add(lab_day);
frame.add(lab_weekday);
frame.add(lab_nextday);
frame.add(lab_lastday);
frame.add(txt_year);
frame.add(txt_month);
frame.add(txt_day);
frame.add(txt_weekday);
frame.add(txt_nextday);
frame.add(txt_lastday);
frame.add(btn_submit);
frame.add(btn_cancel);

// 为标签组件设置大小位置
lab_prompt.setBounds(20, 20, 500, 30);
lab_year.setBounds(40, 60, 50, 30);
lab_month.setBounds(160, 60, 50, 30);
lab_day.setBounds(270, 60, 50, 30);
lab_weekday.setBounds(40, 160, 100, 30);
lab_nextday.setBounds(40, 210, 150, 30);
lab_lastday.setBounds(40, 260, 150, 30);
// 为文本框组件设置大小位置
txt_year.setBounds(60, 60, 70, 30);
txt_month.setBounds(180, 60, 70, 30);
txt_day.setBounds(290, 60, 70, 30);
txt_weekday.setBounds(120, 160, 100, 30);
txt_weekday.setEnabled(false);
txt_nextday.setBounds(110, 210, 150, 30);
txt_nextday.setEnabled(false);
txt_lastday.setBounds(110, 260, 150, 30);
txt_lastday.setEnabled(false);
// 为按钮组件设置大小位置
btn_submit.setBounds(120, 110, 80, 30);
btn_cancel.setBounds(240, 110, 80, 30);
// 设置窗口的位置和大小
frame.setBounds(500, 200, 430, 370);
frame.setVisible(true);

// 按钮事件
// OK按钮
btn_submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn_submit) {
String y, m, d;
y = txt_year.getText().toString();
m = txt_month.getText().toString();
d = txt_day.getText().toString();

// 用正则表达式判断是否为数字
if (y.matches("[0-9]+") && m.matches("[0-9]+") && d.matches("[0-9]+")) {
int year = Integer.parseInt(txt_year.getText());
int month = Integer.parseInt(txt_month.getText());
int day = Integer.parseInt(txt_day.getText());
if (NextDate.checkDate(year, month, day)) {
// 下一天
txt_nextday.setText(NextDate.nextDate(month, day, year));
// 上一天
txt_lastday.setText(NextDate.lastDay(month, day, year));
// 周几
txt_weekday.setText(NextDate.weekDay(month, day, year));
} else {
JOptionPane.showMessageDialog(null, "输入有误,请重请输入");
txt_year.setText("");
txt_month.setText("");
txt_day.setText("");
}
} else {
JOptionPane.showMessageDialog(null, "输入有误,请重请输入");
txt_year.setText("");
txt_month.setText("");
txt_day.setText("");
}
}
}
});
// Cancel按钮
btn_cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn_cancel) {
txt_year.setText("");
txt_month.setText("");
txt_day.setText("");
txt_weekday.setText("");
txt_nextday.setText("");
txt_lastday.setText("");
}

}
});
}

}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: