Swing,多个屏幕,控制转移

4

我正在开发一个Swing应用程序。我正在使用两个屏幕。

1.从屏幕1启动的按钮将启动屏幕2。

伪代码:

ScreenA extends JFrame{

    onButtonClick(){
        Screen2.setVisible(true);
    }
    System.out.println("Hai");
}

Screen2 extends JFrame{
    onButtonClick{
         Hide this screen;
    }
}

现在的输出为:

  1. 屏幕2将被显示。
  2. “Hai”将被打印出来。

我的目标:只有当从屏幕2点击一个按钮并且屏幕2消失时,才想要显示“Hai”。

我该如何实现它?

我尝试在屏幕二中为buttonclicked设置一个标志。但程序只是通过条件然后继续执行下一行。我该如何控制它?


根据您的问题,解决方案是在Screen2.onButtonClick()中隐藏后打印“Hai”。如果有误,请详细说明您的问题。 - Sergey Grinev
不行。目标是将完全控制权转移到可见屏幕,并从转移控制时的点继续执行。 - Achilles
2个回答

4

希望这段代码中的注释可以帮助你解释事情。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TwoFrames 
{
    // Making our first JFrame.
    JFrame frame1 = new JFrame("FRAME 1");
    // Declaring our second JFrame.
    JFrame frame2 ;

    public void createAndDisplayGUI()
    {               
        // Used to close the JFrame graciously.
        frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);       

        // Used to position the JFrame at the middle of the screen.
        //frame1.setLocationRelativeTo(null);       

        // Use this instead for placing windows, as determined by the OS.
        frame1.setLocationByPlatform(true);     

        // Calling this method to create our frame2.
        makeNewFrame();

        // Button to show the second JFrame.
        JButton showButton = new JButton("SHOW NEW FRAME");
        showButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                // Checking if the frame2 is already visible
                // on the screen, if YES then we won't 
                // create a new frame, else a new frame 
                // will be created. This will prevent multiple
                // JFrame to be created at the click of this button.                
                if (!(frame2.isShowing()))
                {   
                    // If  you had already disposed it previously 
                    // by clicking the hide Button on the other frame
                    // then the click on this button will recreate
                    // a new frame to be displayed.
                    makeNewFrame();
                    frame2.setVisible(true);
                }
            }
        });

        // Adding the button to the South side of the frame1.
        frame1.add(showButton, BorderLayout.PAGE_END);
        frame1.pack();
        frame1.setVisible(true);
    }

    private void makeNewFrame()
    {       
        frame2 = new JFrame("FRAME 2");
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.setLocationByPlatform(true);

        // Creating a JButton to be shown on the JFrame.
        JButton hideButton = new JButton("HIDE FRAME");
        hideButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                // On the click of this button, frame2 will 
                // disappear and HAI will be displayed on the console.
                frame2.dispose();
                System.out.println("HAI");
            }
        });

        // Adding the button to the South side of the frame1.
        frame2.add(hideButton, BorderLayout.PAGE_END);
        frame2.pack();
    }

    public static void main(String... args)
    {
        /* Here we are Secheduling a JOB for 
         * Event Dispatcher Thread, since Swing
         * is not Thread Safe. This is used to place
         * the code which is responsible for 
         * creating and diaplaying your GUI.
         */
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                TwoFrames tf = new TwoFrames();
                tf.createAndDisplayGUI();
            }
        });
    }
}

1
尽管我不喜欢这种方法(2个框架)和全大写字符串,但是我必须为评论点赞。 :) 请注意,第二条评论应该是“//声明我们的第二个JFrame。” - Andrew Thompson
@AndrewThompson:啊哈,谢谢指出,发现得好:-)我太傻了,在那个地方使用那个注释:-) - nIcE cOw

4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接