单击按钮打开时,新JFrame将处于禁用状态

时间:2016-07-28 06:34:27

标签: java swing jframe

我陷入了非常不寻常的境地。我有一个班级" ScreenSizeSelector"它有一个方法' getSelectedScreenSize'。该方法的工作是创建一个UI,用户拖动UI和方法返回窗口大小。

现在我通过以下方式调用类的方法:

  1. 一个简单的类(非GUI)
  2. 在按钮上单击JFrame
  3. 在第一种情况下,它工作得非常好(即大小选择器窗口打开,用户拖动它,调整大小并返回窗口坐标)但在第二种情况下,窗口打开但在禁用模式下,用户无法在窗口上执行任何操作,甚至无法关闭窗口。

    以下是我正在使用的代码

    ScreenSizeSelector类:

    package screenrecorder;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.Border;
    
    
    class ScreenSizeSelector {
    
        private JFrame sizeSelectorWindow;
        private JButton btnOk;
        private Border emptyBorder;
        private Rectangle screenArea = null;
        private static Object lock = new Object();
    
    
    
        public Rectangle getSelectedScreenSize(){
    
            screenSizeSelectorUI();
    
            Thread t = new Thread() {
                public void run() {
                    synchronized(lock) {
                        while (sizeSelectorWindow.isVisible())
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                    }
                }
            };
    
            t.start();
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            return screenArea;
        }
    
    
        public void screenSizeSelectorUI() {
            emptyBorder = BorderFactory.createEmptyBorder();
    
            sizeSelectorWindow = new JFrame("Select screen area");
    
            btnOk = new JButton("Start");
            sizeSelectorWindow.setUndecorated(true);
            sizeSelectorWindow.getRootPane().setWindowDecorationStyle(3);
            sizeSelectorWindow.setBackground( new Color(0, 0, 0, 0) );
            sizeSelectorWindow.setSize(400,400);
    
            sizeSelectorWindow.addWindowListener(new WindowEventHandler());
            sizeSelectorWindow.setAlwaysOnTop(true);
            sizeSelectorWindow.setLocationRelativeTo(null);
    
    
            btnOk.setToolTipText("Click this button after deciding the screen area");
            btnOk.addActionListener(new ButtonEventHandler());
    
    
            JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
            buttonPanel.setBackground(new Color(0,0,0,0));
            buttonPanel.add(btnOk);
            sizeSelectorWindow.add(buttonPanel,BorderLayout.SOUTH);
    
            sizeSelectorWindow.setVisible(true);
            sizeSelectorWindow.setEnabled(true);
    
        }
    
    class ButtonEventHandler implements ActionListener {
    
    @Override
    public void actionPerformed(ActionEvent e) {
    
        int x = (int)(sizeSelectorWindow.getBounds().getX());
        int y = (int) (sizeSelectorWindow.getBounds().getY());
        int width = sizeSelectorWindow.getWidth();
        int height = sizeSelectorWindow.getHeight();
    
        screenArea = new Rectangle(x,y,width,height);
        sizeSelectorWindow.dispatchEvent(new WindowEvent(sizeSelectorWindow, WindowEvent.WINDOW_CLOSING));
    }
    
    
    }
    
    class WindowEventHandler implements WindowListener{
    
        @Override
        public void windowOpened(WindowEvent e) {
        }
    
        @Override
        public void windowClosing(WindowEvent e) {
    
            synchronized (lock) {
                sizeSelectorWindow.setVisible(false);
                lock.notify();
            }
        }
    
        @Override
        public void windowClosed(WindowEvent e) {
        }
    
        @Override
        public void windowIconified(WindowEvent e) {
            sizeSelectorWindow.setState(JFrame.NORMAL);
            Toolkit.getDefaultToolkit().beep();
        }
    
        @Override
        public void windowDeiconified(WindowEvent e) {}
    
        @Override
        public void windowActivated(WindowEvent e) {}
    
        @Override
        public void windowDeactivated(WindowEvent e) {}
    
    
        }
    
    }
    

    Test1课程:

    package screenrecorder;
    
    import java.awt.Rectangle;
    
    public class Test1{
    
        public static void main(String[] args){
            System.out.println(new ScreenSizeSelector().getSelectedScreenSize());
        }
    }
    

    Test2课程:

    package screenrecorder;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class Test2 extends JFrame{
    
        public Test2(){
    
            JButton btn = new JButton("Click ME");
            btn.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println(new ScreenSizeSelector().getSelectedScreenSize());
    
                }
            });
    
            getContentPane().add(btn);
            setSize(100,100);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        }
    
        public static void main(String[] args){
            new Test2();
        }
    
    }
    

    感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

单击该按钮时,操作侦听器将等待getSelectedScreenSize()函数返回。并且getSelectedScreenSize()函数正在等待screenSelectorUI()创建的第二个窗口不可见。 screenSelectorUI()会创建第二个窗口,但您可以设置如下颜色:

sizeSelectorWindow.setBackground( new Color(0, 0, 0, 0) );

如果你看一下颜色构造函数javadocs:

public Color(int r,
                int g,
                int b,
                int a)
     

使用指定的红色,绿色,蓝色和Alpha值创建sRGB颜色,范围为(0 - 255)。
  的参数:

     
      
  • r - 红色成分
  •   
  • g - 绿色成分
  •   
  • b - 蓝色组件
  •   
  • a - alpha组件
  •   

将alpha值设置为0,使其完全不可见。 (alpha值是透明度),第二个窗口是未修饰的,不会在关闭时退出,所以你根本不知道它在那里。

我不知道的是test1是如何工作的。 旁注:当我在Mac上尝试测试1时,它只显示按钮,我所能做的就是点击它。按钮将消失,但应用程序仍将运行。

这基本上是一个总猜测,但很多swing组件向操作系统发出请求,而不是命令。有点像说,“嘿,我可以把它调到400,400吗?”操作系统在技术上不必做你说的。我正在阅读How does Java handle multithreading?,它说多线程真的取决于操作系统。我有一种感觉,当screenSelectorUI()被自己调用时,它只是在某处弄乱了,但当它在某个按钮的线程内时,它会以某种方式正确。