gpt4 book ai didi

JavaRx 基于响应式编程构建 GUI

转载 作者:行者123 更新时间:2023-12-04 03:34:05 27 4
gpt4 key购买 nike

大家好,我是响应式编程的新手,我只想知道如何使用这段代码实现和处理新按钮。如果您知道答案,请写下正确的代码。所以我要问的是用第二个按钮来实现这段代码,它可以做任何你想做的事。我只需要看到用第二个按钮实现的这段代码。

package pcd.lab08.rx;

import javax.swing.*;

import io.reactivex.rxjava3.core.Scheduler;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.subjects.PublishSubject;

import java.awt.event.*;
import java.util.concurrent.TimeUnit;

public class Test04_swing_pubsub {

static class MyFrame extends JFrame {

private PublishSubject<Integer> stream;

public MyFrame(PublishSubject<Integer> stream){
super("Swing + RxJava");
this.stream = stream;
setSize(150,60);
setVisible(true);
JButton button = new JButton("Press me");
button.addActionListener((ActionEvent ev) -> {
stream.onNext(1);
});
getContentPane().add(button);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev){
System.exit(-1);
}
});
}
}

static public void main(String[] args){

PublishSubject<Integer> clickStream = PublishSubject.create();

SwingUtilities.invokeLater(()->{
new MyFrame(clickStream);
});

clickStream
.observeOn(Schedulers.computation())
.subscribe((v) -> {
System.out.println(Thread.currentThread().getName() + "click: "+System.currentTimeMillis());
});

clickStream
.buffer(clickStream.throttleWithTimeout(250, TimeUnit.MILLISECONDS))
.map(xs -> xs.size())
.filter((v) -> v >= 2)
.subscribe((v) -> {
System.out.println(Thread.currentThread().getName() + "Multi-click: "+v);
});

}

}

最佳答案

好吧,我已经解决了这个问题很简单,我们必须使用过滤器。

package pcd.lab08.rx;

import javax.swing.*;

import io.reactivex.rxjava3.core.Scheduler;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.subjects.PublishSubject;

import java.awt.event.*;
import java.util.concurrent.TimeUnit;

public class Test04_swing_pubsub {

static class MyFrame extends JFrame {

private PublishSubject<Integer> stream;

public MyFrame(PublishSubject<Integer> stream){
super("Swing + RxJava");
this.stream = stream;
setSize(150,60);
setVisible(true);

JButton button = new JButton("Press me");
button.addActionListener((ActionEvent ev) -> {
stream.onNext(1);
});

JButton button2 = new JButton("Button2");
button.addActionListener((ActionEvent ev) -> {
stream.onNext(2);
});

getContentPane().add(button);
getContentPane().add(button2);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev){
System.exit(-1);
}
});
}
}

static public void main(String[] args){

PublishSubject<Integer> clickStream = PublishSubject.create();

SwingUtilities.invokeLater(()->{
new MyFrame(clickStream);
});

clickStream
.observeOn(Schedulers.computation())
.subscribe((v) -> {
System.out.println(Thread.currentThread().getName() + "click: "+System.currentTimeMillis());
});

clickStream
.observeOn(Schedulers.computation())
.filter((v) -> v == 2)
.subscribe((v) -> {
System.out.println(Thread.currentThread().getName() + "i've pressed the button2": "+System.currentTimeMillis());
});

clickStream
.buffer(clickStream.throttleWithTimeout(250, TimeUnit.MILLISECONDS))
.map(xs -> xs.size())
.filter((v) -> v >= 2)
.subscribe((v) -> {
System.out.println(Thread.currentThread().getName() + "Multi-click: "+v);
});

}

}

关于JavaRx 基于响应式编程构建 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67285469/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com