我想首先说,如果这是常识,请原谅我并有耐心.我对Java有些新意.我正在尝试编写一个程序,它将许多变量值存储在一种缓冲区中.我想知道是否有办法让程序"创建"自己的变量,并将它们分配给值.以下是我要避免的示例:
package test;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int inputCacheNumber = 0;
//Text File:
String userInputCache1 = null;
String userInputCache2 = null;
String userInputCache3 = null;
String userInputCache4 = null;
//Program
while (true) {
Scanner scan = new Scanner(System.in);
System.out.println("User Input: ");
String userInput;
userInput = scan.nextLine();
// This would be in the text file
if (inputCacheNumber == 0) {
userInputCache1 = userInput;
inputCacheNumber++;
System.out.println(userInputCache1);
} else if (inputCacheNumber == 1) {
userInputCache2 …Run Code Online (Sandbox Code Playgroud) 我正在使用Swing作为GUI开发Java应用程序.在这个项目中,我使用的是Java 8 Update 25.我一直在用Java编写另一个图形应用程序,但我使用的是Java 6.在这两个项目中,我都编写了相同的paint()方法.(如下所示)我也以同样的方式称呼'repaint()'.在这两个项目中,我正在绘制一个字符串.此字符串显示本地int,count的值; 每次调用paint()方法时,计数增加1.
当两个项目表现不同时,我的问题出现了.在Java 6中,屏幕更新速度非常快,应用程序的行为也很理想.但是,在Java 7和8中,应用程序不显示任何内容.如果我增加重绘之间的延迟,(大约300毫秒)我能看到字符串闪烁.但是,如果我想用Java 8开发游戏,例如,角色的闪烁和抖动是非常不可取的.
为什么不同的Java版本以这种方式表现不同?有没有一种方法可以使用类似的设置在Java 8中复制流畅的重绘(通过Java 6)?(如下所列)如果有,怎么样?如果没有,如何实现平滑,最小的闪烁显示?(我希望这个重绘不断重新绘制,但它不像显示器的流程那样必要)
感谢您的帮助,~Rane
Java 6项目代码:
public class App {
static AppDisplay display = new AppDisplay();
public static void main(String args[]) {
display.setup();
Thread graphics = new Thread() {
public void run() {
while(true) {
display.repaint();
try {
Thread.sleep(17); // This is the delay I am talking about above
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
graphics.start();
}
}
public class AppDisplay() extends JFrame {
private static …Run Code Online (Sandbox Code Playgroud) 我对这个论坛有另一个问题.我正在用Java编写一个小应用程序,其中我有多个Threads.我有两个不同类的线程.我有一个专用于定义所有JFrame组件的Thread(thread1).(JPanels,JButtons,ActionListeners等)在另一个Thread中,(thread2)我正在检查变量(v)是否等于某个值.(98)当这个变量确实变为98时,该相同的线程应该更新JLabel.(现在让我们称这个标签为标签.)它没有.有什么我做错了.我已经尝试为类编写一个方法 - 声明所有JFrame组件 - 更改标签的文本,但是当我从thread2调用此方法时,它不会更新标签.我试过调用一个包含thread1的超类,但这似乎没有什么区别.此外,我一直在调用repaint()方法,但这似乎没有帮助.
请让我知道如何从两个不同的线程更新这些JLabel.
提前谢谢,~Rane
码:
1类:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel label = new JLabel("V does not equal 98 Yet...");
Thread thread1 = new Thread(){
public void run(){
panel.add(label);
frame.add(panel);
System.out.println("Components added!");
}
});
public void setLabelText(String text){
label.setText(text);
}
Run Code Online (Sandbox Code Playgroud)
的Class1 = 2:
v = 0;
Thread thread2 = …Run Code Online (Sandbox Code Playgroud)