Topic: 怎么样在控制台实现清屏????

  Print this page

1.怎么样在控制台实现清屏???? Copy to clipboard
Posted by: nationalist
Posted on: 2006-09-18 17:30

比如我要实现让 1 自动每隔一秒钟就加 1 ,也就是在同一个位置变成1,2,3,4,5...

而不是那样一排排的打印出来

就象C++里面的system(csl);
在java 里面怎么做啊?

2.Re:怎么样在控制台实现清屏???? [Re: nationalist] Copy to clipboard
Posted by: jackchengen
Posted on: 2006-09-18 19:52

用定时器Timer和运行时Runtime.
定时器实现定时刷行,Runtime调用本地方法cls实现清屏。不过我觉得这种方法是否太复杂了~
大家是否还有其他的方法?

3.Re:怎么样在控制台实现清屏???? [Re: nationalist] Copy to clipboard
Posted by: lisliefor
Posted on: 2006-09-19 11:18

LZ want to write a program about clock ?
why do you decide to showing on Console ? you can paint on canvas !
I do it before , if you want ....

4.Re:怎么样在控制台实现清屏???? [Re: nationalist] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2006-09-19 19:05

就让我来回答你的问题,真好让我复习一下JNI

基本上你要实现在Java的console上面是不太可能的。。有人实现了希望告诉我一下阿。(我对console和gui不是很熟的,我不是专门搞这个的)

1.写一段Java代码。

/**
* Increment 1 every 1 second and keep the output in the
* same line on the Console.
*/
package com.console;

/**
* Increment 1 every 1 second.
* @author jiafanz
*/
public class IncrementOneEvery1Sec
{
//the right command line usage message
private final static String MESSAGE = "Usage: java <path_and_filename> \n"
+ "\t(to execute the class file)\n\n"
+ "where path_and_filename indicates the system file path and name.\n\n"
+ "e.g. java IncrementOneEvery1Sec \r\n";

private static int value = 0;

/**
* This is a native method to invoke a c program.
* @param value the vlaue to be printed
*/
public native void printConsole(int value);


/**
* Load the library.
*
* Then the runtime system later loads this shared library
* into the Java class that requires it.
*
* a static initializer.
*
* e.g.
* Solaris system converts "hello" to "libhello.so"
* Windows system converts "hello" to "hello.dll"
*/
static
{
System.loadLibrary("printconsole");
}

/**
* Main method entry point.
* @param args the main method argument
*/
public static void main(String[] args)
{
if (args.length != 0)
{
// user provided wrong parameters
System.err.println("wrong parameter length.");
System.out.println(MESSAGE);
System.exit(1);
}

IncrementOneEvery1Sec test = new IncrementOneEvery1Sec();
try
{
while (true)
{
Thread.sleep(1000);
test.printConsole(++value);
}
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
}

基本上注释很全的,我就不解释了。
然后compile

2.生成 .h file
javah -jni com.console.IncrementOneEvery1Sec
(fully qualified modifier)

3.Native Method Implementation
(这里就我们的可爱的C了)

#include <jni.h>
#include "com_console_IncrementOneEvery1Sec.h"
#include <stdio.h>
#include <stdlib.h>

JNIEXPORT void JNICALL
Java_com_console_IncrementOneEvery1Sec_printConsole(JNIEnv *env, jobject obj, jint value)
{
  system("cls");
  printf("%d\n", value);
  return;
}

注意要用到stdlib这个头文件噢,不要忘了。

4.用gcc或者cl生成dll..(我用的是CL)

cl -IC:\jdk1.5.0_06\include -IC:\jdk1.5.0_06\include\win32 -LD IncrementOneEvery1SecImp.c -Feprintconsole.dll
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.507
Copyright (C) Microsoft Corporation. All rights reserved.

IncrementOneEvery1SecImp.c
Microsoft (R) Incremental Linker Version 8.00.50727.42
Copyright (C) Microsoft Corporation. All rights reserved.

/dll
/implib:printconsole.lib
/out:printconsole.dll
IncrementOneEvery1SecImp.obj
Creating library printconsole.lib and object printconsole.exp


5.好拉,下面就可以用了。
(不要在Eclipse里面运行,好象有点问题的)
java -Djava.library.path=<yourDllFolder> <YourClass>

e.g.
java -Djava.library.path=C:\cjsdn\cjsdn\clib\cjsdn com.console.IncrementOneEvery1Sec
(我这里的cmd)

能看出我是用心写的。。斑竹加分阿。

嘉帆

5.Re:怎么样在控制台实现清屏???? [Re: HenryShanley] Copy to clipboard
Posted by: lisliefor
Posted on: 2006-09-20 12:47

HenryShanley wrote:

能看出我是用心写的。。斑竹加分阿。



Haha , we have caught your essence !

6.Re:怎么样在控制台实现清屏???? [Re: nationalist] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2006-09-20 16:19

Come on, where are all the moderators?

they need to take care of here..

Jiafan

7.Re:怎么样在控制台实现清屏???? [Re: lisliefor] Copy to clipboard
Posted by: zcjl
Posted on: 2006-09-20 17:34

lisliefor wrote:
Haha , we have caught your essence !


本来面目: [ běn lái miàn mù ]

1. true colors
2. true features

其它相关解释:
<for what they are>

例句与用法:
1. 他一掌了权就露出了本来面目.
Once he achieved power he showed (himself in) his true colours.

8.Re:怎么样在控制台实现清屏???? [Re: zcjl] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2006-09-21 06:08

zcjl wrote:
本来面目: [ běn lái miàn mù ]

1. true colors
2. true features

其它相关解释:
<for what they are>

例句与用法:
1. 他一掌了权就露出了本来面目.
Once he achieved power he showed (himself in) his true colours.


What?

9.Re:怎么样在控制台实现清屏???? [Re: HenryShanley] Copy to clipboard
Posted by: zcjl
Posted on: 2006-09-21 08:28

HenryShanley wrote:
What?

我想lisliefor是想表达一个“本来面目”的意思,所以帮他查了一下词典和例句
hah

10.Re:怎么样在控制台实现清屏???? [Re: zcjl] Copy to clipboard
Posted by: lisliefor
Posted on: 2006-09-21 10:59

zcjl wrote:
本来面目: [ běn lái miàn mù ]

1. true colors
2. true features

其它相关解释:
<for what they are>

例句与用法:
1. 他一掌了权就露出了本来面目.
Once he achieved power he showed (himself in) his true colours.


Thanks ! Smile
I want to say "catch your '本质'" !
In my team , our boss used to say it .
I save the '帖子' !

Introduce some good dictionary about Chinest-to-English for me please!
Thanks !


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923