如何在mouseListener中删除和创建对象?

时间:2013-12-02 11:05:43

标签: java swing jscrollpane mouselistener

我想创建一个调用我的delete方法的鼠标事件,该方法将删除一个对象,一个JScrollPane及其所有组件,然后使用另一个方法调用创建另一个JScrollPane。

我做到了。如果我在我的鼠标事件之外运行它,如get_more()make_it()它可以工作,但如果我在mouseListener中执行它不起作用。我已经尝试了revalidaterepaint(),但没有尝试过。那么问题是什么?

    import javax.swing.JTextArea;
    import javax.swing.JLabel;
    import javax.swing.SwingConstants;

    import java.io.FileReader;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.Graphics;
    import javax.swing.JTextArea;
    import javax.swing.JButton;
    import java.awt.BorderLayout;
    import java.awt.Font;
    import java.awt.event.*;
    import java.awt.GridLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import javax.swing.JScrollPane;
    import java.io.File;
    import java.util.Scanner;
    import java.awt.Dimension;
    import java.io.LineNumberReader;
    public class getmessage 
    extends JFrame
    {
        getter g1;
        getmessage()
        {

            try{
                g1 = new getter("Ionescu Gabriel","Vladimir Alexandru");

                add(g1.scrollpane);
            }
            catch(Exception eos)
            {
                System.out.println("File not found");

            }

        }
        public static void main(String[] args)
        {
            getmessage rama = new getmessage();
            rama.setTitle("A Good Title");
            rama.setLocationRelativeTo(null);
            rama.setSize(400,400);
            rama.setVisible(true);
        }
    }
    class
    getter
    {
        StringBuilder text;
        String sir;
        JPanel panou_1;
        JTextArea[] text_fragmet;
        JPanel panou;
        JScrollPane scrollpane;
        JPanel[] panou_set;
        int  number_message = 20;
        Scanner scan;
        File message;
        String name;
        JLabel  more;
        String name_messager;
        JPanel panou2; 

        getter(String name,String name_messager)
        throws Exception
        {
            this.name = name;
            this.name_messager = name_messager;
            message = new File(""+name+"/"+name_messager);

            scan = new Scanner(message);
            text = new StringBuilder();
            panou = new JPanel();
            text_fragmet = new JTextArea[5000];
            panou_set = new JPanel[5000];
            sir = new String();

            panou2 = new JPanel();
            panou2.setLayout(new FlowLayout(FlowLayout.CENTER,20,10));
            more = new JLabel("Show More Messages");

            more.setBackground(Color.white);
            panou2.add(more);

            make_it();

            more.addMouseListener(new MouseListener(){
                public void mousePressed(MouseEvent moe)
                {

                }
                public void mouseExited(MouseEvent moe)
                {

                }
                public void mouseEntered(MouseEvent moe)
                {
                    Color c1  = new Color(20,20,20);
                    more.setBackground(Color.red);
                }
                public void mouseReleased(MouseEvent moe)
                {
                    more.setBackground(Color.white);

                }
                public void mouseClicked(MouseEvent moe)
                {
                                   get_more();
                    make_it();


                    scrollpane.repaint();
                    scrollpane.revalidate();
                    System.out.println("Clicked");
                }
            });


        //  get_more();\
        //  make_it() -> Works just fine


        }
    public void init()
    {
        panou_1 = new JPanel();
        panou_1.setLayout( new BorderLayout());
        panou_1.add(panou2 , BorderLayout.NORTH);
        panou_1.add(panou,BorderLayout.CENTER);


        scrollpane = new JScrollPane(panou_1);

        scrollpane.revalidate();
        scrollpane.repaint();
        panou_1.revalidate();
        panou_1.repaint();
        panou.repaint();
        panou.revalidate();
        scrollpane.setVisible(false);
        scrollpane.setVisible(true);

        scrollpane.setBackground(Color.white);
        scrollpane.setHorizontalScrollBar(null);

    }
    public void make_it()
    {


        if(message.exists())
        {
            int i = 0;
            while(scan.hasNext() && i < number_message)
            {
                sir = scan.nextLine();

                if(i % 2 == 0)
                {

                    text_fragmet[i] = new JTextArea("");
                    text_fragmet[i].setText(sir+": ");
                    text_fragmet[i].setFont(new Font("SansSerif",Font.BOLD, 12));

                    Font f1 = new Font("SansSerif",Font.PLAIN ,12);
                    panou_set[i] = new JPanel();
                    panou_set[i].setLayout(new FlowLayout(FlowLayout.RIGHT));
                    panou_set[i].add(text_fragmet[i]);
                    text_fragmet[i].setWrapStyleWord(true);
                    text_fragmet[i].setLineWrap(true);

                }
                else
                {
                    text_fragmet[i] = new JTextArea("");

                    text_fragmet[i].setText(sir);
                    text_fragmet[i].setFont(new Font("Angelic War",Font.PLAIN, 12));
                    panou_set[i] = new JPanel();
                    panou_set[i].setLayout(new FlowLayout(FlowLayout.LEFT));
                    panou_set[i].add(text_fragmet[i]);
                    text_fragmet[i].setWrapStyleWord(true);
                    text_fragmet[i].setLineWrap(true);
                    text_fragmet[i].setColumns(30);

                }

                panou.add(panou_set[i]);

                i++;
            }      panou.setLayout(new GridLayout( (i-2)/2 +1 ,2)); 
        scan.close();
        }
        else
        {
            System.out.println("File not found");
        }
        panou.setBackground(Color.white);


        init();
        scrollpane.revalidate();
        scrollpane.repaint();

    }
    public void get_more()

    {

        scrollpane = null;

        text_fragmet = null;
        panou = null;
        scrollpane = null;
        panou_set = null;

        scan = null;
        message = null;
        System.gc();

        message = new File(""+name+"/"+name_messager);
        try {
        scan = new Scanner(message);
        }
        catch(Exception oe)
        {

        }
        text = new StringBuilder();
        panou = new JPanel();
        text_fragmet = new JTextArea[5000];
        panou_set = new JPanel[5000];
        sir = new String();
        number_message = number_message + 20;

        panou2 = new JPanel();
        panou2.setLayout(new FlowLayout(FlowLayout.CENTER,20,10));
        more = new JLabel("Show More Messages");

        more.setBackground(Color.white);
        panou2.add(more);
        more.addMouseListener(new MouseListener(){
            public void mousePressed(MouseEvent moe)
            {

            }
            public void mouseExited(MouseEvent moe)
            {

            }
            public void mouseEntered(MouseEvent moe)
            {
                Color c1  = new Color(20,20,20);
                more.setBackground(Color.red);
            }
            public void mouseReleased(MouseEvent moe)
            {
                more.setBackground(Color.white);
            }
            public void mouseClicked(MouseEvent moe)
            {
                get_more();
                make_it();
                System.out.println("Clicked");

            }
        });
    }
    }

1 个答案:

答案 0 :(得分:0)

  

将删除一个对象,一个JScrollPane及其所有组件,然后用另一个方法调用创建另一个JScrollPane。

不要那样做。应尽可能避免在运行时删除和添加组件。

在这种情况下,您可以轻松地执行所需操作,只需重置滚动窗格的视口:

scrollPane.setViewportView( yourNewPanelFullOfComponents );

不需要revalidate()或repaint()。