⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 additemdialog.java

📁 一个带界面的在线测试系统
💻 JAVA
字号:
/*
 * AddItemDialog.java
 *
 * Created on 2007年12月19日, 下午8:35
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package olts.ui;
import java.awt.GridLayout;
import javax.swing.*;
import olts.application.*;
import java.awt.event.*;

/**
 * 这个类用于实现添加试题时候的出现的信息对话框
 * @author ZZ
 */
public class AddItemDialog implements ActionListener {
    
    protected MainFrame mainFrame;
    protected Item i;
    protected ItemFactory itemMaker = new OltsItemFactory();
    protected JLabel labelForId = new JLabel("试题ID:");
    protected JLabel labelForDiff = new JLabel("试题难度");
    protected JLabel labelForCon = new JLabel("试题内容");
    protected JLabel labelForAns = new JLabel("试题答案");
    protected JLabel labelForScore = new JLabel("试题分值");
    protected JLabel labelForTime = new JLabel("试题时间限制");
    protected JLabel labelForType = new JLabel("试题类型");
    protected JTextField textFieldForId = new JTextField(10);
    protected JTextField textFieldForDiff = new JTextField(10);
    protected JTextField textFieldForCon = new JTextField(10);
    protected JTextField textFieldForAns = new JTextField(10);
    protected JTextField textFieldForScore = new JTextField(10);
    protected JTextField textFieldForTime = new JTextField(10);
    protected JComboBox comboBoxForType = new JComboBox();
    protected JButton enterButton = new JButton("确定");
    protected JButton cancelButton = new JButton("取消");
    protected JPanel idArea = new JPanel(new GridLayout(1,2));
    protected JPanel diffArea = new JPanel(new GridLayout(1,2));
    protected JPanel conArea = new JPanel(new GridLayout(1,2));
    protected JPanel ansArea = new JPanel(new GridLayout(1,2));
    protected JPanel scoreArea = new JPanel(new GridLayout(1,2));
    protected JPanel timeArea = new JPanel(new GridLayout(1,2));
    protected JPanel typeArea = new JPanel(new GridLayout(1,2));
    protected JPanel buttonArea = new JPanel(new GridLayout(1,2));
    protected JDialog dialog;
    protected String numFormat = "\\d+";
    protected String trueFalseFormat = "true|false";
    protected String singleChoiceFormat ="A|B|C|D";
    
    /**
     * Creates a new instance of AddItemDialog
     */
    public AddItemDialog(MainFrame mf) {
        this.init(mf);
    }
    
    public AddItemDialog(MainFrame mf,Item it){
        this.i = it;
        this.init(mf);
        
    }
    
    /**初始化整个面板*/
    private void init(MainFrame mf){
        
        this.forUpdate();
        
        this.mainFrame = mf;
        this.enterButton.setActionCommand("确定");
        this.cancelButton.setActionCommand("取消");
        this.enterButton.addActionListener(this);
        this.cancelButton.addActionListener(this);
        
        this.idArea.add(this.labelForId);
        this.idArea.add(this.textFieldForId);
        this.diffArea.add(this.labelForDiff);
        this.diffArea.add(this.textFieldForDiff);
        this.conArea.add(this.labelForCon);
        this.conArea.add(this.textFieldForCon);
        this.ansArea.add(this.labelForAns);
        this.ansArea.add(this.textFieldForAns);
        this.scoreArea.add(this.labelForScore);
        this.scoreArea.add(this.textFieldForScore);
        this.timeArea.add(this.labelForTime);
        this.timeArea.add(this.textFieldForTime);
        this.typeArea.add(this.labelForType);
        this.typeArea.add(this.comboBoxForType);
        this.buttonArea.add(this.enterButton);
        this.buttonArea.add(this.cancelButton);
        //设置对话框上的面板
        this.dialog = new JDialog(mf,true);
        this.dialog.getContentPane().setLayout(new GridLayout(8,1));
        this.dialog.getContentPane().add(this.idArea);
        this.dialog.getContentPane().add(this.diffArea);
        this.dialog.getContentPane().add(this.conArea);
        this.dialog.getContentPane().add(this.ansArea);
        this.dialog.getContentPane().add(this.scoreArea);
        this.dialog.getContentPane().add(this.timeArea);
        this.dialog.getContentPane().add(this.typeArea);
        this.dialog.getContentPane().add(this.buttonArea);
        
        Object[] types = this.itemMaker.getItemTypes();
        if (types != null){
            for (int i = 0; i < types.length; i++){
                this.comboBoxForType.addItem(types[i]);
            }
        }
        this.show();
        dialog.setLocation(500,400);
        dialog.pack();
        dialog.setVisible(true);
        
    }
    
    /**用于实现对对话框按下确定后的监听*/
    public void actionPerformed(ActionEvent e){
        String cmd = e.getActionCommand();
        if (cmd.equals("确定")){
            
            //按下确定之后
            if (this.checkText() == true){
                
                int id = Integer.parseInt(this.textFieldForId.getText());
                int diff = Integer.parseInt(this.textFieldForDiff.getText());
                String com = this.textFieldForCon.getText();
                String ans = this.textFieldForAns.getText();
                int score = Integer.parseInt(this.textFieldForScore.getText());
                int time = Integer.parseInt(this.textFieldForTime.getText());
                String type = (String)this.comboBoxForType.getSelectedItem();
            
                try {
                    Item item = this.itemMaker.makeConcreteItem(
                            id,diff,time,score,com,ans,type);
                    this.mainFrame.setNewItem(item);//保存新值
                }catch(Exception exc){
                    this.mainFrame.setNewItem(null);
                }finally{
                    this.dialog.setVisible(false);
                }
               
            
            }
            
        }else if (cmd.equals("取消")) {
            this.mainFrame.setNewItem(null);
            this.dialog.setVisible(false);
        }
    }
    
    /**为Update对话框留下的接口,Update对话框应该实现这个方法,用于显示默认的值*/
    protected void forUpdate(){
        
    }
    
    /**
     *检查文本字段是否合乎要求
     */
    private boolean checkText(){
        
        if (this.textFieldForId.getText().matches(this.numFormat) == false ){
            JOptionPane.showMessageDialog(this.dialog,"ID字段必须输入一个整型数字");
            return false;
        }else if (this.textFieldForDiff.getText().matches(this.numFormat) == false){
            JOptionPane.showMessageDialog(this.dialog,"难度ID字段必须输入一个整型数字");
            return false;
        }else if (this.textFieldForScore.getText().matches(this.numFormat) ==false ){
            JOptionPane.showMessageDialog(this.dialog,"分值字段ID字段必须输入一个整型数字");
            return false;
        }else if (this.textFieldForTime.getText().matches(this.numFormat) == false ){
            JOptionPane.showMessageDialog(this.dialog,"时间限制字段必须输入一个整型数字");
            return false;
        }else if (this.textFieldForCon.getText().length() == 0){
            JOptionPane.showMessageDialog(this.dialog,"内容字段输入有误");
            return false;
        }else if (this.textFieldForAns.getText().length() == 0 ){
            JOptionPane.showMessageDialog(this.dialog,"答案字段输入有误");
            return false;
        }else {
            String type = (String) this.comboBoxForType.getSelectedItem();
            if (type.equals("TrueFalseItem") == true){
                if (this.textFieldForAns.getText().matches(this.trueFalseFormat) == false){
                    JOptionPane.showMessageDialog(this.dialog,"判断题的答案只能是true或者false");
                    return false;
                }
            }else if (type.equals("SingleChoiceItem") == true){
                if (this.textFieldForAns.getText().matches(this.singleChoiceFormat) == false){
                    JOptionPane.showMessageDialog(this.dialog,"单选题的答案只能是A,B,C或者D");
                    return false;
                }
            }
        }
        
        return true;
    }
    
    /**将对话框上的组件设置为可视的*/
    private void show(){
        labelForId.setVisible(true);
        labelForDiff.setVisible(true);
        labelForCon.setVisible(true);
        labelForAns.setVisible(true);
        labelForScore.setVisible(true);
        labelForTime.setVisible(true);
        labelForType.setVisible(true);
        textFieldForId.setVisible(true);
        textFieldForDiff.setVisible(true);
        textFieldForCon.setVisible(true);
        textFieldForAns.setVisible(true);
         textFieldForScore.setVisible(true);
         textFieldForTime.setVisible(true);
        comboBoxForType.setVisible(true);
        this.enterButton.setVisible(true);
        this.cancelButton.setVisible(true);
        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -