JavaFX学习笔记-拖拽进度条Slider;使用自动任务更新组件实现自动滑动进度条
程序员文章站
2022-03-30 21:13:12
...
Pane pane = new Pane();
Slider slider = new Slider(0, 100, 50);
slider.setPrefWidth(300);
slider.setLayoutX(100);
slider.setLayoutY(300);
// 进度条下面的刻度
slider.setShowTickMarks(true);
// 进度条下面的值
slider.setShowTickLabels(true);
// 刻度值之间的间距值
slider.setMajorTickUnit(10);
// 当前位置
slider.setValue(20);
// 方向改为竖的样式
// slider.setOrientation(Orientation.VERTICAL);
slider.valueProperty().addListener(new ChangeListener<Number>()
{
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue)
{
System.out.println("正在拖动进度条, 当前值: " + newValue);
}
});
slider.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent event)
{
if (event.getClickCount() == 1)
{
System.out.println("鼠标点击位置的值是: " + slider.getValue());
}
}
});
pane.getChildren().add(slider);
// 使用线程自动滚动进度条
new Thread(new Runnable()
{
int t = 0;
@Override
public void run()
{
for (int i = 0; i < 100; ++i)
{
t = i;
Platform.runLater(new Runnable()
{
@Override
public void run()
{
slider.setValue(t);
}
});
try
{
Thread.sleep(100);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
Thread.sleep(100);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在线程里面更新组件的方法:
Pane pane = new Pane();
Slider slider = new Slider(0, 100, 50);
slider.setPrefWidth(300);
slider.setLayoutX(100);
slider.setLayoutY(300);
// 进度条下面的刻度
slider.setShowTickMarks(true);
// 进度条下面的值
slider.setShowTickLabels(true);
// 刻度值之间的间距值
slider.setMajorTickUnit(10);
// 当前位置
slider.setValue(20);
// 方向改为竖的样式
// slider.setOrientation(Orientation.VERTICAL);
My m = new My(slider);
m.setDelay(Duration.millis(0)); // 延迟多久运行
m.setPeriod(Duration.millis(1000)); // 多久运行一次
m.start();
slider.valueProperty().addListener(new ChangeListener<Number>()
{
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue)
{
System.out.println("正在拖动进度条, 当前值: " + newValue);
}
});
slider.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent event)
{
if (event.getClickCount() == 1)
{
System.out.println("鼠标点击位置的值是: " + slider.getValue());
}
}
});
pane.getChildren().add(slider);
new Thread(new Runnable()
{
int t = 0;
@Override
public void run()
{
for (int i = 0; i < 100; ++i)
{
t = i;
Platform.runLater(new Runnable()
{
@Override
public void run()
{
slider.setValue(t);
}
});
try
{
Thread.sleep(100);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
Thread.sleep(100);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class My extends ScheduledService<Integer>
{
public int i;
public Slider s;
public My(Slider s)
{
this.s = s;
i = 0;
}
@Override
protected Task<Integer> createTask()
{
Task task = new Task<Integer>()
{
@Override
protected Integer call() throws Exception
{
return i = i + 1;
}
@Override
protected void updateValue(Integer value)
{
// super.updateValue(value);
s.setValue(value);
}
};
return task;
}
}
上一篇: JavaFX中TableView使用
下一篇: 动手实现编译器(二)——语法分析