Java 锁定单词以防止它们被使用
Java locking words so they can't be used
我正在开发一个聊天客户端,它运行良好,但是如果你打开聊天客户端,你会在我的框架顶部看到一个栏,如果你在那里输入你的名字并向它发送消息将显示您的姓名+消息。但我不想让人们使用某些词或字符,而且我不知道该怎么做。我试图在互联网上查看但找不到任何有用的东西。所以这是我的聊天客户端代码,我也想在你按下回车键时发送消息,但我所做的不起作用:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
class Chat implements KeyListener{
JTextArea incoming;
JTextField output, name;
JButton send;
BufferedReader reader;
PrintWriter writer;
Socket sock;
public static void main(String [] args){
Chat c = new Chat();
c.Start();
}
public void Start(){
JFrame frame = new JFrame("Chat Client Alpha 1.1");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(700,700);
frame.setResizable(false);
frame.setBackground(Color.black);
JPanel center = new JPanel();
JPanel south = new JPanel();
JPanel north = new JPanel();
JLabel EName = new JLabel("Enter your name: ");
incoming = new JTextArea(35,45);
send = new JButton("SEND");
send.addActionListener(new SendMessageListener());
output = new JTextField(40);
name = new JTextField(37);
name.setText("Default");
incoming.setEditable(false);
incoming.setLineWrap(true);
JScrollPane sc = new JScrollPane(incoming);
sc.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
sc.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
north.add(EName);
north.add(name);
center.add(sc);
south.add(output);
south.add(send);
north.setBackground(Color.gray);
center.setBackground(Color.gray);
south.setBackground(Color.gray);
setUpNetworking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
frame.getContentPane().add(BorderLayout.NORTH, north);
frame.getContentPane().add(BorderLayout.CENTER, center);
frame.getContentPane().add(BorderLayout.SOUTH, south);
frame.setVisible(true);
} private void setUpNetworking(){
try{
sock = new Socket("89.99.7.141", 5000);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
incoming.append("Networking Established" + "\n");
}catch(IOException ex){
ex.printStackTrace();
incoming.append("Connecting Failed" + "\n");
}
}
class SendMessageListener implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
writer.println("(" + name.getText() + ")" + " " + output.getText());
writer.flush();
}catch(Exception ex){
ex.printStackTrace();
}
output.setText("");
output.requestFocus();
}
}
public class IncomingReader implements Runnable{
public void run(){
String message;
try{
while((message = reader.readLine()) !=null){
System.out.println("read" + message);
incoming.append(message + "\n");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}public void keyTyped(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_ENTER){
new SendMessageListener();
System.out.println("key typed");
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER){
new SendMessageListener();
System.out.println("key pressed");
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER){
new SendMessageListener();
System.out.println("key released");
}
}
}
希望你明白我的意思。但我对锁定 words/characters 的意思是,除了我之外,人们不能使用 "admin" 这个词。
抱歉英语不好,我来自荷兰。
您可以使用包含坏词的集合,然后只测试输入的词是否存在于集合中,例如(hello
是下面示例中的坏词)。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
class Chat implements KeyListener{
JTextArea incoming;
JTextField output, name;
JButton send;
BufferedReader reader;
PrintWriter writer;
Socket sock;
Set<String> badWords = new HashSet<>(Arrays.asList("hello"));
public static void main(String [] args){
Chat c = new Chat();
c.Start();
}
public void Start(){
JFrame frame = new JFrame("Chat Client Alpha 1.1");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(700,700);
frame.setResizable(false);
frame.setBackground(Color.black);
JPanel center = new JPanel();
JPanel south = new JPanel();
JPanel north = new JPanel();
JLabel EName = new JLabel("Enter your name: ");
incoming = new JTextArea(35,45);
send = new JButton("SEND");
send.addActionListener(new SendMessageListener());
output = new JTextField(40);
name = new JTextField(37);
name.setText("Default");
incoming.setEditable(false);
incoming.setLineWrap(true);
JScrollPane sc = new JScrollPane(incoming);
sc.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
sc.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
north.add(EName);
north.add(name);
center.add(sc);
south.add(output);
south.add(send);
north.setBackground(Color.gray);
center.setBackground(Color.gray);
south.setBackground(Color.gray);
setUpNetworking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
frame.getContentPane().add(BorderLayout.NORTH, north);
frame.getContentPane().add(BorderLayout.CENTER, center);
frame.getContentPane().add(BorderLayout.SOUTH, south);
frame.setVisible(true);
} private void setUpNetworking(){
try{
sock = new Socket("89.99.7.141", 5000);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
incoming.append("Networking Established" + "\n");
}catch(IOException ex){
ex.printStackTrace();
incoming.append("Connecting Failed" + "\n");
}
}
class SendMessageListener implements ActionListener{
public void actionPerformed(ActionEvent e){
try {
if (badWords.contains(output.getText())) {
writer.println("(" + name.getText() + ")" + " " + "sorry bad words are not allowed");
} else if (output.getText().length() > 0){
writer.println("(" + name.getText() + ")" + " " + output.getText());
}
writer.flush();
}catch(Exception ex){
ex.printStackTrace();
}
output.setText("");
output.requestFocus();
}
}
public class IncomingReader implements Runnable{
public void run(){
String message;
try{
while((message = reader.readLine()) !=null){
System.out.println("read" + message);
incoming.append(message + "\n");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}public void keyTyped(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_ENTER){
new SendMessageListener();
System.out.println("key typed");
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER){
new SendMessageListener();
System.out.println("key pressed");
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER){
new SendMessageListener();
System.out.println("key released");
}
}
}
我正在开发一个聊天客户端,它运行良好,但是如果你打开聊天客户端,你会在我的框架顶部看到一个栏,如果你在那里输入你的名字并向它发送消息将显示您的姓名+消息。但我不想让人们使用某些词或字符,而且我不知道该怎么做。我试图在互联网上查看但找不到任何有用的东西。所以这是我的聊天客户端代码,我也想在你按下回车键时发送消息,但我所做的不起作用:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
class Chat implements KeyListener{
JTextArea incoming;
JTextField output, name;
JButton send;
BufferedReader reader;
PrintWriter writer;
Socket sock;
public static void main(String [] args){
Chat c = new Chat();
c.Start();
}
public void Start(){
JFrame frame = new JFrame("Chat Client Alpha 1.1");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(700,700);
frame.setResizable(false);
frame.setBackground(Color.black);
JPanel center = new JPanel();
JPanel south = new JPanel();
JPanel north = new JPanel();
JLabel EName = new JLabel("Enter your name: ");
incoming = new JTextArea(35,45);
send = new JButton("SEND");
send.addActionListener(new SendMessageListener());
output = new JTextField(40);
name = new JTextField(37);
name.setText("Default");
incoming.setEditable(false);
incoming.setLineWrap(true);
JScrollPane sc = new JScrollPane(incoming);
sc.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
sc.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
north.add(EName);
north.add(name);
center.add(sc);
south.add(output);
south.add(send);
north.setBackground(Color.gray);
center.setBackground(Color.gray);
south.setBackground(Color.gray);
setUpNetworking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
frame.getContentPane().add(BorderLayout.NORTH, north);
frame.getContentPane().add(BorderLayout.CENTER, center);
frame.getContentPane().add(BorderLayout.SOUTH, south);
frame.setVisible(true);
} private void setUpNetworking(){
try{
sock = new Socket("89.99.7.141", 5000);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
incoming.append("Networking Established" + "\n");
}catch(IOException ex){
ex.printStackTrace();
incoming.append("Connecting Failed" + "\n");
}
}
class SendMessageListener implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
writer.println("(" + name.getText() + ")" + " " + output.getText());
writer.flush();
}catch(Exception ex){
ex.printStackTrace();
}
output.setText("");
output.requestFocus();
}
}
public class IncomingReader implements Runnable{
public void run(){
String message;
try{
while((message = reader.readLine()) !=null){
System.out.println("read" + message);
incoming.append(message + "\n");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}public void keyTyped(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_ENTER){
new SendMessageListener();
System.out.println("key typed");
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER){
new SendMessageListener();
System.out.println("key pressed");
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER){
new SendMessageListener();
System.out.println("key released");
}
}
}
希望你明白我的意思。但我对锁定 words/characters 的意思是,除了我之外,人们不能使用 "admin" 这个词。
抱歉英语不好,我来自荷兰。
您可以使用包含坏词的集合,然后只测试输入的词是否存在于集合中,例如(hello
是下面示例中的坏词)。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
class Chat implements KeyListener{
JTextArea incoming;
JTextField output, name;
JButton send;
BufferedReader reader;
PrintWriter writer;
Socket sock;
Set<String> badWords = new HashSet<>(Arrays.asList("hello"));
public static void main(String [] args){
Chat c = new Chat();
c.Start();
}
public void Start(){
JFrame frame = new JFrame("Chat Client Alpha 1.1");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(700,700);
frame.setResizable(false);
frame.setBackground(Color.black);
JPanel center = new JPanel();
JPanel south = new JPanel();
JPanel north = new JPanel();
JLabel EName = new JLabel("Enter your name: ");
incoming = new JTextArea(35,45);
send = new JButton("SEND");
send.addActionListener(new SendMessageListener());
output = new JTextField(40);
name = new JTextField(37);
name.setText("Default");
incoming.setEditable(false);
incoming.setLineWrap(true);
JScrollPane sc = new JScrollPane(incoming);
sc.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
sc.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
north.add(EName);
north.add(name);
center.add(sc);
south.add(output);
south.add(send);
north.setBackground(Color.gray);
center.setBackground(Color.gray);
south.setBackground(Color.gray);
setUpNetworking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
frame.getContentPane().add(BorderLayout.NORTH, north);
frame.getContentPane().add(BorderLayout.CENTER, center);
frame.getContentPane().add(BorderLayout.SOUTH, south);
frame.setVisible(true);
} private void setUpNetworking(){
try{
sock = new Socket("89.99.7.141", 5000);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
incoming.append("Networking Established" + "\n");
}catch(IOException ex){
ex.printStackTrace();
incoming.append("Connecting Failed" + "\n");
}
}
class SendMessageListener implements ActionListener{
public void actionPerformed(ActionEvent e){
try {
if (badWords.contains(output.getText())) {
writer.println("(" + name.getText() + ")" + " " + "sorry bad words are not allowed");
} else if (output.getText().length() > 0){
writer.println("(" + name.getText() + ")" + " " + output.getText());
}
writer.flush();
}catch(Exception ex){
ex.printStackTrace();
}
output.setText("");
output.requestFocus();
}
}
public class IncomingReader implements Runnable{
public void run(){
String message;
try{
while((message = reader.readLine()) !=null){
System.out.println("read" + message);
incoming.append(message + "\n");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}public void keyTyped(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_ENTER){
new SendMessageListener();
System.out.println("key typed");
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER){
new SendMessageListener();
System.out.println("key pressed");
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER){
new SendMessageListener();
System.out.println("key released");
}
}
}