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

📄 truefalseitemdisplay.java

📁 支持GUI和持久对象的联机测试系统
💻 JAVA
字号:
/*
 * TrueFalseItemDisplay.java
 * 
 * MSE06B班张智力的实验报告
 * 
 * 2006年12月10日
 */

package olts.display;

import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import olts.AnswerSheet;

/**
 * TrueFalseItemDisplay类,继承了JPanel,用于生成显示判断题的JPanel
 */
public class TrueFalseItemDisplay extends JPanel {

	/**
	 * jLabel对象,显示试题的题目
	 */
    private JLabel jLabel = null;
    
    /**
     * JRadioButton对象,显示true选项
     */
    private JRadioButton jRadioButton = null;
    
    /**
     * JRadioButton1对象,显示false选项
     */
    private JRadioButton jRadioButton1 = null;
    
    /**
     * answerSheet对象,该Display的数据模型
     */
    private AnswerSheet answerSheet;
    
    /**
     * parent,记录本JPanel所在的frame父容器,方便回调父容器中的响应代码。
     */
    private MainFrame parent;
    
    /**
     * TrueFalseItemDisplay类的构造函数,
	 * @param answerSheet Display类的数据模型
	 * @param mf Display类的frame父容器
     */
    public TrueFalseItemDisplay(AnswerSheet answerSheet, MainFrame mf) {
        super();
        this.answerSheet = answerSheet;
        this.parent = mf;
        initialize();
    }

    /**
     * initializes方法,构造函数调用该方法初始化UI组件
     */
    private void initialize() {
        jLabel = new JLabel();
        jLabel.setText(this.answerSheet.getTestId() + "." + this.answerSheet.getItem().content + "(" + this.answerSheet.getItem().score + "分)");
        GridLayout gridLayout = new GridLayout();
        gridLayout.setRows(3);
        gridLayout.setHgap(1);
        gridLayout.setVgap(1);
        gridLayout.setColumns(1);
        this.setLayout(gridLayout);
        this.setSize(400, 120);
        this.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.LOWERED));
        ButtonGroup group = new ButtonGroup();
        group.add(getJRadioButton());
        group.add(getJRadioButton1());
        this.add(jLabel, null);
        this.add(getJRadioButton(), null);
        this.add(getJRadioButton1(), null);
    }

    
    /**
	 * getJRadioButton方法,返回一个JRadioButton对象,并在其中注册了listener,一但用户输入数据引发事件,
	 * 用户输入的结果将被保存到answerSheet中,以便以后批改用 
     * @return javax.swing.JRadioButton 
     */
    private JRadioButton getJRadioButton() {
        if (jRadioButton == null) {
            jRadioButton = new JRadioButton();
            jRadioButton.setText("true");
            jRadioButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    answerSheet.setAnswer("true");                    
                    answerResponse(answerSheet.getTestId(), "true");
                }
            });
        }
        return jRadioButton;
    }

    /**
	 * getJRadioButton1方法,返回一个JRadioButton1对象,并在其中注册了listener,一但用户输入数据引发事件,
	 * 用户输入的结果将被保存到answerSheet中,以便以后批改用 
     * @return javax.swing.JRadioButton 
     */
    private JRadioButton getJRadioButton1() {
        if (jRadioButton1 == null) {
            jRadioButton1 = new JRadioButton();
            jRadioButton1.setText("false");
            jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    answerSheet.setAnswer("false");
                    answerResponse(answerSheet.getTestId(), "false");
                }
            });
        }
        return jRadioButton1;
    }

    /**
	 * 调用frame父容器上的响应方法,修改父容器上的某些组件的值和外观
	 * @param i 试题号
	 * @param s 用户答案
	 */
    private void answerResponse(int i, String s){
        parent.answerResponse(i, s);
    }

  
}

⌨️ 快捷键说明

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