📄 filterpane2.java
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class filterPane2 extends JPanel{
private JTextField text;
private JButton button;
private JButton dbutton;
private JButton exit;
private JTextArea textArea;
private JLabel label;
String str=null;
public filterPane2() {
this.setLayout(null);
label=new JLabel("输入添加/删除禁止访问的网站地址:");
label.setHorizontalAlignment(SwingConstants.LEFT);
label.setBounds(new Rectangle(0, 10, 500, 20));
text=new JTextField(20);
text.setText("");
text.setHorizontalAlignment(SwingConstants.LEFT);
text.setBounds(new Rectangle(0,30,200,20));
button=new JButton("添加");
button.setBounds(new Rectangle(202,30,60,20));
dbutton=new JButton("删除");
dbutton.setBounds(new Rectangle(264,30,60,20));
exit=new JButton("返回");
exit.setBounds(new Rectangle(326,30,60,20));
textArea=new JTextArea();
textArea.setEditable(false);
textArea.setBounds(new Rectangle(2,55,300,200));
//添加滚动条
this.add(label, null);
this.add(text, null);
this.add(button, null);
this.add(dbutton,null);
this.add(exit);
this.add(textArea,null);
read();
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
str=text.getText();
if(str.equals("")){
JOptionPane.showMessageDialog(null,"输入不能为空!","错误",JOptionPane.ERROR_MESSAGE);
}
else{
insert(str);
text.setText("");
}
}
});
dbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
str=text.getText();
if(str.equals("")){
JOptionPane.showMessageDialog(null,"输入不能为空!","错误",JOptionPane.ERROR_MESSAGE);
}
else{
delete(str);
}
text.setText("");
}
});
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
eventFrame.jSplitPane1.remove(eventFrame.jSplitPane1.getRightComponent());
if(toolBarPanel.state()){
eventFrame.jSplitPane1.add(eventFrame.welcomePanel1,JSplitPane.RIGHT);
}else{
eventFrame.jSplitPane1.add(eventFrame.disPlay,JSplitPane.RIGHT);
}
}
});
}
public static boolean find(String str1){
String s=null;
boolean flag=false;
try{
BufferedReader in=new BufferedReader(new FileReader("bansite\\bansite2.txt"));
while((s=in.readLine())!=null){
if(s.equals(str1)){
flag=true;
break;
}
}
}catch(Exception e){
JOptionPane.showMessageDialog(null,"读文件出错!","错误",JOptionPane.ERROR_MESSAGE);
}
return flag;
}
public void read(){
String s=null;
try{
BufferedReader in=new BufferedReader(new FileReader("bansite\\bansite2.txt"));
while((s=in.readLine())!=null){
textArea.append(s);
textArea.append("\n");
}
}catch(Exception e){
JOptionPane.showMessageDialog(null,"读文件出错!","错误",JOptionPane.ERROR_MESSAGE);
}
}
public void insert(String str1){
boolean flag=find(str1);
if(!flag){
try{
BufferedWriter out=new BufferedWriter(new FileWriter("bansite\\bansite2.txt",true));
out.write(str1);
out.newLine();
out.flush();
out.close();
textArea.append(str1);
textArea.append("\n");
JOptionPane.showMessageDialog(null,"添加成功!","消息",JOptionPane.INFORMATION_MESSAGE);
}catch(IOException e){
JOptionPane.showMessageDialog(null,"写文件出错!","错误",JOptionPane.ERROR_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(null,"该地址已经存在!","消息",JOptionPane.INFORMATION_MESSAGE);
}
}
public void delete(String str1){
String s=null;
boolean flag=find(str1);
if(flag){
try{
BufferedReader in=new BufferedReader(new FileReader("bansite\\bansite2.txt"));
BufferedWriter out=new BufferedWriter(new FileWriter("bansite\\temp.txt",false));
while((s=in.readLine())!=null){
if(s.equals(str)==false){
out.write(s);
out.newLine();
}
}
out.flush();
out.close();
in.close();
textArea.setText("");
in=new BufferedReader(new FileReader("bansite\\temp.txt"));
out=new BufferedWriter(new FileWriter("bansite\\bansite2.txt",false));
while((s=in.readLine())!=null){
out.write(s);
out.newLine();
textArea.append(s);
textArea.append("\n");
}
out.flush();
out.close();
in.close();
File f=new File("bansite\\temp.txt");
f.delete();
JOptionPane.showMessageDialog(null,"删除成功!","消息",JOptionPane.INFORMATION_MESSAGE);
}catch(IOException e){
JOptionPane.showMessageDialog(null,"文件操作出错!","错误",JOptionPane.ERROR_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(null,"该地址不存在!删除失败!","消息",JOptionPane.INFORMATION_MESSAGE);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -