[英]How can I delete and create an object in a mouseListener?
我要創建一個鼠標事件,該事件調用我的delete方法,該方法將刪除一個對象,一個JScrollPane及其所有組件,然后使用另一個方法調用創建另一個JScrollPane。
我做到了 如果我在諸如get_more()
和make_it()
之類的鼠標事件之外運行它,則可以運行,但是如果在mouseListener中運行它,則將不起作用。 我已經嘗試過revalidate
和repaint()
,但是什么也沒有。 那是什么問題呢?
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");
}
});
}
}
這將刪除一個對象,一個JScrollPane及其所有組件,然后使用另一個方法調用創建另一個JScrollPane。
不要那樣做 應盡可能避免在運行時刪除和添加組件。
在這種情況下,您可以輕松執行所需操作,只需重置滾動窗格的視口即可:
scrollPane.setViewportView( yourNewPanelFullOfComponents );
不需要revalidate()或repaint()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.