Java 让两个jframe相互通信
我正在写一个程序来测量给定时间内的cps。 我希望将程序拆分为两个窗口:Java 让两个jframe相互通信,java,swing,Java,Swing,我正在写一个程序来测量给定时间内的cps。 我希望将程序拆分为两个窗口: 一个,您可以设置时间,然后启动另一个窗口 一种是,有一个给定时间的计时器运行,并有一个按钮尽可能频繁地点击 计时器过期后,我希望结果显示在第一个窗口中 我的问题是我不知道如何让第二个窗口告诉第一个窗口计算出的cps。作为一个解决方案,我考虑过协同程序,但我不知道它们在Java中是如何工作的 提前谢谢你对我的帮助 很抱歉向您展示了整个代码,但我认为有必要提供完整的详细信息以帮助您解决此问题 我的主要班级: public
- 一个,您可以设置时间,然后启动另一个窗口
- 一种是,有一个给定时间的计时器运行,并有一个按钮尽可能频繁地点击
public class Main {
public static void main(String[] args){
CPS test = new CPS();
}
}
我的第一节课:(第一个窗口)
第二个类(第二个窗口):
要从第二帧返回值,请使用
JDialog
而不是JFrame
请注意代码中的其他注释更改:
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JProgressBar;
import javax.swing.Timer;
public class MCPS {
JDialog frame;
JProgressBar timeProgress;
int stateTimeProgress, amountClicked, duration;
double cps; //use double to avoid rounding error
JButton button;
Container pane;
boolean inGame, isMeasured;
Timer timer;
private static int CYCLE = 1000;
public MCPS(int time){
cps = 0;
stateTimeProgress = 0;
duration = time;
frame = new JDialog();
//make JDialog modal
frame. setModalityType(JDialog.ModalityType.DOCUMENT_MODAL);
initLogic();
initFrame();
initProgressBar(duration);
initButton();
pane = new Container();
pane.setLayout(new FlowLayout());
init();
}
private void initProgressBar(int time) {
timeProgress = new JProgressBar(0, time);
timeProgress.setValue(0);
}
private void initLogic() {
inGame = false;
amountClicked = 0;
isMeasured = false;
}
private void startTimer(int time) {
//use swing timer to interact with gui
timer = new Timer(CYCLE, e->{
stateTimeProgress++;
timeProgress.setValue(stateTimeProgress);
if(stateTimeProgress == time){
timer.stop();
stopGame();
}
});
timer.setRepeats(true);
timer.start();
}
private void stopGame() {
inGame = false;
cps = (double)amountClicked / duration;
frame.dispose();
}
private boolean isInGame() {
return inGame;
}
private void initButton() {
button = new JButton("Start");
//use action listener rather than mouse listener on buttons
button.addActionListener(e->{
if(isInGame()){
amountClicked++;
}else if(isMeasured == false) {
button.setText("Click me!");
startGame();
}
});
}
private void startGame() {
inGame = true;
isMeasured = true;
startTimer(duration);
}
private void initFrame() {
frame.setResizable(true);
frame.setSize(350, 150);
frame.setLocationRelativeTo(null);
}
private void init() {
pane.add(button);
pane.add(timeProgress);
frame.add(pane);
frame.setVisible(true);
}
double getCps(){
return cps;
}
}
要从MCPS
获取cps
的值,请将initButton()
修改为:
private void initButton() {
button = new JButton("Start");
button.addActionListener(e->{
if(duration != 0){
MCPS measure = new MCPS(duration);
System.out.println("CPS is: "+measure.getCps());
}
});
checkDuration(duration);
}
如果您想在两个Java对象之间传递数据,只需添加一个要调用的方法:
setClicksPerSecond()
。这不起作用,因为MCPS类不知道已在Main中实例化的对象,因此我无法调用该方法Swing应用程序应该有一个顶级容器(即JFrame
)。这就是为什么存在JDialog
类,所以您可以打开第二个窗口。正确的解决方案是让MCP知道另一个对象。要么在构造函数中注入类,要么在MCPS上添加一个“listener”方法,这样您就可以在创建后注入它@JGStyleSSBU
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JProgressBar;
import javax.swing.Timer;
public class MCPS {
JDialog frame;
JProgressBar timeProgress;
int stateTimeProgress, amountClicked, duration;
double cps; //use double to avoid rounding error
JButton button;
Container pane;
boolean inGame, isMeasured;
Timer timer;
private static int CYCLE = 1000;
public MCPS(int time){
cps = 0;
stateTimeProgress = 0;
duration = time;
frame = new JDialog();
//make JDialog modal
frame. setModalityType(JDialog.ModalityType.DOCUMENT_MODAL);
initLogic();
initFrame();
initProgressBar(duration);
initButton();
pane = new Container();
pane.setLayout(new FlowLayout());
init();
}
private void initProgressBar(int time) {
timeProgress = new JProgressBar(0, time);
timeProgress.setValue(0);
}
private void initLogic() {
inGame = false;
amountClicked = 0;
isMeasured = false;
}
private void startTimer(int time) {
//use swing timer to interact with gui
timer = new Timer(CYCLE, e->{
stateTimeProgress++;
timeProgress.setValue(stateTimeProgress);
if(stateTimeProgress == time){
timer.stop();
stopGame();
}
});
timer.setRepeats(true);
timer.start();
}
private void stopGame() {
inGame = false;
cps = (double)amountClicked / duration;
frame.dispose();
}
private boolean isInGame() {
return inGame;
}
private void initButton() {
button = new JButton("Start");
//use action listener rather than mouse listener on buttons
button.addActionListener(e->{
if(isInGame()){
amountClicked++;
}else if(isMeasured == false) {
button.setText("Click me!");
startGame();
}
});
}
private void startGame() {
inGame = true;
isMeasured = true;
startTimer(duration);
}
private void initFrame() {
frame.setResizable(true);
frame.setSize(350, 150);
frame.setLocationRelativeTo(null);
}
private void init() {
pane.add(button);
pane.add(timeProgress);
frame.add(pane);
frame.setVisible(true);
}
double getCps(){
return cps;
}
}
private void initButton() {
button = new JButton("Start");
button.addActionListener(e->{
if(duration != 0){
MCPS measure = new MCPS(duration);
System.out.println("CPS is: "+measure.getCps());
}
});
checkDuration(duration);
}