簡體   English   中英

Mouslistener引起問題

[英]Mouslistener causing problems

我已經在代碼中創建了一個實現鼠標偵聽器的GoInvisible類,我嘗試使用按下的鼠標和釋放的鼠標的方法使框架透明,然后在按下並釋放按鈕時恢復正常在框架上。 我在內部類中調用這些方法,該類實現了處理按鈕事件的動作偵聽器,但是由於某些原因,當我運行應用程序時,框架從不顯示。

這是框架代碼;

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

這是組件類;

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

這是鼠標偵聽器類

 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. 您正在創建的新實例FNAFrameTextFrameGoInvisible處理程序,它有那些實際上是在屏幕上的實例沒有任何關系!

相反,您應該監視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

您可能希望通讀Java TM編程語言的代碼約定 ,這將使人們更容易閱讀您的代碼,並使您閱讀其他代碼更容易

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM