帮同学写了个随机50道0~100的算术题,外加GUI。昨晚电脑忘充电,在床上一下黑屏。。。
讲讲遇到了坑点吧。
(1)同学给的需求有点不实际,0~100的数不能重复,我就列个数组记录,但又要随机,那到最后就是查找哪个数没被用过(orz,为难蒟蒻的我0.0)
(2)就是Table是否可被编辑
首先Table 是可编辑 返回为true,如图
![图1](https://img-blog.csdn.net/20160916161319959)
所以并不需要另弄个类继承,重写覆盖这个方法,(0.0,事前我是重写了,还return false,绕了地球一圈),还有就是若你的Table return false,DefaultTableModel true也不行(DefaultTableModel 我也重建了个类 重写方法了,orz)
![图2](https://img-blog.csdn.net/20160916163643193)
(3)设置宽度
![图3](https://img-blog.csdn.net/20160916164122588)
(4)其他赶脚没什么好说的了,可以去看下源码
(5)写后感:本打算试下敏捷型开发(xp)但发现我都忘了,那就乱搞吧,封装还是不会,设计模式的概念貌似都没出现。。。
最后上源码
Line.java
package Dao;
import java.util.ArrayList;
import java.util.Random;
/**
* Created by Donald on 2016/9/15.
*/
public class Line {
private int num1[] = new int[50];
private int num2[] = new int[50];
private int result[] = new int[50];
private int tag[] = new int[100];
private Random random = new Random();
private int i = 0;
private int flag;
private ArrayList<String> al;
public void clear(){
i = 0;
al = null;
for(int j = 0; j < 100; ++j )
tag[j] = 0;
}
public void randLine(){
clear();
al = new ArrayList<String>();
while (i < 50){
createRandom();
if(spare())
fillTag();
else
continue;
if(flag == 1){
isExchange();
num2[i] = -num2[i];
}else{
if(overHundred()){
isExchange();
num2[i] = -num2[i];
}
}
setResultSum();
++i;
}
createString();
}
private boolean spare(){
return tag[num1[i]] ==0 && tag[num2[i]] == 0 ;
}
private void fillTag(){
tag[num1[i]] = tag[num2[i]] = 1;
}
private boolean overHundred(){
return num1[i] + num2[i] > 100;
}
private void isExchange(){
if(num2[i] > num1[i]){
int tage = num1[i];
num1[i] = num2[i];
num2[i] = tage;
}
}
private void createRandom(){
num1[i] = random.nextInt(100);
num2[i] = random.nextInt(100);
flag = random.nextInt(1);
}
private void setResultSum(){
result[i] = num1[i] + num2[i];
}
public void printLine(){
for(int j = 0 ; j < 50 ; ++j){
if(num2[j] >= 0)
System.out.println(j + 1 + "line: " + num1[j] + "+" + num2[j] + " = " + result[j]);
else
System.out.println(j + 1 + "line: " + num1[j] + num2[j] + " = " + result[j]);
}
}
public ArrayList<String> getAl(){
return al;
}
private void createString(){
String str;
for(int j = 0; j < 50; ++j){
if(num2[j] >= 0)
str = num1[j] + "+" + num2[j] + " = " ;
else
str = ""+ num1[j] + num2[j] + " = ";
al.add(str);
}
}
public int getResult(int j){
return result[j];
}
}
Lineview.java
package view;
import Dao.Line;
import javax.swing.*;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Donald on 2016/9/15.
*/
public class LineView
{
private JFrame jf = new JFrame();
Dimension faceSize = new Dimension(500, 800);
private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private JPanel pSelect = new JPanel();
private JLabel lSelect = new JLabel("Calculation problems");
private JButton bSelect = new JButton("start");
private JTable table;
private DivDefaultTableModel tableModel;
private JScrollPane tableScrollPane;
private Object[] tableTitle = {"Num", "Line", "answer","result"};
private Object[][] tableData = {new Object[]{""}};
private JPanel buttonPanel = new JPanel();
private JButton submit = new JButton("提交");
private JButton reset = new JButton("重置");
private Line line = new Line();
private JPanel pButton = new JPanel();
public void init() {
pSelect.add(lSelect);
pSelect.add(bSelect);
bSelect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clearTable();
line.randLine();
ArrayList<String> al = line.getAl();
int i = 0;
for(String str : al){
insertTable(i++, str);
}
}
});
tableModel=new DivDefaultTableModel(tableData, tableTitle);
table=new JTable(tableModel);
tableScrollPane=new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
buttonPanel.add(submit);
buttonPanel.add(reset);
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showResult();
}
} );
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
reset();
}
});
TableColumn numColumn = table.getColumn(tableTitle[0]);
numColumn.setMinWidth(20);
TableColumn lineColumn = table.getColumn(tableTitle[1]);
lineColumn.setMinWidth(200);
TableColumn inputColumn = table.getColumn(tableTitle[2]);
inputColumn.setMinWidth(50);
TableColumn resultColumn = table.getColumn(tableTitle[3]);
resultColumn.setMinWidth(100);
jf.setLayout(new BorderLayout());
jf.add(pSelect, BorderLayout.NORTH);
jf.add(tableScrollPane, BorderLayout.CENTER);
jf.add(buttonPanel, BorderLayout.SOUTH);
jf.pack();
jf.setSize(faceSize);
jf.setLocation((int)((screenSize.width-faceSize.getWidth())/2),
(int)((screenSize.height-faceSize.getHeight())/2));
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public void insertTable(int i, String str){
String [] newCell = new String[4];
newCell[0] = i + 1 +"";
newCell[1] = str;
tableModel.addRow(newCell);
}
public void clearTable(){
int rows=tableModel.getRowCount();
for(int i=rows-1;i>=0;i--){
tableModel.removeRow(i);
}
}
public void reset(){
int rows=tableModel.getRowCount();
for(int i=rows-1;i>=0;i--){
tableModel.setValueAt("", i, 2);
tableModel.setValueAt("", i, 3);
}
}
public void showResult(){
String strInt;
String numRegegEx = "^\\d+$";
Pattern numPattern = Pattern.compile(numRegegEx);
Matcher matcher = null;
for(int i = 0 ; i < 50; ++i){
if(tableModel.getValueAt(i, 2) == null || tableModel.getValueAt(i, 2).toString().trim().length() == 0){
tableModel.setValueAt("Not fill", i, 3);
continue;
}
strInt = tableModel.getValueAt(i, 2).toString().trim();
if(matcher == null)
matcher = numPattern.matcher(strInt);
else
matcher.reset(strInt);
if(!matcher.matches())
tableModel.setValueAt("Format error", i, 3);
else if(Integer.parseInt(strInt) == line.getResult(i))
tableModel.setValueAt("YES", i, 3);
else
tableModel.setValueAt("No, is " + line.getResult(i), i, 3);
}
}
public static void main(String []args) throws Exception{
new LineView().init();
}
}
MyJTable.java
public class DivDefaultTableModel extends DefaultTableModel{
public DivDefaultTableModel(Object[][] data, Object[] columnNames) {
super(data, columnNames);
}
public boolean isCellEditable(int row, int column){
if(column == 2)
return true;
else
return false;
}
}
DivDefaultTableModel.java
package view;
import javax.swing.table.DefaultTableModel;
/**
* Created by Donald on 2016/9/16.
*/
public class DivDefaultTableModel extends DefaultTableModel{
public DivDefaultTableModel(Object[][] data, Object[] columnNames) {
super(data, columnNames);
}
public boolean isCellEditable(int row, int column){
if(column == 2)
return true;
else
return false;
}
}