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

📄 mainpanel.java

📁 Java程序设计大学教程程序源代码
💻 JAVA
字号:
/* * MainPanel.java * Java应用程序转化为applet的演示 * Created on 2005年7月15日, 下午3:12 */package jbookch8;import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;import java.net.*;public class MainPanel extends javax.swing.JPanel {    /** MainPanel构造函数 */    public MainPanel(URL url) {        try {            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");        } catch (Exception e){            e.printStackTrace();        }        fileURL=url;        initComponents();        //startButton.setEnabled(false);        keyRB[0]=keyA;        keyRB[1]=keyB;        keyRB[2]=keyC;        keyRB[3]=keyD;        optionsPanel.setVisible(false);        setup.setVisible(true);        open();        setup.setCount( groupCount );    }        /** This method is called from within the constructor to     * initialize the form.     * WARNING: Do NOT modify this code. The content of this method is     * always regenerated by the Form Editor.     */    private void initComponents() {        java.awt.GridBagConstraints gridBagConstraints;                optionsPanel = new javax.swing.JPanel();        keyA = new javax.swing.JRadioButton();        keyB = new javax.swing.JRadioButton();        keyC = new javax.swing.JRadioButton();        keyD = new javax.swing.JRadioButton();        buttonPanel = new javax.swing.JPanel();        startButton = new javax.swing.JButton();        //setupButton = new javax.swing.JButton();        testWord = new javax.swing.JLabel();        jScrollPane1 = new javax.swing.JScrollPane();        trace = new javax.swing.JTextArea();                setLayout(new java.awt.GridBagLayout());                optionsPanel.setLayout(new javax.swing.BoxLayout(optionsPanel, javax.swing.BoxLayout.Y_AXIS));                keyA.setLabel("A");        keyA.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                keyChosen(evt);            }        });                optionsPanel.add(keyA);                keyB.setLabel("B");        keyB.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                keyChosen(evt);            }        });                optionsPanel.add(keyB);                keyC.setLabel("C");        keyC.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                keyChosen(evt);            }        });                optionsPanel.add(keyC);                keyD.setLabel("D");        keyD.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                keyChosen(evt);            }        });                optionsPanel.add(keyD);                gridBagConstraints = new java.awt.GridBagConstraints();        gridBagConstraints.gridx = 0;        gridBagConstraints.gridy = 1;        gridBagConstraints.gridwidth = 2;        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;        gridBagConstraints.ipadx = 0;        gridBagConstraints.ipady =0;        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;        gridBagConstraints.insets = new java.awt.Insets(40, 23, 0, 0);        JPanel leftPanel = new javax.swing.JPanel();        leftPanel.add(optionsPanel);//        leftPanel.add(setup);//        add(leftPanel, gridBagConstraints);//                startButton.setText("\u5f00\u59cb");        startButton.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                testWord(evt);            }        });                buttonPanel.add(startButton);        /*        setupButton.setText("\u8bbe\u7f6e");        setupButton.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                setupButtonActionPerformed(evt);            }        });                 buttonPanel.add(setupButton);         */        gridBagConstraints = new java.awt.GridBagConstraints();        gridBagConstraints.gridx = 0;        gridBagConstraints.gridy = 2;        gridBagConstraints.gridwidth = 2;        gridBagConstraints.gridheight = 4;        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;        gridBagConstraints.ipadx = 144;        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;        gridBagConstraints.insets = new java.awt.Insets(40, 54, 19, 52);        add(buttonPanel, gridBagConstraints);                testWord.setBackground(java.awt.SystemColor.info);        testWord.setFont(new java.awt.Font("宋体", 0, 24));        testWord.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);        testWord.setText("Word Winner");        testWord.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));        testWord.setOpaque(true);        gridBagConstraints = new java.awt.GridBagConstraints();        gridBagConstraints.gridx = 0;        gridBagConstraints.gridy = 0;        gridBagConstraints.gridwidth = 2;        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;        gridBagConstraints.ipadx = 40;        gridBagConstraints.ipady = 10;        gridBagConstraints.insets = new java.awt.Insets(28, 16, 0, 0);        add(testWord, gridBagConstraints);                jScrollPane1.setViewportView(trace);                gridBagConstraints = new java.awt.GridBagConstraints();        gridBagConstraints.gridx = 3;        gridBagConstraints.gridy = 0;        gridBagConstraints.gridheight = 3;        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;        gridBagConstraints.ipadx = 173;        gridBagConstraints.ipady = 257;        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;        gridBagConstraints.weightx = 1.0;        gridBagConstraints.weighty = 1.0;        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);        add(jScrollPane1, gridBagConstraints);            }        private void keyChosen(java.awt.event.ActionEvent evt) {        //选择答案        if (keyRB[keyPosition].isSelected()){            score++;            trace.append("\n \u221A");        }else{            trace.append("\n \u00D7");        }        trace.append("  答案:"+keyRB[keyPosition].getText());        test() ;    }        private void setupButtonActionPerformed(java.awt.event.ActionEvent evt) {        // 设置        setup();    }        private void testWord(java.awt.event.ActionEvent evt) {        //测试        test();    }        //读入并将词库文件转换为中英文单词列表    public void loadWords(URL url) {        try {            //Reader dataReader=new FileReader(wordfileName);            Reader dataReader=new InputStreamReader(url.openStream());            BufferedReader inStream= new BufferedReader(dataReader); // 打开文件流            String line = inStream.readLine();            // 读文件中的一行            while (line != null) {                //将英中词汇对解析成英文、中文单词                //例如:"love=vt.爱" 解析成: "love"和"爱 "                int posColon=line.indexOf('=');                if (posColon>0) {                    String e=line.substring(0,posColon).trim();                    String c=line.substring(posColon+1).trim();                    wordsEnglish.add(e) ;                    wordsChinese.add(c) ;                }                line = inStream.readLine();               // 读下一行            }            inStream.close();                             // 关闭文件流        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        total=wordsEnglish.size();        groupCount=total/50;//50个单词分为一组    }        public void open(){        /*        int choice;        JFileChooser jfc = new JFileChooser();        jfc.setSize( 400,300 );        choice = jfc.showOpenDialog( this );        if ( choice == JFileChooser.APPROVE_OPTION ) {            String fileName = jfc.getSelectedFile().getAbsolutePath();            loadWords(fileName);        }         **/        loadWords(fileURL);            }        public void setup(){       if (!setup.confirmed()) return;                groupIndex = setup.getIndex();        mode=setup.getMode();        option=setup.getOption();        makeTestList();        startButton.setText("开始");        startButton.setEnabled(true);        trace.setText("开始...");    }        private void makeTestList(){        java.util.List chosenList1=wordsEnglish;        java.util.List chosenList2=wordsChinese;        testList.clear();        keyList.clear();        if (option==1)  {            chosenList1=wordsChinese;            chosenList2=wordsEnglish;        }        if (mode==0){            //产生随机测试单词            for (int i=0;i<50;i++){                int r=(int)(Math.random()*total);                testList.add(chosenList1.get(r));                keyList.add(chosenList2.get(r));            }        } else{            //产生顺序测试单词            for (int i=0;i<50;i++){                int s=i+groupIndex*50;                if ( s>(total-1) ) return;                testList.add(chosenList1.get(s));                keyList.add(chosenList2.get(s));            }        };        it1=testList.listIterator();        it2=keyList.listIterator();        score=0;//计分归0    }        private void makeKeys(){        String keyToWord=(String)(it2.next());        //随机产生假答案        for (int i=0;i<4;i++){            int r=(int)(Math.random()*total);            if (option==0){                key[i]=(String)(wordsChinese.get(r));            } else {                key[i]=(String)(wordsEnglish.get(r));            }            //如果随机产生的假答案刚好与真答案相同,则重来。            if (key[i]==keyToWord)                i--;        }        //随机产生真答案的位置        keyPosition=(int)(Math.random()*4);        key[keyPosition]=keyToWord;        //显示答案选择项        keyA.setText("A.  "+key[0]);        keyB.setText("B.  "+key[1]);        keyC.setText("C.  "+key[2]);        keyD.setText("D.  "+key[3]);    };        public void test(){        if (!optionsPanel.isVisible()) setup();        if (setup.isVisible())  return;                optionsPanel.setVisible(true);        if (it1.hasNext()){            makeKeys();            String curWord=(String)(it1.next());            testWord.setText(curWord);            startButton.setText("下一个");            String str="\n  "+it1.nextIndex()+"  "+curWord;            trace.append(str);            for (int i=0;i<4;i++){                keyRB[i].setSelected(false);            }                    } else {            startButton.setText("开始");            String sum=" 本次成绩: "+(score*2)+"分";            testWord.setText(sum);            trace.append("\n"+sum);            optionsPanel.setVisible(false);            setup.setVisible(true);                    };            }        // 变量声明 - 不进行修改    private javax.swing.JPanel buttonPanel;    private javax.swing.JScrollPane jScrollPane1;    private javax.swing.JRadioButton keyA;    private javax.swing.JRadioButton keyB;    private javax.swing.JRadioButton keyC;    private javax.swing.JRadioButton keyD;    private javax.swing.JPanel optionsPanel;    private javax.swing.JButton setupButton;    private javax.swing.JButton startButton;    private javax.swing.JLabel testWord;    private javax.swing.JTextArea trace;    // 变量声明结束        private java.util.List wordsEnglish=new ArrayList(),            wordsChinese=new ArrayList(),            testList=new ArrayList(),            keyList=new ArrayList();    private  int total,            groupCount,            groupIndex,            mode,            option,            keyPosition,            score;    private SetUp setup=new SetUp();//    private ListIterator it1,it2;    private String key[]={"A","B","C","D"};    private JRadioButton keyRB[]=new JRadioButton[4];    private static final int GROUPSIZE=50;    public URL fileURL;    }

⌨️ 快捷键说明

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