如何解决“应该避免内部任务”

时间:2015-12-13 23:36:01

标签: java intellij-idea checkstyle

使用CheckStyle来检查我的java代码(Intellij的想法),我解决了所有的建议,除了一个“”内部任务应该避免“ 在这一行     { frame.add(new JScrollPane(messages = new JTextPane())); } 有什么建议如何以解决问题的方式重新编写代码?

public class Gui implements Room.Subscriber {
// static block, executes when class is loaded first time
static {
try {
    // Set system gui skin
    UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());

}  catch (Exception e) {
        e.printStackTrace();
        System.exit(2);
         }
         }

private JTextPane messages;
private Room currentRoom;
private String nick;
private static final int A = 480, B = 320, C = 100;

public Gui()     {
    // constructing form
    final JFrame frame = new JFrame("Chat");
    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(final WindowEvent  arg0) {
            currentRoom.unsubscribe(Gui.this);
            frame.dispose();
        }
    });
    // messages field
    frame.add(new JScrollPane(messages = new JTextPane()));
    messages.setEditable(false);

    // creating styles
    messages.setBackground(Color.white);

    Style msg = messages.addStyle("nick", null);
    StyleConstants.setForeground(msg, Color.red);

    msg = messages.addStyle("message", null);
    StyleConstants.setForeground(msg, Color.black);

    msg = messages.addStyle("my-message", null);
    StyleConstants.setForeground(msg, Color.blue);

    msg = messages.addStyle("join", null);
    StyleConstants.setForeground(msg, Color.gray);
    StyleConstants.setItalic(msg, true);

    msg = messages.addStyle("part", null);
    StyleConstants.setForeground(msg, Color.gray);
    StyleConstants.setItalic(msg, true);

    // input panel
    final JTextField input = new JTextField();

    ActionListener sendMessage = new ActionListener() {
        public void actionPerformed(final ActionEvent event)            {
            sendMessage(input.getText());
            input.setText("");
        }
    };

    input.addActionListener(sendMessage);

    JButton send = new JButton("Send");
    send.addActionListener(sendMessage);

    JPanel bottom = new JPanel();
    bottom.setLayout(new BorderLayout());
    bottom.add(input);
    bottom.add(send, BorderLayout.LINE_END);
    frame.add(bottom, BorderLayout.SOUTH);

    frame.setSize(A, B);
    frame.setVisible(true);
}

private void appendLine(final String line, final String style)    {
    try {
        messages.getDocument().insertString(
                messages.getDocument().getEndPosition().getOffset(),
                line + "\n", messages.getStyle(style));

    }  catch (BadLocationException e) {
        throw new RuntimeException(e);
    }
}

public final void subscribeTo(final Room room)    {
    Random random = new Random();
    this.nick = "User" + random.nextInt(C);
    room.subscribe(this);
    this.currentRoom = room;
}

// -- events

private void sendMessage(final String text)    {
    currentRoom.publish(this, text);
    appendLine("[" + nick + "]: ", "nick");
    appendLine(text, "my-message");
}

public final void joined(final String nick01)    {
    appendLine("** " + nick + " joined chat", "join");
}

public final void parted(final String nick02)    {
    appendLine("** " + nick + " left us", "part");
}

public final void  receive(final String nick03, final String message)    {


    appendLine("[" +  nick + "]: ", "nick");
    appendLine(message, "message");
}

public static void main(final String[] args)
throws UnknownHostException, IOException    {
    final int x = 1024;
           Gui gui = new Gui();
    System.out.println("Connecting to server...");
    Room room = new ClientTransport(new Socket(args[0], x));
    System.out.println("Connected, registering...");
    gui.subscribeTo(room);
}

public final String getNick()    {
    return nick;
}

}

1 个答案:

答案 0 :(得分:1)

// messages field
messages = new JTextPane();
frame.add(new JScrollPane(messages));
messages.setEditable(false);