如何在后台运行jar应用程序?

时间:2016-06-17 16:35:33

标签: java jar

我希望我的java jar应用程序能够在后台运行。我想玩一个特定的游戏,当应用程序运行时,应用程序将计算我在游戏中按下的键。

这是我的代码:

import java.awt.Font;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class Osu extends JFrame implements KeyListener,ActionListener {

    File plik = new File("clicks.txt");
    static ArrayList<Integer> liczby = new ArrayList<Integer>();
    static JLabel label1 = new JLabel("");
    static JLabel label2 = new JLabel("");
    static int tab[];
    public static final int WIDTH=200, HEIGHT=200;
    public String click1="";
     static int licznik1,licznik2;
     static int a=0;

    static int liczba=0,liczba1=0;

public Osu(){


    setTitle("Osu Click Counter");

    setSize(WIDTH,HEIGHT);

    addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
              doPliku();
          }
        });


    addKeyListener(this);
    setResizable(false);
    setFocusable(true);
    setVisible(true);
    setLayout(null);
    draw();

    pack();
    label1.setFont(new Font("Courier New", Font.BOLD,25));
    label2.setFont(new Font("Courier New", Font.BOLD, 25));
    label1.setBounds(0, 20, 100, 20);
    label2.setBounds(0, 40,100, 20);
    add(label1);
    add(label2);



}

public void draw(){

    try {

    setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("background.png")))));
    } catch (IOException e) {
        System.out.println("nie znaleziono obrazu kulki");
        e.printStackTrace();
    }

}

public static  void update(){

    while(true){
        label1.setText("E " + licznik1);
        label2.setText("W " + licznik2);
        }
}

public static void doPliku(){
    try{
        PrintWriter zapis = new PrintWriter("clicks2.txt");
        zapis.println( licznik1 );
        zapis.println( licznik2);
        zapis.close();
        }


    catch(FileNotFoundException e){ 
    }
}


public static void scan() throws NumberFormatException, IOException{


      String nazwaPliku = "clicks2.txt";

      try {
          BufferedReader br = new BufferedReader(new FileReader(nazwaPliku));
          String linia = null;
          while ((linia = br.readLine()) != null) {
              int liczba = Integer.parseInt(linia.trim());
              liczby.add(liczba);
          }
      } catch (Exception e) {

      }


}
    public static void check(){
     if(liczby.size() !=0){
         licznik1=liczby.get(0);
      licznik2=liczby.get(1);
      liczby.clear();
     }
    }



    public static void main(String[] args) throws NumberFormatException, IOException  {

        scan();
        check();
    new Osu();
    update();

}



    @Override
    public void keyPressed(KeyEvent e) {
        if( e.getKeyCode() == KeyEvent.VK_E){
            licznik1++;
        }
        if( e.getKeyCode() == KeyEvent.VK_W){
            licznik2++;
        }


    }
    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }

    }

0 个答案:

没有答案