有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java RMI聊天程序,需要红色“错误”消息

public String commando(String username, String channel, String text) throws RemoteException{
        String[] result = text.split(" ", 3);
        if(result[0].equalsIgnoreCase("/join")){
            channel = result[1];
            setChannel(channel);
            joinChannel(username, channel);
        }
        else if(result[0].equalsIgnoreCase("/leave")){
            channel = result[1];
            setChannel(channel);
            leaveChannel(username, channel);
        }
        else if(result[0].equalsIgnoreCase("/whisper")){
            for (int x=2; x<result.length; x++)
            newPrivateMessage(username, result[1], result[x]);
        }
        else if(result[0].equalsIgnoreCase("/exit")){
            System.exit(0);
        }
        else{  
        error(brukernavn, "Wrong!");
        }

        return tekst;
    }

我需要红色的错误。此消息(“错误!”)转到编写类似/dfdsfsd的内容的用户 我在屏幕上看到了信息,但我看不到红色的。有什么想法吗

编辑:

干扰:

public interface ChatFront extends Remote {
        void error(String to, String message) throws RemoteException;
}

public interface Klient extends Remote {
        void error(String to, String message) throws RemoteException;
}  

在服务器中:

class ChatFrontImpl extends UnicastRemoteObject implements ChatFront {

    private UserDAO b = new UserDAO();
    private Hashtable<String, ArrayList<String>> chanel = new Hashtable<String, ArrayList<String>>();
    private ArrayList<Klient> clients= new ArrayList<Client>();


    public ChatFrontImpl() throws RemoteException {
    }
public void error(String to, String message) throws RemoteException{
        errorTo(to, message);
    }
private void errorTo(String to, String message) throws RemoteException{
        for(Client k: clients){
            if(k.findName().equals(to)){
                k.error(to, message);
            }
        }
    }

我已经编辑了一些名字(使用挪威语),所以这对u来说可能是一个问题,但这个程序可以工作。唯一的问题是我无法在错误消息上显示红色

编辑2:忘记客户端中的GUI:

public class GUI extends javax.swing.JFrame {

    GUILogikk gl = new GUILogikk(this);

    public void error(String to, String message){
         //chatFelt.setCaretColor(Color.RED);
        chatFelt.append("" + message + "\n");
        chatFelt.setCaretPosition(chatFelt.getText().length());
    }
}

共 (1) 个答案

  1. # 1 楼答案

    如果您使用的是控制台窗口,则必须找到特定于操作系统的函数来设置文本颜色。这些功能因操作系统而异,因此请重新考虑使用控制台窗口,或者针对您计划在其上使用应用程序的每个系统解决问题。如果使用的是Swing之类的工具,则可以检查与试图绘制的组件相关联的可用文本颜色属性(setSelectedTextColor(),等等)。更多信息:JTextArea

    如果只想在图形对象上绘制,可以执行以下操作:

    g.setColor(Color.RED);
    g.drawString("WRONG!", 32, 32); // text, x, y