新学
1
JCheckBox chckbxA = new JCheckBox("a");
JCheckBox的监听器接口ItemListener,响应方法itemStateChanged
boolean isSelect=a.isSelected();是否被选
a.setSelected(false);可以控制 是否选择
2
Timer类stop了,好像不用restart,直接start就可以了
package thread; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; import javax.swing.border.EmptyBorder; import javax.swing.JTextArea; public class MyWin extends JFrame implements ActionListener,ItemListener { private JPanel contentPane; JButton xuantimu = new JButton("u9009u9898u76EE"); JFileChooser file1=new JFileChooser(); File dir; int MaxTime=8; JLabel second = new JLabel("剩8秒 问题:"); JTextArea text = new JTextArea(); JLabel label2 = new JLabel("分数:"); JButton button = new JButton("再做一遍"); Timer timer1=new Timer(1000,this); String daan=null; FileReader fileReader; BufferedReader in1; int fenshu=0; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MyWin frame = new MyWin(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public MyWin() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 719, 145); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JPanel panel = new JPanel(); contentPane.add(panel, BorderLayout.CENTER); panel.add(xuantimu); panel.add(second); text.setColumns(17); text.setRows(3); panel.add(text); JCheckBox chckbxA = new JCheckBox("a"); panel.add(chckbxA); JCheckBox chckbxB = new JCheckBox("b"); panel.add(chckbxB); JCheckBox chckbxC = new JCheckBox("c"); panel.add(chckbxC); JCheckBox chckbxD = new JCheckBox("d"); panel.add(chckbxD); panel.add(label2); panel.add(button); chckbxA.addItemListener(this); chckbxB.addItemListener(this); chckbxC.addItemListener(this); chckbxD.addItemListener(this); button.addActionListener(this); xuantimu.addActionListener(this); } void readNext(){ try{ text.setText(null); String s=null; for(;(s=in1.readLine())!=null;){ if(!s.startsWith("-")){ text.append(s+" "); } else{ s=s.replaceAll("-",""); daan=s; break; } } timer1.start(); } catch(Exception e){ System.out.println("2222"); System.out.println(e.getMessage()); } } void setFile(){ int state=file1.showOpenDialog(this); if(state==JFileChooser.APPROVE_OPTION){ try{ dir=file1.getSelectedFile(); fileReader=new FileReader(dir); in1=new BufferedReader(fileReader); readNext(); } catch(Exception e1){ System.out.println("11111"); System.out.println(e1.getMessage()); } } } @Override public void itemStateChanged(ItemEvent e) { // TODO 自动生成的方法存根 JCheckBox a=(JCheckBox)e.getItemSelectable(); String s=a.getText(); // System.out.println(s); // System.out.println(daan); boolean isSelect=a.isSelected(); if(s.equals(daan.trim())&&isSelect){ fenshu++; label2.setText("分数"+fenshu); timer1.stop(); MaxTime=8; readNext(); a.setSelected(false); } } @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 if(e.getSource()==timer1){ second.setText("剩"+MaxTime+"秒"+",问题"); MaxTime--; if(MaxTime<=0){ MaxTime=8; readNext(); } } else if(e.getSource()==button){ fenshu=0; setFile(); } else if(e.getActionCommand()=="选题目"){ setFile(); } } }