- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做一个小小的GUI应用程序来模拟游泳比赛。我有一个单独的GUI,用于游泳池和计分板,游泳者在其中移动了5个线程,当他们到达触摸屏时我已经编码了该时间,以便在计分板上显示该时间。
问题是,每当在记分板上显示标签的时间更新时,游泳者就会突然回到起始位置(而不是消失并出现在起始点的连续运动)而没有停留在触摸板附近。但是,如果标签未更新,则此方法有效。
这是GUI的图像。
这不是完整的代码,但这是负责游泳者运动并在游泳者完成长度调整时更新时间标签的代码段。
private final Map<Swimmer, JLabel> mapLaneTime = new HashMap<>();
private void startTheGame(final String swimmingStyle) {
final ArrayList<Swimmer> listSwimmer =
new ArrayList<> (mapSwimmers.keySet());
for (int i = 0; i < mapSwimmers.size(); i++) {
final Swimmer swmr = listSwimmer.get(i);
// set up the intial position of the JLabel to the swimmer
swmr.setCurrentPosition(mapSwimmers.get(swmr).getX());
new Thread(new Runnable() {
@Override
public void run() {
try {
swmr.slowDown();
JLabel lblSwmr = mapSwimmers.get(swmr);
swmr.doSwimmingStyle(swimmingStyle);
int startPosition = swmr.getCurrentPosition();
while ((swmr.getCurrentPosition()
!= swmr.getSwimLane().getLength()
+ startPosition)) {
lblSwmr.setLocation(swmr.doLegMovements(),
lblSwmr.getY());
swmr.doHandMovements();
swmr.speedUp();
}
// time takes for the swimmer to touch the touch pad
double time = swmr.touchTheTouchPad(swimmingEvent);
/** problem comes when the below line is present it is the way to set text
** to the label if this is not here swimmers properly end the length
** without appearing at the starting position when ever they finish the
** length
**/
mapLaneTime.get(swmr).setText(time + "");
} catch (InterruptedException ex) {
ex.printStackTrace();
} finally {
swmr.giveUp();
}
}
}).start();
}
}
import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public abstract class Swimmer extends Person {
private final String gender; // gender of the Swimmer
private final String costumeColor; //color of the costume
private final int personalBest;
private volatile int currentPosition;
private SwimLane swimLane;
private final Lock lock; // lock for the thread
private final Condition condition;
protected Swimmer(String name, String gender, String costumeColor) {
super(name);
this.gender = gender;
this.costumeColor = costumeColor;
Random random = new Random();
this.personalBest = random.nextInt(20) + 8;
lock = new ReentrantLock();
condition = lock.newCondition();
}
public SwimLane getSwimLane() {
return this.swimLane;
}
/**
* @return the color of the swimming costume
*/
protected String getDressColor() {
return this.costumeColor;
}
/**
* perform hand movements
*/
protected void doHandMovements() {
this.condition.signalAll();
}
/**
* perform leg movements
*
* @return
*/
protected int doLegMovements() {
return this.currentPosition;
}
/**
* perform Butterfly stroke
*
* @param sleepTime
*/
protected abstract void doButterflyStroke(int sleepTime);
/**
* perform Backstroke
*
* @param sleepTime
*/
protected abstract void doBackStroke(int sleepTime);
/**
* perform Breaststroke
*
* @param sleepTime
*/
protected abstract void doBreastStroke(int sleepTime);
/**
* perform Freestyle
*
* @param sleepTime
*/
protected abstract void doFreeStyle(int sleepTime);
public void doSwimmingStyle(String style) {
switch (style) {
case "Freestyle":
doFreeStyle(personalBest);
break;
case "Backstroke":
doBackStroke(personalBest);
break;
case "Butterfly Stroke":
doButterflyStroke(personalBest);
break;
case "Breaststroke":
doBreastStroke(personalBest);
break;
}
this.swimLane.getTouchPad().startTimer(); // start the timer when swimmer starts to swim
}
void updatePosition(int sleepTime, double factor) {
final int swimTime = (int) (sleepTime * factor);
new Thread(new Runnable() {
@Override
public void run() {
try {
lock.lock(); // lock the thread
int endPosition = currentPosition + swimLane.getLength(); // length of the lane from staring to end
while (currentPosition <= endPosition) {
currentPosition++;
Thread.sleep(swimTime);
condition.signal();
condition.await();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
lock.unlock(); // give up the lock from the thread
}
}
}).start();
}
public int getPersonalBest() {
return this.personalBest;
}
public void setCurrentPosition(int currentPosition) {
this.currentPosition = currentPosition;
}
public int getCurrentPosition() {
return this.currentPosition;
}
public void setSwimLane(SwimLane swimLane) {
this.swimLane = swimLane;
}
/**
* When swimmer finish the event swimmer should touch the touch pad. This
* method achieve that task
*
* @param swimmingEvent
* @return swimmer's finishing time
*/
public double touchTheTouchPad(SwimmingEvent swimmingEvent) {
return this.swimLane.getTouchPad().notifyScoreBoard(this,
swimmingEvent);
}
public void slowDown() throws InterruptedException {
this.lock.lock();
}
public void speedUp() throws InterruptedException {
this.condition.awaitNanos(500);
}
public void giveUp() {
this.lock.unlock();
}
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ScoreBoard implements Serializable {
Queue<Double> time;
Queue<Swimmer> swimmers;
Map<Swimmer, Double> finalResults;
private int numberOfRows;
private final Lock lock;
private final Condition condition;
public ScoreBoard() {
time = new LinkedList<>();
swimmers = new LinkedList<>();
finalResults = new LinkedHashMap<>();
lock = new ReentrantLock();
condition = lock.newCondition();
}
/**
* This method will add swimmer and timeTaken to the list
*
* @param swimmer swimmer who completed the event
* @param timeTaken time took for swimmer to complete the event
*/
public void updateList(Swimmer swimmer, double timeTaken) {
time.add(timeTaken);
swimmers.add(swimmer);
}
/**
* This method will start the process of the Score Board
*/
public void powerUp() {
new Thread(new Runnable() {
@Override
public void run() {
try {
// swimmer will take more than 4s to complete a game
Thread.sleep(4000);
lock.lock();
while (swimmers.size() != numberOfRows) {
// wait this thread untill this is awoken
Thread.sleep(100);
}
comapreTimes();
condition.signal();
} catch (InterruptedException ex) {
ex.printStackTrace();
} finally {
lock.unlock();
}
}
}).start();
}
/**
* This method will set the number of rows in Score Board
*
* @param numberOfRows number of rows that should be in Score Board
*/
public void setNumberOfRows(int numberOfRows) {
this.numberOfRows = numberOfRows;
}
/**
* This method will pass the lock to the thread inside the powerUp method
*/
public void lockTheScoreBoard() {
this.lock.lock();
}
public void unlockTheScoreBoard() {
this.lock.unlock();
}
public void awaitScoreBoard() throws InterruptedException {
this.condition.await();
}
void comapreTimes() {
for (Swimmer s : swimmers) {
this.finalResults.put(s, time.poll());
// need to do something more in here
}
}
public Map<Swimmer, Double> getFinalResults() {
new Thread(new Runnable() {
@Override
public void run() {
try {
lock.lock();
condition.await();
} catch (InterruptedException ex) {
ex.printStackTrace();
} finally {
lock.unlock();
}
}
}).start();
return this.finalResults;
}
最佳答案
首先,swmr.giveUp();应该从final移到为InterruptedExcveption执行的部分。
为什么每个小火都放弃了?只有宽松的人应该这样做。
检查是否可以解决该问题。也许是这样。
您知道,我认为设置JLabel的文本没有问题,它只是强制重画窗口,所以,这就是为什么您在开始时会看到所有游泳者的原因,swmr.giveUp();重设职位开始吗?
如果是这种情况,那么这就是为什么您在设置JLabel文本后在beginnig上看到所有这些位置的原因,因为它们已经具有该位置,但是如果不重绘,您将看不到它们的新位置。
关于java - 如何停止崩溃的JLabels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34237733/
我有一段代码看起来像这样: void update_clock(uint8_t *time_array) { time_t time = *((time_t *) &time_array[0]
应用程序崩溃了 :( 请帮助我.. 在这方面失败了。我找不到错误?该应用程序可以连接到 iTunesConnect 但它会出错。 谁能根据下面的崩溃报告判断问题出在哪里? share_with_app
小二是新来的实习生,作为技术 leader,我给他安排了一个非常简单的练手任务,把前端 markdown 编辑器里上传的图片保存到服务器端,结果他真的就把图片直接保存到了服务器上,这下可把我气坏了,就
我正在创建一个函数,它将目录路径作为参数传递,或者如果它留空,则提示用户输入。 我已经设置了我的 PATH_MAX=100 和 if 语句来检查 if ((strlen(folder path) +
我已将“arial.ttf”文件(从我的/Windows/Fonts 文件夹中获取)加载到内存中,但是将其传递到 FT_New_Memory_Face 时会崩溃(在 FT_Open_Face 中的某处
我正在尝试在我的计算机上的两个控制台之间进行 rtsp 流。 在控制台 1 上,我有: ffmpeg -rtbufsize 100M -re -f dshow -s 320x240 -i video=
我正在尝试使用 scio_beast在一个项目中。我知道它还没有完成,但这并不重要。我已经设法让它工作得很好。 我现在正在尝试连接到 CloudFlare 后面的服务器,我知道我需要 SNI 才能工作
我有一个带有关联宏的下拉列表,如下所示: Sub Drop() If Range("Hidden1!A1") = "1" Then Sheets("Sheet1").Se
我对 bash 很陌生。我要做的就是运行这个nvvp -vm /usr/lib64/jvm/jre-1.8.0/bin/java无需记住最后的路径。我认为 instafix 就是这样做...... n
我在 Windows 上使用 XAMPP 已经两年左右了,它运行完美,没有崩溃没有问题。 (直到四个月前。) 大约四个月前,我们将服务器/系统升级到了更快的规范。 这是旧规范的内容 - Windows
我面临着一个非常烦人的 android 崩溃,它发生在大约 1% 的 PRODUCTION session 中,应用程序始终在后台运行。 Fatal Exception: android.app.Re
尝试使用下面的函数: public void createObjectType() { try { mCloudDB.createObjectType(ObjectTypeIn
由于我正在进行的一个项目,我在 CF11 管理员中弄乱了类路径,我设法使服务器崩溃,以至于我唯一得到的是一个漂亮的蓝屏和 500 错误.我已经检查了日志,我会把我能做的贴在帖子的底部,但我希望有人会启
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 10 个月前关闭。 Improve
我最近从 xcode 3.x 更新到 4.2,当我在 4.2 中运行应用程序时,我遇到了核心数据问题。我还更新到了 iOS 5,所以问题可能就在那里,我不太确定。 这些应用程序在 3.x 中运行良好,
我是一个相对较新的 iPhone 应用程序开发人员,所以我的知识有点粗略,所以如果这是一个微不足道的问题,请原谅我。 我有一个导航应用程序,它通过在navigationController对象上调用p
if ([MFMailComposeViewController canSendMail]) { MFMailComposeViewController *mailViewController
你能帮我吗? 我正在设置 UILocalNotification,当我尝试设置其 userInfo 字典时,它崩溃了。 fetchedObjects 包含 88 个对象。 这是代码: NSDi
为什么我的代码中突然出现 NSFastEnumeration Mutation Handler 崩溃。我很茫然为什么会突然出现这个崩溃以及如何解决它。 最佳答案 崩溃错误: **** 由于未捕获的异常
当我从表中删除行时,我的应用程序崩溃了。这是我检测到错误和堆栈跟踪的来源。谢谢! //delete row from database - (void)tableView:(UITableView *
我是一名优秀的程序员,十分优秀!