Mouslistener 造成问题

Mouslistener causing problems

我在我的代码中创建了一个 GoInvisible class,它实现了一个鼠标侦听器,我尝试使用鼠标按下和鼠标释放的方法让我的框架变得透明,然后 return 正常按下和释放框架上的按钮。我在内部 class 中调用这些方法,它实现了一个处理按钮事件的动作侦听器,但由于某种原因,当我 运行 应用程序框架从未出现时。

这是框架代码;

public class FNAFrame extends JFrame {

public FNAFrame()
{
    super ("FNA Comments Generator");
    setLayout(new BorderLayout());

    setResizable(false);
    TextFrame comps = new TextFrame();
    add(comps);
    pack();
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


/**
 * @param args the command line arguments
 */
  public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                // 
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) 
            {
                ex.printStackTrace();
            }


            new FNAFrame();

        }
    });
  }  
} // end of class FNA Frame

这是组件 class;

public class TextFrame extends JPanel 
{
    private JButton Go_Shadow;

public TextFrame()
{
    super(new GridBagLayout());

    setPreferredSize(new Dimension(300,200));
    setBackground(Color.white);

    init();  
   } // end of class constructor

    private void init()
    { 
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10,10,10,10);

        // button to display date in textarea
        Go_Shadow = new JButton("Shadow");
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.WEST;
        add(Go_Shadow, gbc);


        // adding listeners to components
        // registering all components with their respective listeners
        CompHandler compHandler = new CompHandler();
        Go_Shadow.addActionListener(compHandler);
    }

    // class to handle text fields
    private class CompHandler implements ActionListener
    {    
        private MouseEvent me;

        @Override
        public void actionPerformed(ActionEvent e)  
        {
            Object button_command = e.getActionCommand();



                if (button_command.equals("Go_Shadow"))
                { 
                    GoInvisible invisy = new GoInvisible();

                    invisy.mousePressed(me);
                    invisy.mouseReleased(me);    
                }

        }
    } // end component handler class 
} // end of TextFrame class

这里是鼠标监听class

 public class GoInvisible implements MouseListener {


 FNAFrame Parentpane = new FNAFrame();
 TextFrame compPanel = new TextFrame();


@Override
public void mouseClicked(MouseEvent e) {

    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void mousePressed(MouseEvent e) {
    Parentpane.setUndecorated(true);
    Parentpane.setOpacity(0.5f);
    compPanel.setOpaque(true);
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void mouseReleased(MouseEvent e) {
    Parentpane.setUndecorated(false);
    compPanel.setOpaque(true);
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void mouseEntered(MouseEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void mouseExited(MouseEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}   
}

有一系列的问题...

  1. 您需要从您的方法中删除 throw new UnsupportedOperationException("Not supported yet.");,这会导致问题并阻止您的代码执行
  2. 你的按钮的actionCommand不是"Go_Shadow",它将是按钮的文本,除非你另外指定。
  3. 您不应该使用 MouseListenerActionListener 添加一个,而应该监视 ButtonModel
  4. 的状态
  5. 您正在 GoInvisible 处理程序中创建 FNAFrameTextFrame 的新实例,它们与实际在屏幕上的那些实例没有关系!

相反,您应该监视 ButtonModel 的状态,例如...

Go_Shadow.getModel().addChangeListener(new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent e) {
        ButtonModel model = (ButtonModel) e.getSource();
        JFrame window = (JFrame) SwingUtilities.getWindowAncestor(TextFrame.this);
        if (model.isArmed() && model.isPressed()) {
            window.setUndecorated(true);
            window.setOpacity(0.5f);
            setOpaque(false);
        } else if (model.isArmed() && !model.isPressed()) {
            setOpaque(true);
            window.setOpacity(1f);
            window.setUndecorated(false);
        }
    }
});

但是,您仍然会发现这会导致问题,因为框架的边框状态一旦显示就无法更改

更好的解决方案可能是使用 JToggleButton,例如

public class TextFrame extends JPanel {

    private JToggleButton Go_Shadow;

    public TextFrame() {
        super(new GridBagLayout());

        setPreferredSize(new Dimension(300, 200));
        setBackground(Color.white);

        init();
    } // end of class constructor

    private void init() {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);

        // button to display date in textarea
        Go_Shadow = new JToggleButton("Shadow");
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.WEST;
        add(Go_Shadow, gbc);

        // adding listeners to components
        // registering all components with their respective listeners
        Go_Shadow.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame window = (JFrame) SwingUtilities.getWindowAncestor(TextFrame.this);
                Point location = window.getLocation();
                if (Go_Shadow.isSelected()) {
                    window.dispose();
                    window.setUndecorated(true);
                    window.setOpacity(0.5f);
                    setOpaque(false);
                } else {
                    window.dispose();
                    window.setOpacity(1f);
                    window.setUndecorated(false);
                    setOpaque(true);
                }
                window.setLocation(location);
                window.setVisible(true);
            }
        });

    }

} // end of TextFrame class

您可能希望通读 Code Conventions for the Java TM Programming Language,这将使人们更容易阅读您的代码,您也可以更轻松地阅读其他人