如何从运行方法中获取价值?

时间:2019-12-20 14:04:42

标签: java methods parameters timer local

我有两个框架。第一个有一个按钮。通过单击它,将打开第二个框架并启动计时器。可以通过再次按下按钮或等待计时器将局部变量x增至3来关闭第二帧。我有几种方法。

  1. 按钮仅启动计时器,并且在计时器的run方法中,第二个框架已打开。当计时器将变量x增至3时,第二帧关闭。它工作正常。问题是我无法再次按下按钮来关闭框架。所以我尝试了以下方法。

  2. 按钮将打开第二个框架,第二个框架包含计时器的对象。现在,我可以再次单击按钮来关闭第二帧。但是,当计时器将x增至3时,它将不会关闭,因为第二帧现在已超出run方法,并且无法再访问局部变量x

这是我的问题。我只是不知道如何从run方法中获取x的值。 run方法不允许任何参数传递值。使用timerClass.x进行访问不会更改任何内容。这里是代码。

public class Main {

public static void main(String[] args) {

    FrameOne frameOne = new FrameOne ();
}}

///////////////////////////////////////     THE FIRST FRAME 

public class FrameOne extends JFrame implements ActionListener {

private FrameTwo frameTwo;
private JButton btn= new JButton();

///////////////////////    PROPERTIES OF THE FIRST FRAME

FrameOne(){

    btn.addActionListener(this);
    add(btn);
    setSize(400,400);
    setLocation(300, 250);
    setLayout(new FlowLayout());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}


///////////////////////     OPENS THE SECOND FRAME BY CLICKING THE BUTTON

public void actionPerformed(ActionEvent e) {                

    if (e.getSource() == btn && frameTwo == null) {

            frameTwo = new FrameTwo();  
}


///////////////////////     CLOSES THE SECOND FRAME BY CLICKING THE BUTTON AGAIN


    else if (e.getSource() == btn && frameTwo != null) {  

            frameTwo.dispatchEvent(new WindowEvent(frameTwo, WindowEvent.WINDOW_CLOSING));
            frameTwo = null;
    }}

}

///////////////////////////////////////     THE SECOND FRAME


public class FrameTwo extends JDialog {

TimerClass timerClass;

FrameTwo() {


    setSize(400,400);
    setLocation(900, 250);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setVisible(true);
    timerClass = new TimerClass();  // starts the timer


//////////////////////////     HERE I TRY TO LET THE TIMER CLOSE THE FRAME AT 3    ////////

    if(timerClass.x == 3) {this.dispatchEvent(
        new WindowEvent(
            this, WindowEvent.WINDOW_CLOSING));
               }}}

//////////////////       THE TIMER       
//////////////////       IT INCREMENTS x
//////////////////       WHEN x = 3 IT SHALL CLOSE THE FRAME 


public class TimerClass implements EventListener{

            public Timer timer = new Timer(true);
            public int x;

    TimerClass(){

        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                x++;
        }

    },0,1000);
}}

还建议我使用frameTwo.dispose();代替this.dispatchEvent命令。或使用frameTwo.dispatchEvent,依此类推。没有任何工作。我知道这是因为我对传递变量的值不够熟悉。请不要给我任何使用教程的障碍。我需要从本示例中了解它才能快速取得进展。这是我的学习方式。感谢您的同情和努力。

1 个答案:

答案 0 :(得分:0)

一个简单的解决方法是将timerClass.x == 3检查移入计时器类,如下所示:

FrameTwo类中,替换:

timerClass = new TimerClass();  // starts the timer

使用

timerClass = new TimerClass(()->this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)));

在类TimerClass中替换构造函数:

TimerClass(){

        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                x++;
        }

    },0,1000);

使用

TimerClass(Runnable runWhenThree) {    
            timer.scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
                    x++;
                    if(x == 3) {
                        runWhenThree.run();
                    }                       
                }    
            }, 0, 1000);
        }