要求:用户界面新增支持 Windows GUI,同时保留原有命令行下所有功能。提示: 先测试驱动开发,然后重构代码,以GUI为目标修改"核心"函数,把与GUI/Console相关的部分提取出来
在原来实现四则运算的基础上通过java中的Swing组件实现了GUI版本
GUI的实现代码如下

public class CalMachineSwingBuild extends JFrame {

    private JPanel contentPane;
private JComboBox comboBox;
private JLabel lblNewLabel;
private JTextField textField;
private JLabel lblNewLabel_1;
private ArrayList<String> questionList=new ArrayList<String>();
private ArrayList<JTextField> answersByUser = new ArrayList<JTextField>(); //存放用户填写的答案的文本域
private ArrayList<String> RealAnswerList=new ArrayList<String>(); //存放真正的答案
private String strAnswerAndResult[]=new String[2];
private JLabel label_1;
private JLabel lblNewLabel_3;
private JButton submitButton;
private JPanel panelNorth;
private JPanel panelCenter;
private JPanel panelSouth;
private static int numOfQuestion; //问题的数量
private String itemValue; //难度级别
private String recordWrong=" ";
private String userName;
private int countWrong=0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalMachineSwingBuild frame = new CalMachineSwingBuild();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the frame.
*/
public CalMachineSwingBuild() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
panelNorth=new JPanel();
JLabel label = new JLabel("难度值");
label.setBounds(10, 10, 42, 15);
panelNorth.add(label);
//contentPane.add(label); comboBox = new JComboBox();
comboBox.addItem("简单");
comboBox.addItem("普通");
comboBox.addItem("复杂");
comboBox.setBounds(62, 7, 50, 100);
comboBox.setEditable(false);
comboBox.setSize(60, 20);
panelNorth.add(comboBox);
// contentPane.add(comboBox); lblNewLabel = new JLabel("题目数量");
lblNewLabel.setBounds(144, 10, 62, 15);
panelNorth.add(lblNewLabel);
// contentPane.add(lblNewLabel); textField = new JTextField();
textField.setBounds(216, 7, 46, 21);
// contentPane.add(textField);
textField.setColumns(10);
panelNorth.add(textField); JButton btnNewButton = new JButton("开始出题");
btnNewButton.setBounds(272, 6, 93, 23);
/* labelForSpec=new JLabel();
panelNorth.add(labelForSpec);*/
panelNorth.add(btnNewButton);
contentPane.add(panelNorth,BorderLayout.NORTH); panelSouth=new JPanel();
lblNewLabel_1 = new JLabel(); //用作提示
submitButton = new JButton("提交答案");//提交按钮
panelSouth.add(lblNewLabel_1);
panelSouth.add(submitButton);
contentPane.add(panelSouth,BorderLayout.SOUTH); panelCenter=new JPanel();
contentPane.add(panelCenter,BorderLayout.CENTER);
panelCenter.setLayout(new MigLayout("", "[][grow]", "[][]")); btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent event) {
repaint(); //重新展示界面
panelCenter.removeAll();
questionList.clear();
answersByUser.clear();
Pattern pattern = Pattern.compile("^+?[1-9][0-9]*$");
Matcher isNum = pattern.matcher(textField.getText());
if(!isNum.matches()){
lblNewLabel_1.setText("输入的题目数必须为整数!");
}else{
lblNewLabel_1.setText("");
itemValue=(String) comboBox.getSelectedItem();
numOfQuestion=Integer.parseInt(textField.getText());
CalMachine cm=new CalMachine();
CalculateMachine04 cm04=new CalculateMachine04();
switch(itemValue){
case "简单":
questionList=(ArrayList<String>) cm.addBracketTest(numOfQuestion); for(int i=0;i<questionList.size();i++){
JLabel questionBar = new JLabel();
panelCenter.add(questionBar, "cell 0 " + i + ",alignx trailing");
JTextField answerBar = new JTextField("", 10);
panelCenter.add(answerBar, "cell 1 " + i + " ,alignx trailing");
strAnswerAndResult=questionList.get(i).split("="); //前者为问题,后者为答案
questionBar.setText("question"+(i+1)+": "+strAnswerAndResult[0]);
RealAnswerList.add(strAnswerAndResult[1]); //存放真正的答案
answersByUser.add(answerBar);
}
break;
case "普通":
questionList=(ArrayList<String>) cm.generateSimpleQuestion(numOfQuestion);
for(int i=0;i<questionList.size();i++){
JLabel questionBar = new JLabel();
panelCenter.add(questionBar, "cell 0 " + i + ",alignx trailing");
JTextField answerBar = new JTextField("", 10);
panelCenter.add(answerBar, "cell 1 " + i + " ,alignx trailing");
strAnswerAndResult=questionList.get(i).split("="); //前者为问题,后者为答案
questionBar.setText("question"+(i+1)+": "+strAnswerAndResult[0]);
RealAnswerList.add(strAnswerAndResult[1]); //存放真正的答案
answersByUser.add(answerBar);
}
break;
case "复杂":
questionList=(ArrayList<String>) cm04.generateComplexQuestion(numOfQuestion);
for(int i=0;i<questionList.size();i++){
JLabel questionBar = new JLabel();
panelCenter.add(questionBar, "cell 0 " + i + ",alignx trailing");
JTextField answerBar = new JTextField("", 10);
panelCenter.add(answerBar, "cell 1 " + i + " ,alignx trailing");
strAnswerAndResult=questionList.get(i).split("="); //前者为问题,后者为答案
questionBar.setText("question"+(i+1)+": "+strAnswerAndResult[0]);
RealAnswerList.add(strAnswerAndResult[1]); //存放真正的答案
answersByUser.add(answerBar);
}
break;
}
System.out.println();
}
}
});
//提交判断
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { for(int i=0;i<numOfQuestion;i++){
if(!answersByUser.get(i).getText().trim().equals(RealAnswerList.get(i))){
//用户答错了
recordWrong+=(i+1)+",";
countWrong++;
}
}
if(recordWrong.length()>1){
lblNewLabel_1.setText("第"+recordWrong+"题答错了!");
}else if(textField.getText().length()==0){
lblNewLabel_1.setText("请输入题数!");
} else{
lblNewLabel_1.setText("恭喜你,全都搭对了!");
} /* if(e.getSource()==submitButton){
System.out.println("点击了"+itemValue);
new CalMachineSwingBuildResult(userName,itemValue,numOfQuestion
,countWrong,numOfQuestion-countWrong).show();;
}*/ recordWrong=""; }
});
} }

