JavaFX TextArea 到 ListView 集成
JavaFX TextArea to ListView intergration
- 正在开发一个计时器应用程序,该应用程序在 运行 时打印到控制台。
- 我想要所有相同的信息打印在应用程序的文本区域。
- 只会打印文本区当前的activity,不会打印全部。
- 尝试使用 ListView 而不是 TextArea,但无法弄清楚如何将其集成到应用程序中。有更简单的方法吗?
- 图像显示:箭头 2 "loggedMessages" = 这只存储最后一个 activity。箭头 3 是记录了所有活动的控制台。
我希望 loggedMessage 区域显示控制台捕获的所有活动。
代码如下:
Image of Timer app fields
public class Controller {
@FXML private AnchorPane container;
@FXML private Label title;
@FXML private TextArea inputMessage;
@FXML private TextArea loggedMessages;
private Timeline mTimeline;
private TimerAttempt mCurrentAttempt;
private StringProperty mTimerText;
public Controller(){
mTimerText = new SimpleStringProperty();
setTimerText(0);
}
public String getTimerText() {
return mTimerText.get();
}
public StringProperty timerTextProperty() {
return mTimerText;
}
public void setTimerText(String timerText) {
this.mTimerText.set(timerText);
}
public void setTimerText(int remainingSeconds){
int minutes = remainingSeconds / 60;
int seconds = remainingSeconds % 60;
setTimerText(String.format("%02d:%02d", minutes, seconds));
}
private void prepAttempt(AttemptKind kind ){
reset();
mCurrentAttempt = new TimerAttempt(kind, "");
addAttemptStyle(kind);
title.setText(kind.getTheDisplayName());
setTimerText(mCurrentAttempt.getRemainingSeconds());
mTimeline = new Timeline();
mTimeline.setCycleCount(kind.getAmountOfSeconds());
mTimeline.getKeyFrames().add(new KeyFrame(Duration.seconds(1), e ->{
mCurrentAttempt.tickDown();
setTimerText(mCurrentAttempt.getRemainingSeconds());
}));
mTimeline. setOnFinished(e -> {
saveCurrentAttempt();
mApplause.play();
prepAttempt(mCurrentAttempt.getTypeOfAttempt() == AttemptKind.STUDYING ?
AttemptKind.BREAK : AttemptKind.STUDYING);
});
}
private void saveCurrentAttempt() {
mCurrentAttempt.setMessage(inputMessage.getText());
mCurrentAttempt.save();
loggedMessages.setText(String.valueOf(mCurrentAttempt));
inputMessage.clear();
}
private void reset() {
clearAttemptStyles();
if (mTimeline != null && mTimeline.getStatus() == Animation.Status.RUNNING) {
mTimeline.stop();
}
}
private void addAttemptStyle(AttemptKind kind) {
container.getStyleClass().add(kind.toString().toLowerCase());
}
private void clearAttemptStyles(){
container.getStyleClass().remove("playing");
for (AttemptKind kind : AttemptKind.values()){
container.getStyleClass().remove(kind.toString().toLowerCase());
}
}
//--------------------定时器Class---------------- -----
public class TimerAttempt {
private String mMessage;
private int mRemainingSeconds;
private AttemptKind mTypeOfAttempt;
public TimerAttempt(AttemptKind kind, String inputMessage) {
mTypeOfAttempt = kind;
mMessage = inputMessage;
mRemainingSeconds = kind.getAmountOfSeconds();
}
public AttemptKind getTypeOfAttempt() {
return mTypeOfAttempt;
}
public String getMessage() {
return mMessage;
}
public int getRemainingSeconds() {
return mRemainingSeconds;
}
public void setMessage(String message) {
mMessage = message;
}
public void tickDown() {
mRemainingSeconds --;
}
public void save() {
System.out.printf("LOGGED: %s %n ", this);
}
@Override
public String toString() {
Date date = new Date();
SimpleDateFormat timeStamp = new SimpleDateFormat("MM/dd/yyyy h:mm;ss a");
String formattedDate = timeStamp.format(date);
return formattedDate +
"\n Working on: " + mMessage +
"\n During : " + mTypeOfAttempt + '\n';
}
}
替换
loggedMessages.setText(String.valueOf(mCurrentAttempt));
和
loggedMessages.appendText(String.valueOf(mCurrentAttempt));
TextArea
方法 appendText(...)
将文本附加到文本区域。 TextArea
方法setText(...)
设置文本区域的文字
- 正在开发一个计时器应用程序,该应用程序在 运行 时打印到控制台。
- 我想要所有相同的信息打印在应用程序的文本区域。
- 只会打印文本区当前的activity,不会打印全部。
- 尝试使用 ListView 而不是 TextArea,但无法弄清楚如何将其集成到应用程序中。有更简单的方法吗?
- 图像显示:箭头 2 "loggedMessages" = 这只存储最后一个 activity。箭头 3 是记录了所有活动的控制台。 我希望 loggedMessage 区域显示控制台捕获的所有活动。
代码如下:
Image of Timer app fields
public class Controller {
@FXML private AnchorPane container;
@FXML private Label title;
@FXML private TextArea inputMessage;
@FXML private TextArea loggedMessages;
private Timeline mTimeline;
private TimerAttempt mCurrentAttempt;
private StringProperty mTimerText;
public Controller(){
mTimerText = new SimpleStringProperty();
setTimerText(0);
}
public String getTimerText() {
return mTimerText.get();
}
public StringProperty timerTextProperty() {
return mTimerText;
}
public void setTimerText(String timerText) {
this.mTimerText.set(timerText);
}
public void setTimerText(int remainingSeconds){
int minutes = remainingSeconds / 60;
int seconds = remainingSeconds % 60;
setTimerText(String.format("%02d:%02d", minutes, seconds));
}
private void prepAttempt(AttemptKind kind ){
reset();
mCurrentAttempt = new TimerAttempt(kind, "");
addAttemptStyle(kind);
title.setText(kind.getTheDisplayName());
setTimerText(mCurrentAttempt.getRemainingSeconds());
mTimeline = new Timeline();
mTimeline.setCycleCount(kind.getAmountOfSeconds());
mTimeline.getKeyFrames().add(new KeyFrame(Duration.seconds(1), e ->{
mCurrentAttempt.tickDown();
setTimerText(mCurrentAttempt.getRemainingSeconds());
}));
mTimeline. setOnFinished(e -> {
saveCurrentAttempt();
mApplause.play();
prepAttempt(mCurrentAttempt.getTypeOfAttempt() == AttemptKind.STUDYING ?
AttemptKind.BREAK : AttemptKind.STUDYING);
});
}
private void saveCurrentAttempt() {
mCurrentAttempt.setMessage(inputMessage.getText());
mCurrentAttempt.save();
loggedMessages.setText(String.valueOf(mCurrentAttempt));
inputMessage.clear();
}
private void reset() {
clearAttemptStyles();
if (mTimeline != null && mTimeline.getStatus() == Animation.Status.RUNNING) {
mTimeline.stop();
}
}
private void addAttemptStyle(AttemptKind kind) {
container.getStyleClass().add(kind.toString().toLowerCase());
}
private void clearAttemptStyles(){
container.getStyleClass().remove("playing");
for (AttemptKind kind : AttemptKind.values()){
container.getStyleClass().remove(kind.toString().toLowerCase());
}
}
//--------------------定时器Class---------------- -----
public class TimerAttempt {
private String mMessage;
private int mRemainingSeconds;
private AttemptKind mTypeOfAttempt;
public TimerAttempt(AttemptKind kind, String inputMessage) {
mTypeOfAttempt = kind;
mMessage = inputMessage;
mRemainingSeconds = kind.getAmountOfSeconds();
}
public AttemptKind getTypeOfAttempt() {
return mTypeOfAttempt;
}
public String getMessage() {
return mMessage;
}
public int getRemainingSeconds() {
return mRemainingSeconds;
}
public void setMessage(String message) {
mMessage = message;
}
public void tickDown() {
mRemainingSeconds --;
}
public void save() {
System.out.printf("LOGGED: %s %n ", this);
}
@Override
public String toString() {
Date date = new Date();
SimpleDateFormat timeStamp = new SimpleDateFormat("MM/dd/yyyy h:mm;ss a");
String formattedDate = timeStamp.format(date);
return formattedDate +
"\n Working on: " + mMessage +
"\n During : " + mTypeOfAttempt + '\n';
}
}
替换
loggedMessages.setText(String.valueOf(mCurrentAttempt));
和
loggedMessages.appendText(String.valueOf(mCurrentAttempt));
TextArea
方法 appendText(...)
将文本附加到文本区域。 TextArea
方法setText(...)
设置文本区域的文字