Java + eclipse + awt 编写锻炼打字小软件(未完成)
进入前界面:
import java.awt.*; public class Welcome extends JFrame implements Runnable{ Thread t; private LoginGraphics loginGraphics_IL; public static void main(String[] args) { Welcome inst = new Welcome(); inst.setVisible(true); } public Welcome() { super(); t=new Thread(this); initGUI(); } private void initGUI() { try { setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); setUndecorated(true); //计算屏幕大小 Dimension screen=Toolkit.getDefaultToolkit().getScreenSize(); //添加图形 setSize(443, 155); //将窗口居中 setLocation((screen.width-getWidth())/2,(screen.height-getHeight())/2); { loginGraphics_IL = new LoginGraphics(); add(loginGraphics_IL); loginGraphics_IL.setBorder(BorderFactory.createCompoundBorder( null, null)); loginGraphics_IL.setBackground(new java.awt.Color(192,192,192)); } t.start(); } catch (Exception e) { e.printStackTrace(); } } public void run(){ try{ Thread.sleep(3000); new MainFrame(); this.setVisible(false); t.interrupt(); }catch(InterruptedException e){} } }
主界面:
import java.awt.*; public class MainFrame extends JDesktopPane implements ActionListener{ Dimension screen; Toolkit toolkit=Toolkit.getDefaultToolkit(); private Icon ico; JFrame frame; Tool tool; public MainFrame(){ JFrame.setDefaultLookAndFeelDecorated(true); frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container container = frame.getContentPane(); container.add(this, BorderLayout.CENTER); this.setPreferredSize(new java.awt.Dimension(800, 550)); frame.setTitle("TypingMaster"); frame.setResizable(false); screen=toolkit.getScreenSize(); frame.setLocation((screen.width-800)/2,screen.height/2-600/2); //添加工具条. tool=new Tool(this); this.add(tool,1); frame.pack(); frame.setVisible(true); } // 实现ActionListener接口的方法 public void actionPerformed(ActionEvent e){ } private void showFrame(){ Hint hint =new Hint(tool); this.add(hint,2); } }
tool(左侧栏)
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; /** * 左边的工具栏 */ public class Tool extends JInternalFrame implements ActionListener{ MainFrame parent; private JButton btnJP; private JButton btnExit; private JPanel p; private ImageIcon image; public Tool(MainFrame parent){ this.parent=parent; //按下键盘练习 p=new JPanel(); btnJP=new JButton("\u952E\u76D8\u7EC3\u4E60",image); btnJP.setFont(new Font("x",Font.PLAIN,12)); btnJP.setMnemonic('1'); btnJP.addActionListener(this); //按下退出系统 btnExit=new JButton("\u9000\u51FA\u7CFB\u7EDF",image); btnExit.setFont(new Font("x",Font.PLAIN,12)); btnExit.setMnemonic('Q'); btnExit.addActionListener(this); p.setLayout(new FlowLayout()); p.add(btnJP); p.add(btnExit); getContentPane().add(this.p); setLayer(1); setBounds(1,0,140,121); setPreferredSize(new Dimension(140, 550)); ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null); this.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE); pack(); setVisible(true); } //实现ActionListener接口的方法 public void actionPerformed(ActionEvent e){ if(PublicData.onChoice==true){ if(e.getSource()==btnJP){ PublicData.onChoice=false; PublicData.Lang="English"; PublicData.Text="Text/English_"+(int)(Math.random()*5+1)+".txt"; PublicData.choice=1; showFrame(); } if(e.getSource()==btnExit){ int i=JOptionPane.showConfirmDialog(this.parent,"你真的要退出吗?","疯狂打字", JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE); if(i==0){ System.exit(0); } } } } private void showFrame(){ Hint hint =new Hint(this); parent.add(hint,2); } }
LoginGraphics:
import java.awt.*; public class LoginGraphics extends JPanel{ public LoginGraphics() { } public void paint(Graphics g){ paintComponent(g); g.setFont(new Font("Dialog",Font.BOLD+Font.ITALIC,45)); for(int i=1;i<=5;i++){ g.setColor(new Color(255-(i*50),255-(i*50),255-(i*50))); g.drawLine(0,36+i,293,36+i); } g.setColor(new Color(255,255,255)); g.drawString("Typing Master",62,85); g.setColor(new Color(34,80,23)); g.drawString("Typing Master",60,85); for(int i=1;i<=5;i++){ g.setColor(new Color(255-(i*50),255-(i*50),255-(i*50))); g.drawLine(60,100-i,443,100-i); } } }
编辑键区的字符与键盘对照:
import javax.swing.*; /** * 编辑键区的字符与键盘对照 */ public class Keyset extends JPanel{ public Keyset() { } public void paintComponent(Graphics g){ super.paintComponent(g); String str=new String(PublicData.key); int keyTop=0;//字符在键盘上的纵坐标 int keyLeft=0;//字符在键盘上的横坐标 boolean leftShift=false;//是否是左边的Shift boolean rightShift=false;//是否是右边的Shift boolean yShift=false;//是否要用Shift boolean ySpace=false;//是否是空格 String shift=new String("~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?"); String noShift=new String("`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./"); int LEFT=52; int TOP=20; int lTemp=LEFT; int tTemp=TOP; if(shift.lastIndexOf(str)<=46){//计算是不是要用上Shift if((shift.lastIndexOf(str)>=0 && shift.lastIndexOf(str)<=5) || (shift.lastIndexOf(str)>=13 && shift.lastIndexOf(str)<=17) || (shift.lastIndexOf(str)>=26 && shift.lastIndexOf(str)<=30) || (shift.lastIndexOf(str)>=37 && shift.lastIndexOf(str)<=41)){//计算字符在不在左手区 yShift=true; rightShift=true; if(shift.lastIndexOf(str)>=0 && shift.lastIndexOf(str)<=5){//计算字符在该区的位置 keyTop=1; for(int i=0;i<=5;i++){ if(str.equals(shift.substring(i, i+1))){ keyLeft=i; System.out.println(shift.substring(i, i+1)); } } } if(shift.lastIndexOf(str)>=13 && shift.lastIndexOf(str)<=17){//计算字符在该区的位置 keyTop=2; for(int i=13;i<=17;i++){ if(str.equals(shift.substring(i, i+1))){ keyLeft=i; } } } if(shift.lastIndexOf(str)>=26 && shift.lastIndexOf(str)<=30){//计算字符在该区的位置 keyTop=3; for(int i=26;i<=30;i++){ if(str.equals(shift.substring(i, i+1))){ keyLeft=i; } } } if(shift.lastIndexOf(str)>=37 && shift.lastIndexOf(str)<=41){//计算字符在该区的位置 keyTop=4; for(int i=37;i<=41;i++){ if(str.equals(shift.substring(i, i+1))){ keyLeft=i; } } } } if((shift.lastIndexOf(str)>=6 && shift.lastIndexOf(str)<=12) || (shift.lastIndexOf(str)>=18 && shift.lastIndexOf(str)<=25) || (shift.lastIndexOf(str)>=31 && shift.lastIndexOf(str)<=36) || (shift.lastIndexOf(str)>=42 && shift.lastIndexOf(str)<=46)){//计算字符在不在右手区 yShift=true; leftShift=true; if(shift.lastIndexOf(str)>=6 && shift.lastIndexOf(str)<=12){ keyTop=1; for(int i=6;i<=12;i++){ if(str.equals(shift.substring(i, i+1))){ keyLeft=i; } } } if(shift.lastIndexOf(str)>=18 && shift.lastIndexOf(str)<=25){ keyTop=2; for(int i=18;i<=25;i++){ if(str.equals(shift.substring(i, i+1))){ keyLeft=i; } } } if(shift.lastIndexOf(str)>=31 && shift.lastIndexOf(str)<=36){ keyTop=3; for(int i=31;i<=36;i++){ if(str.equals(shift.substring(i, i+1))){ keyLeft=i; } } } if(shift.lastIndexOf(str)>=42 && shift.lastIndexOf(str)<=46){ keyTop=4; for(int i=42;i<=46;i++){ if(str.equals(shift.substring(i, i+1))){ keyLeft=i; } } } } } if(noShift.lastIndexOf(str)<=46){ if(noShift.lastIndexOf(str)>=0 && noShift.lastIndexOf(str)<=12){ keyTop=1; for(int i=0;i<=12;i++){ if(str.equals(noShift.substring(i,i+1))){ keyLeft=i; } } } if(noShift.lastIndexOf(str)>=13 && noShift.lastIndexOf(str)<=25){ keyTop=2; for(int i=13;i<=25;i++){ if(str.equals(noShift.substring(i,i+1))){ keyLeft=i; } } } if(noShift.lastIndexOf(str)>=26 && noShift.lastIndexOf(str)<=36){ keyTop=3; for(int i=26;i<=36;i++){ if(str.equals(noShift.substring(i,i+1))){ keyLeft=i; } } } if(noShift.lastIndexOf(str)>=37 && noShift.lastIndexOf(str)<=46){ keyTop=4; for(int i=37;i<=46;i++){ if(str.equals(noShift.substring(i,i+1))){ keyLeft=i; } } } } if(str.equals(" ")){ ySpace=true; } //绘制编辑键区图形// g.setColor(Color.black); for(int i=0;i<=12;i++){ if(keyTop==1 && keyLeft==i){ g.setColor(Color.gray); g.fillRect(LEFT,TOP,35,35); } g.setColor(Color.black); g.draw3DRect(LEFT,TOP,35,35,true); LEFT=LEFT+37; } //←键 g.draw3DRect(LEFT,TOP,70,35,true); int sum=12; LEFT=lTemp; TOP=TOP+37; //Tab键 g.draw3DRect(LEFT,TOP,54,35,true); LEFT=LEFT+56; for(int i=1;i<=12;i++) { if(keyTop==2 && keyLeft==i+sum){ g.setColor(Color.gray); g.fillRect(LEFT,TOP,35,35); } g.setColor(Color.black); g.draw3DRect(LEFT,TOP,35,35,true); LEFT=LEFT+37; } //|\键 sum=sum+12; if(keyTop==2 && keyLeft==sum+1){ g.setColor(Color.gray); g.fillRect(LEFT,TOP,51,35); } g.setColor(Color.black); g.draw3DRect(LEFT,TOP,51,35,true); sum=sum+1; LEFT=lTemp; TOP=TOP+37; //Caps Lock键 g.draw3DRect(LEFT,TOP,65,35,true); LEFT=LEFT+67; for(int i=1;i<=11;i++){ if(keyTop==3 && keyLeft==sum+i){ g.setColor(Color.gray); g.fillRect(LEFT,TOP,35,35); } g.setColor(Color.black); g.draw3DRect(LEFT,TOP,35,35,true); LEFT=LEFT+37; } //Enter键 g.draw3DRect(LEFT,TOP,77,35,true); sum=sum+11; LEFT=lTemp; TOP=TOP+37; //左Shift键 if(leftShift==true){ g.setColor(Color.gray); g.fillRect(LEFT,TOP,82,35); } g.setColor(Color.black); g.draw3DRect(LEFT,TOP,82,35,true); LEFT=LEFT+84; for(int i=1;i<=10;i++){ if(keyTop==4 && keyLeft==sum+i){ g.setColor(Color.gray); g.fillRect(LEFT,TOP,35,35); } g.setColor(Color.black); g.draw3DRect(LEFT,TOP,35,35,true); LEFT=LEFT+37; } //右Shift键 if(rightShift==true){ g.setColor(Color.gray); g.fillRect(LEFT,TOP,97,35); } g.setColor(Color.black); g.draw3DRect(LEFT,TOP,97,35,true); //左Ctrl键 LEFT=lTemp; TOP=TOP+37; g.draw3DRect(LEFT,TOP,53,35,true); LEFT=LEFT+55; //win键 g.draw3DRect(LEFT,TOP,44,35,true); LEFT=LEFT+46; //左Alt键 g.draw3DRect(LEFT,TOP,44,35,true); LEFT=LEFT+46; //Space键 if(ySpace==true){ g.setColor(Color.gray); g.fillRect(LEFT,TOP,211,35); } g.setColor(Color.black); g.draw3DRect(LEFT,TOP,211,35,true); //右Alt键 LEFT=LEFT+213; g.draw3DRect(LEFT,TOP,44,35,true); LEFT=LEFT+46; //win键 g.draw3DRect(LEFT,TOP,46,35,true); LEFT=LEFT+48; //快捷键 g.draw3DRect(LEFT,TOP,42,35,true); LEFT=LEFT+44; //右Ctrl键 g.draw3DRect(LEFT,TOP,53,35,true); //编辑键区图形绘制完成 //绘制字母、符号------- LEFT=lTemp; TOP=tTemp; g.setColor(Color.lightGray); Font f=new Font("x",Font.BOLD,13); g.setFont(f); LEFT=LEFT+7; TOP=TOP+14; for(int i=1;i<=13;i++){ g.drawString(shift.substring(i-1,i),LEFT,TOP); g.drawString(noShift.substring(i-1,i),LEFT,TOP+18); LEFT=LEFT+37; } f=new Font("x",Font.BOLD,12); g.setFont(f); g.drawString("BackSpace",LEFT-4,TOP+8); LEFT=lTemp+7; TOP=TOP+37; g.drawString("Tab",LEFT,TOP+8); f=new Font("x",Font.BOLD,14); g.setFont(f); LEFT=lTemp+63; for(int i=13;i<=25;i++){ g.drawString(shift.substring(i,i+1),LEFT,TOP); if(i>=23) g.drawString(noShift.substring(i,i+1),LEFT,TOP+16); LEFT=LEFT+37; } f=new Font("x",Font.BOLD,12); g.setFont(f); LEFT=lTemp; TOP=TOP+37; g.drawString("CapsLock",LEFT+7,TOP+8); f=new Font("x",Font.BOLD,14); g.setFont(f); LEFT=lTemp+74; for(int i=26;i<=36;i++){ g.drawString(shift.substring(i,i+1),LEFT,TOP); if(i>=35) g.drawString(noShift.substring(i,i+1),LEFT,TOP+16); if("F".equals(shift.substring(i,i+1)) || "J".equals(shift.substring(i,i+1))) g.drawString("_",LEFT+5,TOP+15); LEFT=LEFT+37; } f=new Font("x",Font.BOLD,12); g.setFont(f); g.drawString("Enter←┘",LEFT+15,TOP+8); LEFT=lTemp+7; TOP=TOP+37; g.drawString("↑Shift",LEFT,TOP+8); f=new Font("x",Font.BOLD,14); g.setFont(f); LEFT=lTemp+91; for(int i=37;i<=46;i++){ g.drawString(shift.substring(i,i+1),LEFT,TOP); if(i>=44) g.drawString(noShift.substring(i,i+1),LEFT,TOP+16); LEFT=LEFT+37; } f=new Font("x",Font.BOLD,12); g.setFont(f); g.drawString("↑Shift",LEFT,TOP+8); LEFT=lTemp+7; TOP=TOP+37; g.drawString("Ctrl",LEFT,TOP+8); LEFT=LEFT+55; g.drawString("Win",LEFT,TOP+8); LEFT=LEFT+46; g.drawString("Alt",LEFT,TOP+8); LEFT=LEFT+46; g.drawString("Space",LEFT+90,TOP+8); LEFT=LEFT+213; g.drawString("Alt",LEFT,TOP+8); LEFT=LEFT+46; g.drawString("Win",LEFT,TOP+8); LEFT=LEFT+50; g.drawString("SM",LEFT,TOP+8); LEFT=LEFT+44; g.drawString("Ctrl",LEFT,TOP+8); } }
Hint:
import javax.swing.*; public class Hint extends JInternalFrame{ Tool parent; public Hint(Tool parent){ this.parent=parent; setLayer(4); setBounds(142,326,0,0); this.setPreferredSize(new java.awt.Dimension(657, 223)); ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null); this.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE); this.setVisible(true); this.pack(); this.add(new Keyset()); if(PublicData.choice!=2){ Editor edit=new Editor(this); this.parent.parent.add(edit,2); edit.requestFocus(); } } }
Editor:
import java.awt.FlowLayout; public class Editor extends JInternalFrame implements DocumentListener,Runnable{ Runtime r=Runtime.getRuntime(); Hint parent; Document doc; StyledDocument styledDoc = new DefaultStyledDocument(); Thread th; private int hour=0;//时 private int minute=0;//分 private int second=0;//秒 private JLabel lblTime; private JTextField txtCount; private JLabel lblError; private JTextPane txtOriginal; private JTextPane txtNewText; private JPanel pShowData; private JButton btnClose; private JLabel lblWord; private JTextField txtMW; private JLabel lblM; private JTextField txtError; private JTextField txtRight; private JLabel lblRight; private JLabel lblCount; private JTextField txtTime; private JPanel pAttrib; //记录输入的正确和错误的个数据 private int rightWord=0; private int errorWord=0; private int rightCount=0; private int errorCount=0; private int wordCount=0; //临时 private boolean temp=true; public Editor(Hint parent){ this.parent=parent; th=new Thread(this); //组建Editor用户界面.... setLayer(4); setBounds(142,0,0,0); this.setPreferredSize(new java.awt.Dimension(657, 325));//内部窗体大小 ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null); this.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE); getContentPane().setLayout(null); this.setVisible(true); { pAttrib = new JPanel(); getContentPane().add(pAttrib, new GridBagConstraints(1, 0, 12, 3, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); FlowLayout pAttribLayout = new FlowLayout(); pAttrib.setBorder(BorderFactory.createTitledBorder("")); pAttrib.setLayout(pAttribLayout); pAttrib.setBounds(5, 21, 658, 42); { lblTime = new JLabel(); pAttrib.add(lblTime); lblTime.setText("\u7528\u65f6:"); lblTime.setFont(new java.awt.Font("新宋体", 0, 12)); } { txtTime = new JTextField(8); pAttrib.add(txtTime); txtTime.setText("00:00:00"); txtTime.setEditable(false); txtTime.setHorizontalAlignment(SwingConstants.CENTER); } { lblCount = new JLabel(); pAttrib.add(lblCount); lblCount.setText("\u603b\u5b57\u6570:"); lblCount.setFont(new java.awt.Font("新宋体", 0, 12)); } { txtCount = new JTextField(); pAttrib.add(txtCount); txtCount.setColumns(6); txtCount.setText("0"); txtCount.setEditable(false); txtCount.setHorizontalAlignment(SwingConstants.CENTER); } { lblRight = new JLabel(); pAttrib.add(lblRight); lblRight.setText("\u6b63\u786e:"); lblRight.setFont(new java.awt.Font("新宋体", 0, 12)); } { txtRight = new JTextField(); pAttrib.add(txtRight); txtRight.setColumns(4); txtRight.setText("0"); txtRight.setEditable(false); txtRight.setHorizontalAlignment(SwingConstants.CENTER); } { lblError = new JLabel(); pAttrib.add(lblError); lblError.setText("\u9519\u8bef:"); lblError.setFont(new java.awt.Font("新宋体", 0, 12)); } { txtError = new JTextField(); pAttrib.add(txtError); txtError.setColumns(4); txtError.setText("0"); txtError.setEditable(false); txtError.setHorizontalAlignment(SwingConstants.CENTER); } { lblM = new JLabel(); pAttrib.add(lblM); lblM.setText("\u6bcf\u5206\u949f"); lblM.setFont(new java.awt.Font("新宋体", 0, 12)); } { txtMW = new JTextField(); pAttrib.add(txtMW); txtMW.setColumns(4); txtMW.setText("0"); txtMW.setEditable(false); txtMW.setHorizontalAlignment(SwingConstants.CENTER); } { lblWord = new JLabel(); pAttrib.add(lblWord); lblWord.setText("\u4e2a\u5b57"); lblWord.setFont(new java.awt.Font("新宋体", 0, 12)); } { btnClose = new JButton(); pAttrib.add(btnClose); btnClose.setText("\u79bb\u5f00"); btnClose.setFont(new java.awt.Font("新宋体", 0, 12)); btnClose.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { exitFram(); } }); } } { pShowData = new JPanel(); getContentPane().add( pShowData, new GridBagConstraints( 1, 1, 12, 10, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); GridLayout pShowDataLayout = new GridLayout(2, 1); pShowDataLayout.setHgap(5); pShowDataLayout.setVgap(5); pShowDataLayout.setColumns(1); pShowDataLayout.setRows(2); pShowData.setLayout(pShowDataLayout); pShowData.setBounds(5, 82, 644, 231); { txtOriginal = new JTextPane(styledDoc); pShowData.add(txtOriginal); txtOriginal.setPreferredSize(new java.awt.Dimension(600, 0)); txtOriginal.setEditable(false); } { txtNewText = new JTextPane(); pShowData.add(txtNewText); txtNewText.setPreferredSize(new java.awt.Dimension(600, 0)); Document doc=txtNewText.getDocument(); doc.addDocumentListener(this); } } { txtOriginal.setText(DistillText.getData(PublicData.Text,PublicData.textTag)); //PublicData.key=txtOriginal.getText().substring(0,1); Empty empty=new Empty(); empty.start(); if(txtOriginal.getText().length()!=0) { if(PublicData.Lang.equals("English")){ PublicData.key=txtOriginal.getText().substring(0,1); this.parent.repaint(); } if(PublicData.Lang.equals("Chinese")){ PublicData.key=""; } } } this.pack(); txtNewText.requestFocus(); th.start(); } //判断正确和错误的字.并改变它们的颜色 public void chDocs(int xLen,int yLen,String xStr,String yStr) { SimpleAttributeSet attrSet=new SimpleAttributeSet(); rightWord=0; errorWord=0; for(int i=1;i<=xLen;i++) { if(i<=yLen){//数据验证 if(xStr.substring(i-1,i).equals(yStr.substring(i-1,i))){ rightWord++; StyleConstants.setForeground(attrSet,Color.BLUE); } else{ errorWord++; StyleConstants.setForeground(attrSet,Color.RED); } StyleConstants.setUnderline(attrSet,true); } else{ StyleConstants.setForeground(attrSet,Color.BLACK); StyleConstants.setUnderline(attrSet,false); } styledDoc.setCharacterAttributes(i-1,1,attrSet,true); } txtRight.setText((rightWord+rightCount)+""); txtError.setText((errorWord+errorCount)+""); int c=0; for(int i=0;i<minute;i++){ c=c+60; } c=c+second; double s=(double)c/60; txtMW.setText(""+Math.round(((double)Integer.parseInt(txtRight.getText()))/s)); } //实现DocumentListener所有的方法..用于监听输入文本... public void changedUpdate(DocumentEvent e){ if(PublicData.Lang.equals("English")){ PublicData.key=txtOriginal.getText().substring(txtNewText.getText().length(), txtNewText.getText().length()+1); this.parent.repaint(); } if(PublicData.Lang.equals("Chinese")){ PublicData.key=""; } } public void insertUpdate(DocumentEvent e){ if(PublicData.Lang.equals("English")){ try{ PublicData.key=txtOriginal.getText().substring(txtNewText.getText().length(), txtNewText.getText().length()+1); }catch(StringIndexOutOfBoundsException str){} this.parent.repaint(); } if(PublicData.Lang.equals("Chinese")){ PublicData.key=""; } } public void removeUpdate(DocumentEvent e){ if(PublicData.Lang.equals("English")){ PublicData.key=txtOriginal.getText().substring(txtNewText.getText().length(), txtNewText.getText().length()+1); this.parent.repaint(); } if(PublicData.Lang.equals("Chinese")){ PublicData.key=""; } } //数据验证 private void dataValidate(){ chDocs(txtOriginal.getText().length(),txtNewText.getText() .length(),txtOriginal.getText(),txtNewText.getText()); if(txtOriginal.getText().length()>txtNewText.getText().length()){ if(PublicData.Lang.equals("English")){ PublicData.key=txtOriginal.getText().substring(txtNewText.getText().length(), txtNewText.getText().length()+1); } if(PublicData.Lang.equals("Chinese")){ PublicData.key=""; } txtCount.setText((txtNewText.getText().length()+wordCount)+""); } else{ wordCount=wordCount+txtOriginal.getText().length(); rightCount=rightCount+rightWord; errorCount=errorCount+errorWord; txtOriginal.setText(DistillText.getData(PublicData.Text,PublicData.textTag)); if(PublicData.Lang.equals("English")){ PublicData.key=txtOriginal.getText().substring(0,1); this.parent.repaint(); } if(PublicData.Lang.equals("Chinese")){ PublicData.key=""; this.parent.repaint(); } } } //退出时所要关闭的窗体 private void exitFram(){ PublicData.key=""; PublicData.Text=""; this.parent.repaint(); this.parent.doDefaultCloseAction(); this.doDefaultCloseAction(); PublicData.onChoice=true; PublicData.textTag=1; r.gc(); System.gc(); } //计时器 public void run(){ try{ while(true){ Thread.sleep(1000); second++; if(second==60){ second=0; minute++; if(minute==60){ minute=0; hour++; if(hour==1000){ second=0; minute=0; hour=0; } } } txtTime.setText(((hour<=9)?"0":"")+hour+":"+((minute<=9)?"0":"") +minute+":"+((second<=9)?"0":"")+second); } } catch (InterruptedException e){} } class Empty extends Thread{ public void run(){ while(true){ try{ Thread.sleep(100); dataValidate(); if(txtNewText.getText().length()>=txtOriginal.getText().length()){ txtNewText.setText(""); } } catch(InterruptedException e){} } } } }
Text:
import java.io.*; public class DistillText { public static String getData(String fileName,int segment){ int i=0; if((fileName==null) || (fileName.length()==0)) return ""; File f; FileReader in=null; String s=""; try{ f=new File(fileName); in=new FileReader(f); char[] buffer=new char[312]; int len; if(PublicData.Lang.equals("English")){ if(segment>=(int)(f.length()/312)) PublicData.textTag=0; } if(PublicData.Lang.equals("Chinese")){ if(segment>=(int)(f.length()/312-1)+1) PublicData.textTag=0; } while((len=in.read(buffer))!=-1){ s=new String(buffer,0,len); i++; if(i==segment){ PublicData.textTag++; break; } } } catch(IOException e){ System.out.println(e); } finally{ try{ if(in!=null) in.close(); } catch(IOException e){} } return s; } }
我要把它转化成使用swt来写,加油!!。
Java + eclipse + awt 编写锻炼打字小软件(未完成)的更多相关文章
- Java Eclipse解决中文字体太小
解决方式有两种: 一.把字体设置为Courier New 操作步骤:打开Elcipse,点击菜单栏上的“Windows”——点击“Preferences”——点击“Genneral”——点击 ...
- 用Java语言编写的迷宫小游戏软件
可查看本项目的github 源码链接,撒娇打滚求 star 哦~~ღ( ´・ᴗ・ ` )比心 本仓库代码是经过 eclipse 编译运行过的,一般情况下将本仓库代码下载下来之后,使用 eclipse ...
- 【转载】java版打字练习软件
网上找到一个java版的打字的游戏 import java.applet.Applet; import java.applet.AudioClip; import java.awt.Dimension ...
- Qt5.5.0使用mysql编写小软件源码讲解---顾客信息登记表
Qt5.5.0使用mysql编写小软件源码讲解---顾客信息登记表 一个个人觉得比较简单小巧的软件. 下面就如何编写如何发布打包来介绍一下吧! 先下载mysql的库文件链接:http://files. ...
- 用Java编写的猜拳小游戏
学习目标: 熟练掌握各种循环语句 例题: 代码如下: // 综合案例分析,猜拳案例 // isContinue为是否开始游戏时你所输入的值 char isContinue; //y为开始,n为借宿 S ...
- 【6】JAVA---地址App小软件(QueryPanel.class)(表现层)
查找模块: 年龄可进行段查找. 其他的都是模糊匹配. 空格为无用字符,会屏蔽的(除年龄). (如果在年龄中输入空格,会出现异常,当时没想到这点,要防护这点很容易的,但因为在这个小软件的编写过程,我主要 ...
- 8个爽滑如丝的Windows小软件,不好用你拿王思葱砸死我
假如我说有一款软件,能顶替60款软件:还有一款软件,能顶替60个你:还有一款软件,好用到60岁你都不想它被顶替.....我知道,你不相信天是蓝的,你不相信雷的回声,你不相信梦是假的,你不相信死无报应. ...
- java+eclipse安装及配置
一.JDK安装 0x00 下载JDK 首先我们需要下载java开发工具包JDK 下载地址:https://www.oracle.com/technetwork/java/javase/download ...
- 【1】JAVA---地址App小软件(AddressApp.class)(初步接触项目开发的分层思想)(表现层)
这个是表现层的main方法. 实现的地址信息有: 姓名,性别,年龄,电话,地址. 实现的功能有: 增加地址: 删除地址: 修改地址: 查找地址:其中年龄的查找为年龄段的查找. 数据存储的方式为文件存储 ...
随机推荐
- OnMeasureItem和OnDrawItem的区别和联系
我们在做程序设计时界面与功能,那个更加吸引用户的兴趣呢?这是一个很难回答的问题.拥有美丽的外观,软件就成功了一半.界面由控件.工具栏.菜单.窗体等元素组成,对他们进行美化就能得到一个美丽的界面. 目前 ...
- 淘宝API开发系列---阿里.聚石塔.开放平台的使用
好久没有继续跟进淘宝的API使用了,有很多做相关应用的同行都来咨询,很多都因为自己开发工作比较忙而没有来得及好的处理,前几天,有一个朋友叫帮忙指导如何使用淘宝API,由于原来有一些成熟的例子应用,因此 ...
- [CLR via C#]16. 数组
数组是允许将多个数据项当作一个集合来处理的机制.CLR支持一维数组.多维数组和交错数据(即由数组构成的数组).所有数组类型都隐式地从System.Array抽象类派生,后者又派生自System.Obj ...
- [CLR via C#]8. 方法
一.实例构造器和类(引用类型) 类实例构造器是允许将类型的实例初始化为良好状态的一种特殊的方法. 类实例构造器方法在"方法定义元数据表"中始终叫.ctor(代表constructo ...
- sqlsever备份,还原和导入导出方法
一:sqlsever数据库的几种备份和还原方法: 第一种:备份还原方法 1.选择要备份的数据库-任务-备份- 打开之后会看到里面有: 常规:在常规页面中我们可以看到:右下角位置有一个可以更改存储(备份 ...
- [持续更新] 文章列表 last updated SEP 18, 2016
1.前端 HTML5快速学习二 Canvas@20141125 HTML5快速学习一@20141122 2.ASP.NET(MVC) MVC5+EF6 入门完整教程14--动态生成面包屑@201608 ...
- sql server:compare data from two tables
--Comparing data between two tables in SQL Server --Create two Tables-- CREATE TABLE TableA(ID Int, ...
- Linux基础学习系列(一)
Linux是一种类似于UNIX的操作系统,由Linus Torvalds于1991年在minix操作系统的基础创建.Linux凭借其优良特性已经成为目前发展潜力最大的操作系统. Linux的版本有内核 ...
- xshell下载文件到本地/上传文件到服务器
xshell很好用,然后有时候想在windows和linux上传或下载某个文件,其实有个很简单的方法就是rz,sz首先你的Ubuntu需要安装rz.sz(如果没有安装请执行以下命令,安装完的请跳过.其 ...
- guacamole 0.9.9安装与配置
以下命令很多都需要管理权限,建议使用管理员账号执行,遇到问题可以留言. 1.首先需要安装guacamole所需要的依赖库 必需安装的库有:Cairo.libjpeg-turbo.libpng.OSSP ...