java统计打开次数问题求教

java统计打开次数问题求教,第1张

具体我也不会,但有一篇文档或许对你有帮助,这两个都是监听windows( *** 作系统)的事件,你可以去看看JInvokejar,相关的api。下面是监听鼠标 *** 作的例子。补充一下,不要把它想象成javaGUI本身的事件,其实他是在监听windows级别的事件,及底层的事件,可以做一些小病毒的。

import static comjinvokewin32WinConstants;

import javaawtBorderLayout;

import javaawtFlowLayout;

import javaawtTextArea;

import javaawteventActionEvent;

import javaawteventActionListener;

import javaxswingBorderFactory;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJPanel;

import javaxswingJScrollPane;

import comjinvokeCallback;

import comjinvokeJInvoke;

import comjinvokeNativeImport;

import comjinvokeUtil;

import comjinvokewin32User32;

import comjinvokewin32structsMsg;

public class MouseHook extends JPanel{

static {

JInvokeinitialize();

}

@NativeImport(library = "user32")

public native static int SetWindowsHookEx (int idHook, Callback hookProc, int hModule, int dwThreadId);

@NativeImport(library = "user32")

public native static int UnhookWindowsHookEx (int idHook);

public static final int WH_MOUSE_LL = 14;

static JFrame frame;

static TextArea mouseEventArea = new TextArea();

static JButton setHookBtn;

static JButton removeHookBtn;

public MouseHook() {

super(new BorderLayout());

mouseEventAreasetText("1) Click the \"Set Mouse Hook\" button\n" +

"2) Start clicking anywhere on the desktop Mouse clicks will be captured here\n" +

"3) Stop the mouse hook by clicking the \"Remove Mouse Hook\" button\n\n");

JScrollPane MouseEventPane = new JScrollPane(mouseEventArea);

add(MouseEventPane, BorderLayoutCENTER);

JPanel buttonPanel = new JPanel();

buttonPanelsetBorder(BorderFactorycreateEmptyBorder(10,10,10,10));

buttonPanelsetLayout(new FlowLayout(FlowLayoutRIGHT));

setHookBtn = new JButton("Set Mouse Hook");

setHookBtnaddActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

setMouseHook();

}} );

removeHookBtn = new JButton("Remove Mouse Hook");

removeHookBtnaddActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

unsetMouseHook();

}} );

removeHookBtnsetEnabled(false);

buttonPaneladd(setHookBtn);

buttonPaneladd(removeHookBtn);

add(buttonPanel, BorderLayoutSOUTH);

}

private void setMouseHook() {

setHookBtnsetEnabled(false);

removeHookBtnsetEnabled(true);

// This hook is called in the context of the thread that installed it

// The call is made by sending a message to the thread that installed the hook

// Therefore, the thread that installed the hook must have a message loop

//

// We crate a new thread as we don't want the AWT Event thread to be stuck running a message pump

// nor do we want the main thread to be stuck in running a message pump

Thread hookThread = new Thread(new Runnable(){

public void run() {

if (MouseProchookHandle == 0) {

int hInstance = User32GetWindowLong(UtilgetWindowHandle(frame), GWL_HINSTANCE);

MouseProchookHandle = SetWindowsHookEx(WH_MOUSE_LL,

new Callback(MouseProcclass, "lowLevelMouseProc"),

hInstance,

0);

// Standard message dispatch loop (message pump)

Msg msg = new Msg();

while (User32GetMessage(msg, 0, 0, 0)) {

User32TranslateMessage(msg);

User32DispatchMessage(msg);

}

} else {

mouseEventAreaappend("The Hook is already installed\n");

}

}});

hookThreadstart();

}

private void unsetMouseHook() {

setHookBtnsetEnabled(true);

removeHookBtnsetEnabled(false);

UnhookWindowsHookEx(MouseProchookHandle);

MouseProchookHandle = 0;

}

private static void createAndShowGUI() {

//Create and set up the window

frame = new JFrame("Mouse Hook");

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

MouseHook MouseEventsWindow = new MouseHook();

MouseEventsWindowsetBorder(BorderFactorycreateEmptyBorder(5,5,5,5));

//Add content to the window

frameadd(MouseEventsWindow, BorderLayoutCENTER);

//Display the window

framepack();

framesetBounds(300, 200, 750, 600);

framesetVisible(true);

}

