📄 mainpanel.java
字号:
/* * MainPanel.java * * Created on 2005年6月28日, 上午11:47 * * @author Liu Yi ( http://www.liu-yi.net ) */package wordwinner;import java.io.*;import java.awt.*;import javax.swing.*;import java.util.*;public class MainPanel extends javax.swing.JPanel { /** MainPanel构造函数 */ public MainPanel() { initComponents(); startButton.setEnabled(false); keyRB[0]=keyA; keyRB[1]=keyB; keyRB[2]=keyC; keyRB[3]=keyD; optionsPanel.setVisible(false); } /** 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() {//GEN-BEGIN: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 = 107; gridBagConstraints.ipady = 18; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.insets = new java.awt.Insets(40, 23, 0, 0); add(optionsPanel, 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 = 58; 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); }//GEN-END:initComponents private void keyChosen(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keyChosen //选择答案 if (keyRB[keyPosition].isSelected()){ score++; trace.append("\n \u221A"); }else{ trace.append("\n \u00D7"); } trace.append(" 答案:"+keyRB[keyPosition].getText()); test() ; }//GEN-LAST:event_keyChosen private void setupButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setupButtonActionPerformed // 设置 setup(); }//GEN-LAST:event_setupButtonActionPerformed private void testWord(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testWord //测试 test(); }//GEN-LAST:event_testWord //读入并将词库文件转换为中英文单词列表 public void loadWords(String wordfileName) { try { Reader dataReader=new FileReader(wordfileName); 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); } } public void setup(){ if (setupDialog==null) { if (total==0) open(); setupDialog=new SetUpDialog2(null); } setupDialog.setCount( groupCount ); setupDialog.setVisible(true); groupIndex = setupDialog.getIndex(); mode=setupDialog.getMode(); option=setupDialog.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(){ 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); startButton.setEnabled(false); optionsPanel.setVisible(false); }; } // 变量声明 - 不进行修改//GEN-BEGIN:variables 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; // 变量声明结束//GEN-END:variables 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 SetUpDialog2 setupDialog; private ListIterator it1,it2; private String key[]={"A","B","C","D"}; private JRadioButton keyRB[]=new JRadioButton[4]; private static final int GROUPSIZE=50; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -