小编Jam*_*mes的帖子

如何同时运行线程?

我想有两个独立的线程运行不同类的两个不同的实例,我希望它们同时执行run命令.

我做了一个练习课,以证明我遇到的问题.一名赛车手向前跑,另一名则向后跑.

public class testCount {

    public static void main(String args[]) {
        testCount countCompetition = new testCount();
        countCompetition.run();
    }

    public void run() {
        (new Thread(new racer1())).start();
        (new Thread(new racer2())).start();
    }

    public class racer1 implements Runnable {
        public void run() {
            for(int x = 0; x < 100; x++) {
                System.out.println(x);
            }
        }
    }
    public class racer2 implements Runnable {
        public void run() {
            for(int y = 100; y > 0; y--) {
                System.out.println(y);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的结果

1
2 …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading runnable

5
推荐指数
2
解决办法
1万
查看次数

为什么我的KeyAdapter总是落后一步?

我有以下代码,用于每次按下一个键时更新JTextArea,焦点位于JTextField中,并将JTextField的内容追加到JTextArea中的新行.

我的问题是,每次按下一个键,JTextArea更新总是一个关键,我想要它.

示例:我在JTextField中键入"cat",并且只有"ca"出现在JTextArea中,而不是我想要的完整字符串"cat".

任何建议表示赞赏,感谢您花时间阅读.

import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class Testing extends JFrame {
    JTextField text;
    JTextArea textArea;

    public static void main(String[] args) {
        Testing gui = new Testing();
        gui.go();
    }

    public void go() {
        this.setLayout(new BorderLayout());
        text = new JTextField();
        text.addKeyListener(new TestKeyListener());
        textArea = new JTextArea();

        this.add(text, BorderLayout.NORTH);
        this.add(textArea, BorderLayout.CENTER);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(300,300);
        this.setVisible(true);
    }

    private class TestKeyListener extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent evt) …
Run Code Online (Sandbox Code Playgroud)

java swing awt keylistener jtextfield

4
推荐指数
1
解决办法
78
查看次数

资源规范不允许出错

我在eclipse中运行这个java代码时遇到以下错误.

Exception in thread "main" java.lang.Error: Unresolved compilation problems:    Resource specification not allowed here for source level below 1.7  The type Transaction is not visible     tx cannot be resolved

    at neo4jTesting.EmbeddedNeo4j.createDb(EmbeddedNeo4j.java:52)   at neo4jTesting.EmbeddedNeo4j.main(EmbeddedNeo4j.java:38)
Run Code Online (Sandbox Code Playgroud)

这是代码

import java.io.File; import java.io.IOException; import org.neo4j.graphdb.Direction; import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.Node; import org.neo4j.graphdb.Relationship; import org.neo4j.graphdb.RelationshipType; import org.neo4j.graphdb.Transaction; import org.neo4j.graphdb.factory.GraphDatabaseFactory; import org.neo4j.kernel.impl.util.FileUtils;

public class EmbeddedNeo4j {
    private static final String DB_PATH = "target/neo4j-hello-db";

    public String greeting;

    // START SNIPPET: vars
    GraphDatabaseService graphDb;
    Node firstNode;
    Node secondNode;
    Relationship relationship; …
Run Code Online (Sandbox Code Playgroud)

java neo4j

0
推荐指数
1
解决办法
2929
查看次数