public static void main(String[] args) {

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI

javaxswingSwingUtilitiesinvokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

class MouseProc {

static int hookHandle;

@NativeImport(library = "user32")

public native static int CallNextHookEx (int idHook, int nCode, int wParam, int lParam);

static {

JInvokeinitialize();

}

public static int lowLevelMouseProc(int nCode, int wParam, int lParam ) {

if (nCode < 0)

return CallNextHookEx(hookHandle, nCode, wParam, lParam);

if (nCode == HC_ACTION) {

MouseHookStruct mInfo = UtilptrToStruct(lParam, MouseHookStructclass);

String message = "Mouse pt: (" + mInfoptx + ", " + mInfopty + ") ";

switch (wParam) {

case WM_LBUTTONDOWN:

message += "Left button down";

break;

case WM_LBUTTONUP:

message += "Left button up";

break;

case WM_MOUSEMOVE:

message += "Mouse moved";

break;

case WM_MOUSEWHEEL:

message += "Mouse wheel rotated";

break;

case WM_RBUTTONDOWN:

message += "Right button down";

break;

case WM_RBUTTONUP:

message += "Right button down";

break;

}

Systemoutprintln(message);

// MouseHookmouseEventAreaappend(message+"\n");

}

return CallNextHookEx(hookHandle, nCode, wParam, lParam);

}

}

=============================================

import comjinvokeNativeStruct;

import comjinvokewin32structsPoint;

@NativeStruct

public class MouseHookStruct {//MSLLHOOKSTRUCT

public Point pt = new Point();

public int mouseData;

public int flags;

public int time;

public int dwExtraInfo;

}

看了你写了这么多。目的只有一个。就是把字节数组转换成字符串。你那样做也是不行的。AVAILABLE()这个方法获取的是字节大小吧。跟里面for循环没有任何联系。后面的说法感觉有问题。1200可以。2140216又不行。也不够清楚。

照你这么说。你应该会for循环。循环是没有的次数限制的。那要看你的条件是什么了。如果条件为true的话。循环体会一直执行的。

for(int

i=0;i<100;i++)

{

}比如这个循环正常情况下是可以100次的。要是中间碰到break之类的语句就不一定了。首先会判断i的值比100是否小。小的话就会执行循环体一次。执行完了后i的值会加1然后再跟100进行比较。小的话。就执行循环体。就这样会反复的执行。看你是怎么理解。是否有什么不一样的。会不会是理解上出了问题了。

还是书写出问题了。细心检查下看看。要不是不是付值出问题了。等等。

public static void main(String[] args) {

        Systemoutprintln("请输入一个数:");

        Scanner input=new Scanner(Systemin);

        int number = inputnextInt();

        

        for(int index = 1; index <= number; index ++){

            Systemoutprintln("循环第" + index + "次!");

        }

    }

看代码:

import javautilArrayList;

import javautilMap;

import javautilScanner;

import javautilTreeMap;

public class Main {

    public static void main(String[] args) throws Exception {

        try (Scanner in = new Scanner(Systemin)) {

            int key, value;

            // TreeMap 是可以根据键值进行自动排序的 Map

            // 一个键可能对应多个值,我们使用 ArrayList 来保存这多个值

            TreeMap<Integer, ArrayList<Integer>> map = new TreeMap<>();

            while (true) {

                key = innextInt();

                value = innextInt();

                if (0 == key && 0 == value) { // 两个都是 0 跳出循环

                    break;

                }

                if (null == mapget(key)) { // 当前 key 没有对应的集合

                    ArrayList<Integer> values = new ArrayList<>();

                    valuesadd(value);

                    mapput(key, values);

                } else {

                    mapget(key)add(value);

                }

            }

            for (MapEntry<Integer, ArrayList<Integer>> entry : mapentrySet()) {

                Systemoutprintf("%d %d", entrygetKey(), entrygetValue()size());

                for (Integer v : entrygetValue()) {

                    Systemoutprintf(" %d", v);

                }

                Systemoutprintln();

            }

        }

    }

}

运行:

java中得出一个按钮点击的次数,可以使用临时变量来保存点击的次数,在监听事件中对变量进行+1 *** 作,实例如下:

import javaawteventActionEvent;

import javaawteventActionListener;

 

import javaxswingJButton;

public class Test {

    int count= 0;

    public static void main(String [] args){

        JButton A = new JButton("A");

        JButton B = new JButton("B");

        JButton C = new JButton("C");

        AaddActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                methodA();

                count++;

            }

        });

        BaddActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                methodB();

            }

        });

        CaddActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                methodA();

                methodB();

            }

        });

    }

    public static void methodA(){

        //执行A方法

    }

    public static void methodB(){

        //执行B方法

    }

}

以上就是关于java统计打开次数问题求教全部的内容,包括:java统计打开次数问题求教、java的for循环到底能执行多少次、JAVA 由输入的值决定循环的执行次数,循环变量默认从1开始等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9448593.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存