简体   繁体   English

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

[英]How can I delete and create an object in a mouseListener?

I want to make a mouse event that calls my delete method, which will delete an object, a JScrollPane and all its components and then with another method call create another JScrollPane. 我要创建一个鼠标事件,该事件调用我的delete方法,该方法将删除一个对象,一个JScrollPane及其所有组件,然后使用另一个方法调用创建另一个JScrollPane。

I did that. 我做到了 If I run it outside my mouse event like get_more() and make_it() it works, but if I do it in a mouseListener it doesn't work. 如果我在诸如get_more()make_it()之类的鼠标事件之外运行它,则可以运行,但是如果在mouseListener中运行它,则将不起作用。 I've ve tried revalidate and repaint() , but nothing. 我已经尝试过revalidaterepaint() ,但是什么也没有。 So what is the problem? 那是什么问题呢?

    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");

            }
        });
    }
    }

which will delete an object, a JScrollPane and all its components and then with another method call create another JScrollPane. 这将删除一个对象,一个JScrollPane及其所有组件,然后使用另一个方法调用创建另一个JScrollPane。

Don't do that. 不要那样做 Removing and adding components at run time should be avoided when possible. 应尽可能避免在运行时删除和添加组件。

In this case it is easy to do what you want, just reset the viewport of the scrollpane: 在这种情况下,您可以轻松执行所需操作,只需重置滚动窗格的视口即可:

scrollPane.setViewportView( yourNewPanelFullOfComponents );

There is no need for revalidate() or repaint(). 不需要revalidate()或repaint()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM