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

📄 myinter.java

📁 用图形界面实现Timer
💻 JAVA
字号:
// MyInter.java: Receive key input
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyInter extends JApplet
{
  private KeyboardPanel keyboardPanel = new KeyboardPanel();

  // Main method used if run as an application
  public static void main(String[] args)
  {
  	//notic informatin
  	System.out.println("NOTICE:");
  	System.out.println("Pleas make the small 'Timer Demo' window activable, ");
  	System.out.println("when you want to stop(restart) the timer.");
  	System.out.println("But you only can input alternation in the DOS-windouw.");
  	
    // Create a frame
    JFrame frame = new JFrame("Timer Demo     @ copyright 021221082");
    // Create an instance of the applet
    MyInter applet = new MyInter();

    // Add the applet instance to the frame
    frame.getContentPane().add(applet, BorderLayout.CENTER);

    // Invoke init() and start()
    applet.init();
    applet.start();

    // Display the frame
    frame.setSize(350, 200);
    // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    // Set focus on the keyboardPanel
    applet.focus();
  }

  // Initialize UI
  public void init()
  {
    // Add the keyboard panel to accept and display user input
    getContentPane().add(keyboardPanel);

    // Request focus
    focus();

  }

  // Set focus on the panel
  public void focus()
  {
    // It is required for receiving key input
    keyboardPanel.requestFocus();
  }
}

// KeyboardPanel for receiving key input
class KeyboardPanel extends JPanel implements KeyListener
{
  private boolean end=false;
  private boolean exit=true;
  private int x = 5;
  private int y = 80;
  private String keyChar ="Your pressed key is:"; // Default string
  private MyTimer mytimer=new MyTimer();
  public KeyboardPanel()
  {
  	//start mytimer
  	mytimer.start();
  	
  	// Register listener		
    addKeyListener(this); 
  }

  public void keyReleased(KeyEvent e)
  {
  	keyChar ="Your pressed key is:";
  }

  public void keyTyped(KeyEvent e)
  {
  }

  public void keyPressed(KeyEvent e)
  {
	//When end is false and timer's run is true ,you can stop it.
  	if(end==false&&mytimer.getRun()==true)  
	{	
		mytimer.setEnd(true);
		System.out.println("************************************************************");
		System.out.println("timer is stopped now! And you can close the window to exit!");
		System.out.println("If you want to restart it ,pleas press anykey,"); 
		System.out.println("whit makeing the small 'Timer Demo' window activable!");
		System.out.println("************************************************************");
	  	keyChar = keyChar + e.getKeyChar();
	  	end=true;
		
  	}
  	else 
  	{
  		mytimer.setEnd(false);	//restart the timer by set endtime with false
  		
  		end=false;
  		keyChar = keyChar + e.getKeyChar();
  	}
    repaint();
    
  }

  // Draw the character
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);

    g.setFont(new Font("TimesRoman", Font.PLAIN, 24));
    g.drawString(String.valueOf(keyChar), x, y);
  }
}
// Mytimer class 
class MyTimer extends Thread
{
	private boolean endtime=false;
	private boolean run=false;
	public void run()
	{
		
		for(;;)
		{
			int inter=15;
			if(endtime==false)
			{
				System.out.println("************************************************************");
				System.out.print("Pleas set timer's alternation(s): ");
				inter=MyInput.readInt();
				System.out.println("You can input anykey to stop it,");
				System.out.println("whit makeing the small 'Timer Demo' window activable!");
				System.out.println("************************************************************");
				run=true;
			}
			else continue;
			for(int i=1;;i++)
			{
				if(endtime==true)
				{
					run=false;	
					break;
				}
				if(i%inter==0)	//time up,inform
				{
				
					System.out.println("************************************************************");
					System.out.println(i+"s Time up! You can input anykey to stop it,");
					System.out.println("whit makeing the small 'Timer Demo' window activable!");
					System.out.println("************************************************************");
					i=0;
				}
				else System.out.println(i+"s ");
				
				//only sleep 1s per.
				try{Thread.sleep(1000);} 
				  
				catch (InterruptedException ex)
				{
					System.out.println(ex);
				}
				
			}
				
		}		
	}
	public void setEnd(boolean t)
	{
		endtime=t;
	}
	public boolean getRun()
	{
		return run;
	}
}

⌨️ 快捷键说明

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