📄 kaoshi.java
字号:
package gui01;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class KaoShi implements ActionListener{ private JFrame frame; private Container contentPane; private JButton btnExit; private JButton btnStart; private JButton btnOk; private JButton btnAbdant; private JLabel lblTime; private JLabel lblstr1; private JLabel lblfh; private JLabel lblstr2; private JLabel lblequal; private JTextField txtjg; /** * */ private JLabel lblok; private JLabel lblerr; private JLabel lblabdant; private int time=1800; private JButton btnOkp1; private JButton btnCancelp1; private int countOk=0; private int countErr=0; private int countSkip=0; private JButton btnOkp2; private boolean hasp1=false; private Manage manage; private Timer t; public void actionPerformed(ActionEvent e){ if (e.getSource()== t){ time--; lblTime.setText(time/60 +":" + time%60); if (time<=0){ countSkip = 25 - countOk - countErr; selectPanel(2); } } if (e.getSource() == btnStart){ t.start(); addQuestion(); lblequal.setVisible(true); txtjg.setVisible(true); btnStart.setEnabled(false); btnOk.setEnabled(true); btnAbdant.setEnabled(true); } if (e.getSource() ==btnOk || e.getSource()==txtjg){ int jg; try{ jg = Integer.parseInt(txtjg.getText().trim()); }catch(Exception e1){ txtjg.setText(""); txtjg.grabFocus(); return; } int num1 = Integer.parseInt(lblstr1.getText().trim()); int num2 = Integer.parseInt(lblstr2.getText().trim()); String fh = lblfh.getText().trim(); int jgsum=0; if (fh=="+") jgsum = num1 + num2; else if (fh == "-") jgsum = num1 - num2; else if (fh == "*") jgsum = num1 * num2; else if (fh == "/") jgsum = num1 / num2; if (jgsum == jg) countOk++; else countErr++; if ((countOk+countErr+countSkip)>=25) selectPanel(2); else setLabel(); addQuestion(); } if (e.getSource() == btnAbdant){ countSkip++; if ((countOk+countErr+countSkip)>=25) selectPanel(2); else setLabel(); addQuestion(); } if (e.getSource() == btnExit){ if (hasp1){ CardLayout c = (CardLayout)contentPane.getLayout(); c.show(contentPane,"p1"); } else{ hasp1=true; selectPanel(1); } } if (e.getSource() == btnOkp1) System.exit(0); if(e.getSource() == btnCancelp1){ CardLayout c = (CardLayout)contentPane.getLayout(); c.show(contentPane,"p0"); } if(e.getSource() == btnOkp2) System.exit(0); } public void setLabel(){ lblok.setText("答对了"+countOk); lblerr.setText("答错了"+countErr); lblabdant.setText("放弃了"+countSkip); } public void addQuestion(){ String[] fh={"+","-","*","/"}; int intfh = (int)(Math.random()*4); String strfh =fh[intfh]; lblfh.setText(strfh); int num1 =(int)(Math.random()*100)+1; int num2 =(int)(Math.random()*100)+1; if (intfh==3) num1=num1*num2; lblstr1.setText(num1+""); lblstr2.setText(num2+""); txtjg.setText(""); } public KaoShi(){ manage = Login.m; frame = new JFrame("kaoshi"); frame.setBounds(350,250,330,170); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); contentPane = frame.getContentPane(); initGUI(); } public void initGUI(){ contentPane.setLayout(new CardLayout()); selectPanel(0); } public void selectPanel(int id){ CardLayout c = (CardLayout)contentPane.getLayout(); contentPane.add("p"+id,getPanel(id)); c.next(contentPane); //c.addLayoutComponent(getPanel(id), "p"+id); //c.show(contentPane, "p"+id); //System.out.println(contentPane.countComponents()); } public JPanel getPanel(int id){ JPanel pAll = null; JPanel p,p1,p2,p3,p4; switch (id) { case 0: pAll = new JPanel(new BorderLayout()); p1 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); btnStart = new JButton("start"); btnOk = new JButton("ok"); btnOk.setEnabled(false); btnAbdant = new JButton("Abdant"); btnAbdant.setEnabled(false); btnExit = new JButton("Exit"); p1.add(btnStart); p1.add(btnOk); p1.add(btnAbdant); p1.add(btnExit); pAll.add(p1,BorderLayout.SOUTH); p2 = new JPanel(new GridLayout(3,1,0,0)); p3 = new JPanel(new FlowLayout(FlowLayout.CENTER)); lblstr1 = new JLabel(); lblfh = new JLabel(); lblstr2 = new JLabel(); lblequal = new JLabel("="); lblequal.setVisible(false); txtjg = new JTextField(10); txtjg.setVisible(false); p3.add(lblstr1); p3.add(lblfh); p3.add(lblstr2); p3.add(lblequal); p3.add(txtjg); lblTime = new JLabel(time/60 +":" + time%60); p3.add(lblTime); pAll.add(p3,BorderLayout.NORTH); lblok = new JLabel("答对了"); lblerr = new JLabel("答错了"); lblabdant = new JLabel("放弃了"); p2.add(lblok); p2.add(lblerr); p2.add(lblabdant); pAll.add(p2,BorderLayout.CENTER); btnStart.addActionListener(this); btnOk.addActionListener(this); btnAbdant.addActionListener(this); btnExit.addActionListener(this); txtjg.addActionListener(this); t = new Timer(1000,this); break; case 1: pAll = new JPanel(new BorderLayout()); p1 = new JPanel(new FlowLayout()); p1.add(new JLabel("Are you sure to exit? Your cent will be Zero")); pAll.add(p1,BorderLayout.CENTER); btnOkp1 = new JButton("ok"); btnCancelp1 = new JButton("Cancel"); p2 = new JPanel(new FlowLayout()); p2.add(btnOkp1); p2.add(btnCancelp1); pAll.add(p2,BorderLayout.SOUTH); btnOkp1.addActionListener(this); btnCancelp1.addActionListener(this); break; case 2: pAll = new JPanel(new GridLayout(4,1)); p1 = new JPanel(new FlowLayout()); p1.add(new JLabel("答对"+countOk+"题,获得"+countOk*4+"分")); p2 = new JPanel(new FlowLayout()); p2.add(new JLabel("答错"+countErr+"题")); p3 = new JPanel(new FlowLayout()); p3.add(new JLabel("放弃"+countSkip+"题")); pAll.add(p1); pAll.add(p2); pAll.add(p3); btnOkp2 = new JButton("ok"); p4 = new JPanel(new FlowLayout()); p4.add(btnOkp2); pAll.add(p4); btnOkp2.addActionListener(this); } return pAll; } public void go(){ frame.setVisible(true); } public static void main(String[] args){ (new KaoShi()).go(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -