覆盖X按钮上的动作侦听器

时间:2014-03-27 17:04:18

标签: java swing jframe jpanel jbutton

我有一个内部框架,其中有我的客户JPanel。在我的客户JPanel中,我添加了两个来自外部来源的面板。 其中一个具有关闭面板的按钮。但是当我点击它时,它没有任何效果。如何在按钮上创建动作侦听器?

public class MyInternalFrame extends JInternalFrame{

private PDFJPanel panel=null;


public MyInternalFrame(File file) {
     super("Test" + file.getName(), true, true, true, true);
     panel=new PDFJPanel(file);
     this.add(panel, BorderLayout.CENTER);
   }
}

我的客户是JPanel

    public class PDFJPanel extends JPanel {    
        private JPanel jpAnnotation=null;
        private JScrollPane scrollPane = new JScrollPane();
        private File thisFile=null;
        private PDFNotesBean bean=null;
        private CommentPanel commentPane=null; 

    public PDFJPanel(File file) {
        thisFile=file;
        getJPanel();
    }


     public void getJPanel() {                   
          this.setLayout(new BorderLayout());             
          this.add(getPDFNotesBean(), BorderLayout.CENTER);

          commentPane= bean.getCommentPanel();         
          bean.getCommentPanelNotes().getjcbHideComments().setVisible(false);    
          //this code can get the button
          bean.getCommentPanelNotes().getToolbar().getCloseButton();


          //Right size of the Panel
          JPanel rightPanel=new JPanel();
          rightPanel.setLayout(new BorderLayout());           
          rightPanel.setBackground(Color.GREEN);                

          jpAnnotation=new JPanel();                         
          JButton btnUnderline =new JButton(new ImageIcon ("../UnderlineIcon.gif"));
          btnUnderline.setSize(50, 260);
          btnUnderline.setAlignmentX(JButton.LEFT_ALIGNMENT);

          jpAnnotation.add(btnUnderline);         

          rightPanel.add(jpAnnotation, BorderLayout.NORTH ); 
          rightPanel.add((Component) commentPane, BorderLayout.CENTER );
          this.add(rightPanel,  BorderLayout.EAST );
        }
   }

1 个答案:

答案 0 :(得分:0)

我会在黑暗中拍摄。

jpAnnotation.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
        JFrame myInternalFrame= (JFrame)SwingUtilities.getWindowAncestor(this);
        myInternalFrame.remove(PDFJPanel.this);
        myInternalFrame.invalidate();
        myInternalFrame.validate();
        myInternalFrame.repaint();
    }
});