源代码如下:
//startRun.java
public class startRun {
public static void main(String args[]){
mainFrame mainFrame=new mainFrame();
// logDialog logDialog=new logDialog(mainFrame);
// logDialog.setVisible(true);
mainFrame.setVisible(true);
}
}
//mainFrame.java
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.text.PlainDocument;
class mainFrame extends JFrame implements ActionListener,ItemListener{
//构造函数
mainFrame(){
super();
this.setSize(300,300);
qNum=new JTextField[6];
qScore=new JTextField[6];
difficultyFactor=new JComboBox[6];
// customButtons=new JButton[6];
for(int i=0;i<6;i++){
qNum[i]=new JTextField("");
qScore[i]=new JTextField("");
difficultyFactor[i]=new JComboBox();
difficultyFactor[i].addItem("1");
difficultyFactor[i].addItem("2");
difficultyFactor[i].addItem("3");
difficultyFactor[i].addItem("自定义");
difficultyFactor[i].setSelectedIndex(0);
// customButtons[i]=new JButton("自定义");
}
ok=new JButton("OK");
clear=new JButton("Clear All");
exit=new JButton("exit");
gridLayout=new GridLayout(7,4);
container=new Container();
container.setLayout(gridLayout);
container.add(new JLabel("题型"));
container.add(new JLabel("题量"));
container.add(new JLabel("分值"));
container.add(new JLabel("难度系数"));
// container.add(new JLabel(""));
container.add(new JLabel("填空题:"));
container.add(qNum[0]);
container.add(qScore[0]);
container.add(difficultyFactor[0]);
// container.add(customButtons[0]);
container.add(new JLabel("单选题:"));
container.add(qNum[1]);
container.add(qScore[1]);
container.add(difficultyFactor[1]);
// container.add(customButtons[1]);
container.add(new JLabel("多选题:"));
container.add(qNum[2]);
container.add(qScore[2]);
container.add(difficultyFactor[2]);
// container.add(customButtons[2]);
container.add(new JLabel("判断题:"));
container.add(qNum[3]);
container.add(qScore[3]);
container.add(difficultyFactor[3]);
// container.add(customButtons[3]);
container.add(new JLabel("简答题:"));
container.add(qNum[4]);
container.add(qScore[4]);
container.add(difficultyFactor[4]);
// container.add(customButtons[4]);
container.add(new JLabel("计算题:"));
container.add(qNum[5]);
container.add(qScore[5]);
container.add(difficultyFactor[5]);
// container.add(customButtons[5]);
Container maincontainer=this.getContentPane();
maincontainer.setLayout(new BorderLayout(10,10));
maincontainer.add(new JLabel(),BorderLayout.NORTH);
maincontainer.add(new JLabel(),BorderLayout.EAST);
maincontainer.add(new JLabel(),BorderLayout.WEST);
Container con=new Container();
con.setLayout(new GridLayout(1,3));
con.add(ok);
con.add(clear);
con.add(exit);
maincontainer.add(con,BorderLayout.SOUTH);
maincontainer.add(container,BorderLayout.CENTER);
//注册事件源
ok.addActionListener(this);
clear.addActionListener(this);
exit.addActionListener(this);
for(int i=0;i<6;i++) difficultyFactor[i].addActionListener(this);//.addItemListener(this);
// for(int i=0;i<6;i++) customButtons[i].addActionListener(this);
}
Container container;
GridLayout gridLayout;
JTextField qNum[];
JTextField qScore[];
JComboBox difficultyFactor[];
// JButton customButtons[];
JButton ok;
JButton clear;
JButton exit;
String[][] blackData,singleChoiceData,multChoiceData,judgementData,shortAnswerData,computingData;
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==ok){
for(int i=0;i<6;i++)
if(qNum[i].getText().isEmpty()){
JOptionPane.showMessageDialog(this.getContentPane(),
"先输入\"题量\"!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
//检查题量格式是否正确
else{
try{
if(Integer.valueOf(qNum[i].getText())==0){
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"必须大于0!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
}catch(NumberFormatException exception){
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"只能是整数!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
}
int row0=Integer.valueOf(qNum[0].getText());
blackData=new String[row0][2];
for(int i=0;i<row0;i++){
blackData[i][0]=i+1+"";
blackData[i][1]=difficultyFactor[0].getItemAt(difficultyFactor[0].getSelectedIndex()).toString();
}
int row1=Integer.valueOf(qNum[1].getText());
blackData=new String[row1][2];
for(int i=0;i<row1;i++){
blackData[i][0]=i+1+"";
blackData[i][1]=difficultyFactor[0].getItemAt(difficultyFactor[0].getSelectedIndex()).toString();
}
generateTestPaperDialog g=new generateTestPaperDialog(this);
g.setVisible(true);
}
else if(e.getSource()==clear){
for(int i=0;i<6;i++){
qNum[i].setText("");
qScore[i].setText("");
}
}
else if(e.getSource()==exit){
System.exit(0);
}
//对自定义按钮的响应处理
else if(e.getSource()==difficultyFactor[0]){
if(difficultyFactor[0].getSelectedIndex()!=3) return;
//检查题量是否为空
if(qNum[0].getText().isEmpty()){
difficultyFactor[0].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"先输入\"题量\"!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
//检查题量格式是否正确
else{
try{
if(Integer.valueOf(qNum[0].getText())==0){
difficultyFactor[0].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"必须大于0!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
}catch(NumberFormatException exception){
difficultyFactor[0].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"只能是整数!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
}
int row=Integer.valueOf(qNum[0].getText());
blackData=new String[row][2];
for(int i=0;i<row;i++){
blackData[i][0]=i+1+"";
blackData[i][1]="";
}
CustomDialog cDlg=new CustomDialog("自定义填空题",this);
cDlg.setVisible(true);
}
else if(e.getSource()==difficultyFactor[1]){
if(difficultyFactor[1].getSelectedIndex()!=3) return;
//检查题量是否为空
if(qNum[1].getText().isEmpty()){
difficultyFactor[1].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"先输入\"题量\"!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
//检查题量格式是否正确
else{
try{
if(Integer.parseInt(qNum[1].getText())==0){
difficultyFactor[1].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"必须大于0!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[1].setSelectedIndex(0);
return;
}
}catch(NumberFormatException exception){
difficultyFactor[1].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"只能是整数!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[1].setSelectedIndex(0);
return;
}
}
int row=Integer.valueOf(qNum[1].getText());
singleChoiceData=new String[row][2];
for(int i=0;i<row;i++){
singleChoiceData[i][0]=i+1+"";
singleChoiceData[i][1]="";
}
CustomDialog cDlg=new CustomDialog("自定义单选题",this);
cDlg.setVisible(true);
}
else if(e.getSource()==difficultyFactor[2]){
if(difficultyFactor[2].getSelectedIndex()!=3)return;
//检查题量是否为空
if(qNum[2].getText().isEmpty()){
difficultyFactor[2].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"先输入\"题量\"!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
//检查题量格式是否正确
else{
try{
if(Integer.parseInt(qNum[2].getText())==0){
difficultyFactor[2].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"必须大于0!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[2].setSelectedIndex(0);
return;
}
}catch(NumberFormatException exception){
difficultyFactor[2].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"只能是整数!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[2].setSelectedIndex(0);
return;
}
}
int row=Integer.valueOf(qNum[2].getText());
multChoiceData=new String[row][2];
for(int i=0;i<row;i++){
multChoiceData[i][0]=i+1+"";
multChoiceData[i][1]="";
}
CustomDialog cDlg=new CustomDialog("自定义多选题",this);
cDlg.setVisible(true);
}
else if(e.getSource()==difficultyFactor[3]){
if(difficultyFactor[3].getSelectedIndex()!=3)return;
//检查题量是否为空
if(qNum[3].getText().isEmpty()){
difficultyFactor[3].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"先输入\"题量\"!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
//检查题量格式是否正确
else{
try{
if(Integer.parseInt(qNum[3].getText())==0){
difficultyFactor[3].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"必须大于0!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[3].setSelectedIndex(0);
return;
}
}catch(NumberFormatException exception){
difficultyFactor[3].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"只能是整数!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[3].setSelectedIndex(0);
return;
}
}
int row=Integer.valueOf(qNum[3].getText());
judgementData=new String[row][2];
for(int i=0;i<row;i++){
judgementData[i][0]=i+1+"";
judgementData[i][1]="";
}
CustomDialog cDlg=new CustomDialog("自定义判断题",this);
cDlg.setVisible(true);
}
else if(e.getSource()==difficultyFactor[4]){
if(difficultyFactor[4].getSelectedIndex()!=3)return;
//检查题量是否为空
if(qNum[4].getText().isEmpty()){
difficultyFactor[4].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"先输入\"题量\"!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
//检查题量格式是否正确
else{
try{
if(Integer.parseInt(qNum[4].getText())==0){
difficultyFactor[4].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"必须大于0!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[4].setSelectedIndex(0);
return;
}
}catch(NumberFormatException exception){
difficultyFactor[4].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"只能是整数!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[4].setSelectedIndex(0);
return;
}
}
int row=Integer.valueOf(qNum[4].getText());
shortAnswerData=new String[row][2];
for(int i=0;i<row;i++){
shortAnswerData[i][0]=i+1+"";
shortAnswerData[i][1]="";
}
CustomDialog cDlg=new CustomDialog("自定义简答题",this);
cDlg.setVisible(true);
}
else if(e.getSource()==difficultyFactor[5]){
if(difficultyFactor[5].getSelectedIndex()!=3)return;
//检查题量是否为空
if(qNum[5].getText().isEmpty()){
difficultyFactor[5].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"先输入\"题量\"!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
//检查题量格式是否正确
else{
try{
if(Integer.parseInt(qNum[5].getText())==0){
difficultyFactor[5].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"必须大于0!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[5].setSelectedIndex(0);
return;
}
}catch(NumberFormatException exception){
difficultyFactor[5].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"只能是整数!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[5].setSelectedIndex(0);
return;
}
}
int row=Integer.valueOf(qNum[5].getText());
computingData=new String[row][2];
for(int i=0;i<row;i++){
computingData[i][0]=i+1+"";
computingData[i][1]="";
}
CustomDialog cDlg=new CustomDialog("自定义计算题",this);
cDlg.setVisible(true);
}
else{
JOptionPane.showMessageDialog(this.getContentPane(),
"Class:mainFrame Function:actionPerformed 出错!", "系统信息", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(e.getStateChange()==ItemEvent.SELECTED){
//填空题
if(e.getSource().equals(difficultyFactor[0].getItemAt(3))){
//检查题量是否为空
if(qNum[0].getText().isEmpty()){
difficultyFactor[0].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"先输入\"题量1\"!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
//检查题量格式是否正确
else{
try{
if(Integer.valueOf(qNum[0].getText())==0){
difficultyFactor[0].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量1\"必须大于0!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
}catch(NumberFormatException exception){
difficultyFactor[0].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量1\"只能是整数!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
}
int row=Integer.valueOf(qNum[0].getText());
blackData=new String[row][2];
for(int i=0;i<row;i++){
blackData[i][0]=i+1+"";
blackData[i][1]="";
}
CustomDialog cDlg=new CustomDialog("自定义填空题",this);
cDlg.setVisible(true);
}
//单选题
if(e.getSource().equals(difficultyFactor[1].getItemAt(3))){
//检查题量是否为空
if(qNum[1].getText().isEmpty()){
difficultyFactor[1].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"先输入\"题量2\"!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
//检查题量格式是否正确
else{
try{
if(Integer.parseInt(qNum[1].getText())==0){
difficultyFactor[1].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量2\"必须大于0!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[1].setSelectedIndex(0);
return;
}
}catch(NumberFormatException exception){
difficultyFactor[1].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量2\"只能是整数!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[1].setSelectedIndex(0);
return;
}
}
int row=Integer.valueOf(qNum[1].getText());
singleChoiceData=new String[row][2];
for(int i=0;i<row;i++){
singleChoiceData[i][0]=i+1+"";
singleChoiceData[i][1]="";
}
CustomDialog cDlg=new CustomDialog("自定义单选题",this);
cDlg.setVisible(true);
}
//多选题
if(e.getItem()==difficultyFactor[2].getItemAt(3)){
//检查题量是否为空
if(qNum[2].getText().isEmpty()){
difficultyFactor[2].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"先输入\"题量3\"!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
//检查题量格式是否正确
else{
try{
if(Integer.parseInt(qNum[2].getText())==0){
difficultyFactor[2].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量3\"必须大于0!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[2].setSelectedIndex(0);
return;
}
}catch(NumberFormatException exception){
difficultyFactor[2].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"只能是整数!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[2].setSelectedIndex(0);
return;
}
}
int row=Integer.valueOf(qNum[2].getText());
multChoiceData=new String[row][2];
for(int i=0;i<row;i++){
multChoiceData[i][0]=i+1+"";
multChoiceData[i][1]="";
}
CustomDialog cDlg=new CustomDialog("自定义多选题",this);
cDlg.setVisible(true);
}
//判断题
if(e.getItem()==difficultyFactor[3].getItemAt(3)){
//检查题量是否为空
if(qNum[3].getText().isEmpty()){
difficultyFactor[3].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"先输入\"题量\"!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
//检查题量格式是否正确
else{
try{
if(Integer.parseInt(qNum[3].getText())==0){
difficultyFactor[3].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"必须大于0!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[3].setSelectedIndex(0);
return;
}
}catch(NumberFormatException exception){
difficultyFactor[3].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"只能是整数!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[3].setSelectedIndex(0);
return;
}
}
int row=Integer.valueOf(qNum[3].getText());
judgementData=new String[row][2];
for(int i=0;i<row;i++){
judgementData[i][0]=i+1+"";
judgementData[i][1]="";
}
CustomDialog cDlg=new CustomDialog("自定义判断题",this);
cDlg.setVisible(true);
}
//简答题
if(e.getItem()==difficultyFactor[4].getItemAt(3)){
//检查题量是否为空
if(qNum[4].getText().isEmpty()){
difficultyFactor[4].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"先输入\"题量\"!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
//检查题量格式是否正确
else{
try{
if(Integer.parseInt(qNum[4].getText())==0){
difficultyFactor[4].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"必须大于0!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[4].setSelectedIndex(0);
return;
}
}catch(NumberFormatException exception){
difficultyFactor[4].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"只能是整数!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[4].setSelectedIndex(0);
return;
}
}
int row=Integer.valueOf(qNum[4].getText());
shortAnswerData=new String[row][2];
for(int i=0;i<row;i++){
shortAnswerData[i][0]=i+1+"";
shortAnswerData[i][1]="";
}
CustomDialog cDlg=new CustomDialog("自定义简答题",this);
cDlg.setVisible(true);
}
//计算题
if(e.getItem()==difficultyFactor[5].getItemAt(3)){
//检查题量是否为空
if(qNum[5].getText().isEmpty()){
difficultyFactor[5].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"先输入\"题量\"!", "系统信息", JOptionPane.WARNING_MESSAGE);
return;
}
//检查题量格式是否正确
else{
try{
if(Integer.parseInt(qNum[5].getText())==0){
difficultyFactor[5].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"必须大于0!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[5].setSelectedIndex(0);
return;
}
}catch(NumberFormatException exception){
difficultyFactor[5].setSelectedIndex(0);
JOptionPane.showMessageDialog(this.getContentPane(),
"\"题量\"只能是整数!", "系统信息", JOptionPane.WARNING_MESSAGE);
difficultyFactor[5].setSelectedIndex(0);
return;
}
}
int row=Integer.valueOf(qNum[5].getText());
computingData=new String[row][2];
for(int i=0;i<row;i++){
computingData[i][0]=i+1+"";
computingData[i][1]="";
}
CustomDialog cDlg=new CustomDialog("自定义计算题",this);
cDlg.setVisible(true);
}
}
}
}
//CustomDialog.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class CustomDialog extends JFrame implements ActionListener{
CustomDialog(String title,mainFrame parent){
ok=new JButton("ok");
cancel=new JButton("cancel");
exit=new JButton("exit");
JPanel buttonPanel=new JPanel();
buttonPanel.setLayout(new GridLayout(1,3));
buttonPanel.add(ok);
buttonPanel.add(cancel);
buttonPanel.add(exit);
this.setTitle(title);
this.setSize(300,300);
String[] Header = {"题号","难度"};
table=new JTable(parent.blackData,Header);
JScrollPane tablePanel=new JScrollPane(table);
this.getContentPane().setLayout(new BorderLayout(4,4));
this.getContentPane().add(tablePanel,BorderLayout.CENTER);
this.getContentPane().add(new JPanel(),BorderLayout.EAST);
this.getContentPane().add(new JPanel(),BorderLayout.WEST);
this.getContentPane().add(new JPanel(),BorderLayout.NORTH);
this.getContentPane().add(buttonPanel,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e){
}
private JTable table;
private JButton ok;
private JButton cancel;
private JButton exit;
}