在下面的代码中,我得到了一个错误,但不知怎么我找不到信息来修复它。对任何误解感到抱歉。
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.Kernel32;
// JNA infrastructure import libs.Kernel32;
// Proxy interface for kernel32.dll
public interface JnaTests extends Library {
public boolean Beep(int FREQUENCY , int DURATION );
static Kernel32 kernel32 = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
static void toMorseCode(String letter) throws Exception {
for (byte b : letter.getBytes()) {
kernel32.Beep(1200, ((b == '.') ? 50 : 150));
Thread.sleep(50);
}
}
public static void main(String[] args) throws Exception {
String helloWorld[][] = { {"....", ".", ".-..", ".-..", "---"}, {".--", "---", ".-.", ".-..", "-.."}};
for (String word[] : helloWorld) {
for (String letter : word) {
toMorseCode(letter);
Thread.sleep(150);
}
Thread.sleep(350);
}
}
}发布于 2016-04-24 21:48:25
谢谢你的回答。
最后,我发现在一个分离的文件中应该有一个接口(Kernel32)。
社区文档中提到了这一点,但是一些.dll也没有接口,例如User32.dll。
package com.sun.jna.platform;
import com.sun.jna.Library;
//@author windows-System
public class win32 {
public interface Kernel32 extends Library {
boolean Beep(int frequency, int duration);
// ... (lines deleted for clarity) ... }
}}
主文件
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.Kernel32;
// JNA infrastructure import libs.Kernel32;
// Proxy interface for kernel32.dll
public class JnaTests {
private static Kernel32 kernel32 = (Kernel32)
Native.loadLibrary ("kernel32", Kernel32.class);
private static void toMorseCode(String letter) throws Exception {
for (byte b : letter.getBytes()) {
kernel32.Beep(1200, ((b == '.') ? 50 : 150));
Thread.sleep(50);
}
}
public static void main(String[] args) throws Exception {
String helloWorld[][] = { {"....", ".", ".-..", ".-..", "---"},
{".--", "---", ".-.", ".-..", "-.."}};
for (String word[] : helloWorld) {
for (String letter : word) {
toMorseCode(letter);
Thread.sleep(150);
}
Thread.sleep(350);
}} }
发布于 2016-04-23 19:57:15
您没有为Kernel32类使用正确的名称。您已经用以下行导入了它:
import com.sun.jna.platform.win32.Kernel32;但你想用错名字用它:
kernel32.Beep(1200, ((b == '.') ? 50 : 150));注意大写。
值得注意的是,com.sun层次结构中的任何包本质上都是不安全的--它们的目的是完全在Java内部使用,而不是在程序中使用。它们可以在没有警告或向后兼容性的情况下进行更改,并且可能具有极其具体的、无文档的需求,使其不可靠供您使用。
具体而言,蜂鸣是一种非常特定于硬件和平台的东西,你甚至不能保证这段代码能在不同的Windows系统上工作,更不用说其他操作系统了。你最好播放一个实际的声音文件,因为它在任何地方都能工作,并给你带来一致的结果。更深入地讨论你想要的是什么,请看Java equivalent of C# system.beep?。
https://stackoverflow.com/questions/36815488
复制相似问题