运行界面如下:

ssh:git@git.coding.net:muziliquan/GUIVersion.git

git:git://git.coding.net/muziliquan/GUIVersion.git

四则运算GUI版本的更多相关文章

  1. 四则运算GUI版本功能展现

    对于四则运算的GUI版本实现支持批量出题,由于我的不积极导致教师没找到对应的连接,现在重新补上链接 http://www.cnblogs.com/liquan/p/5978687.html codin ...

  2. Mac下好用的编辑器VIM GUI版本 VimR 推荐

    vim号称是编辑器之神,轮其功能和扩展性的确少有编辑器能比,但是大多数编辑器都有的文件浏览功能它确没有,虽然有些插件可以实现,但用起来都不是很方便,偶然发现了一个GUI版本的VIM,与普通的GUI版本 ...

  3. python猜数字GUI版本V0.2

    使用类方式编写猜数字游戏GUI版本. 思路:初始化数字以及初始化wegdits,编写button click event判断代码的函数,每猜一次点击button调用一次该函数,并计算猜的次数.如果猜对 ...

  4. Appium(二):Node.js下载与安装、非GUI版本appium下载与安装、GUI版本appium下载与安装

    1. 下载并安装Node.JS 进入官网:https://nodejs.org/en/. 由于我们是新手嘛,所以肯定是越稳定越好啦,所以选择下载LTS版本. 进入文件下点击文件就进入安装界面了,点击n ...

  5. GUI版本的emacs

    概要 emacs 配置 X11 配置 输入法配置 spacemacs 中的配置 fcitx 汉字显示方块的问题 总结 优势 劣势 概要 之前一直使用 terminal 版本的 emacs, 性能和显示 ...

  6. 结对编程四则运算gui

    码市地址:https://git.coding.net/linzhao/sizeyunsuangui.git 林 钊 -- 201421123105 吴世荣 -- 201421123119 王坤彬 - ...

  7. 四则运算GUI

    一.题目描述 我们在个人作业1中,用各种语言实现了一个命令行的四则运算小程序.进一步,本次要求把这个程序做成GUI(可以是Windows PC 上的,也可以是Mac.Linux,web,手机上的),成 ...

  8. 四则运算GUI版

    小学四则运算界面版 李永豪 201421123117 郑靖涛 201421123114 coding 地址:https://git.coding.net/ras/work2.git 一.题目描述 我们 ...

  9. 结对编程1.四则运算GUI版

    201421123022 王若凡        201421123026  欧阳勇 coding详细代码 a.需求分析: 这个程序做成GUI(可以是Windows PC 上的,也可以是Mac.Linu ...

随机推荐

  1. MFC之常用控件(四)

    常用控件主要包括:静态文本框.编辑框.单选按钮.复选框.分组框.列表框.组合框.图片控件.列表控件.树形控件和进度条控件等等.本节教程先来讲解静态文本框的使用. 控件的通知消息 在将静态文本框的使用之 ...

  2. @Mybatis传多个参数

    首选还是按照面向对象的方式执行sql.但是有时候入参对象嵌套的比较深,类中有类,面向对象就不太好处理了 主要有以下两种方式 1.DAO层的函数方法 public User selectUser(Str ...

  3. Web service是什么?

    原文:http://www.ruanyifeng.com/blog/2009/08/what_is_web_service.html 我认为,下一代互联网软件将建立在Web service(也就是&q ...

  4. Winform项目调用asp.net数据接口

    最近一个WPF项目需要改写成android项目,思路是在asp.net项目中编写一个通用接口,便于其它平台下调用数据.刚接触到这些东西的时候完全是一头雾水,最根本的原因是不明白网站中的一个网页,为什么 ...

  5. AbsListView.OnScrollListener

    public static interface AbsListView.OnScrollListener   android.widget.AbsListView.OnScrollListener 类 ...

  6. Git和SSH协议

    SSH(安全外壳协议)为Secure Shell的缩写,由IETF的网络工作小组(Network Working Group)所制定:SSH为建立在应用层和传输层基础上的安全协议.SSH是目前较可靠, ...

  7. iOS UITableView点击按钮滚到顶部

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  8. mac 启动nfsd共享

    # cat /etc/exports /Users/guang/jumpserver -maproot=root:wheel -alldirs -rw -network 192.168.244.0 - ...

  9. python之django 资料

    里边有不少比较好的文章. http://www.cnblogs.com/luxiaojun/p/5795070.html

  10. Fusioncharts使用说明

    背景 最近由于工作需要,再次接触到了Fusioncharts,但也有不足之处,现在官网上似乎是不支持flash的版本了,只能看到html5相关的javascript版本,无奈再次从网上搜索到了一些别人 ...