为什么我的mouselistener不能用于我的JPanel?

时间:2015-04-04 03:01:25

标签: java swing mouselistener

我有一个JFrame设置,它拥有一个JPanel,其中包含一个包含JTextArea的JScrollPanes的GridView。我要做的是基本上放大在JFrame中点击的组件(删除当前JFrame / JPanel的所有组件,只添加被点击的组件)。出于某种原因,它没有注册点击。

这是我拥有的

String wikiReturn = wikipediaSearch("Miami");
    String googleReturn = googleSearch("Miami");
    JFrame.setDefaultLookAndFeelDecorated(true);
    final JFrame frame = new JFrame("GridLayout Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JPanel panel = new JPanel();
    /*
     * Starting Grid Design Here
     * 
     */
    panel.setLayout(new GridLayout(2, 2));
    JTextArea wiki = new JTextArea();
    wiki.setText(wikiReturn);
    wiki.setLineWrap(true);
    wiki.setEditable(false);
    wiki.setLineWrap(true);
    wiki.setOpaque(false);
    wiki.setWrapStyleWord(true);
    JTextArea google = new JTextArea();
    google.setText(googleReturn);
    google.setLineWrap(true);
    google.setEditable(false);
    google.setLineWrap(true);
    google.setOpaque(false);
    google.setWrapStyleWord(true);
    JTextArea try1 = new JTextArea("Here I Am");
    JTextArea try2 = new JTextArea("Trial2");
    JScrollPane areaScrollPaneWiki = new JScrollPane(wiki);

    areaScrollPaneWiki.setVerticalScrollBarPolicy(
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    //areaScrollPane.setPreferredSize(new Dimension(250, 250));
    JScrollPane areaScrollPaneGoogle = new JScrollPane(google);
    areaScrollPaneGoogle.setVerticalScrollBarPolicy(
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    /*
     * Add everything to frame here
     */

    panel.add(areaScrollPaneGoogle);
    panel.add(areaScrollPaneWiki);
    panel.add(try2);
    panel.add(try1);
    panel.addMouseListener(new MouseAdapter(){
        @Override
        public void mouseClicked(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub
            Component toEnlarge = frame.getComponentAt(e.getX(), e.getY());
            panel.removeAll();
            panel.add(toEnlarge);
            System.out.println("Clicked at" + e.getX() + ", " + e.getY());
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }
    });
    frame.add(panel);
    frame.setVisible(true);

0 个答案:

没有答案