gpt4 book ai didi

java - 如何解决 "inner assignments should be avoided"

转载 作者:行者123 更新时间:2023-12-01 10:44:28 40 4
gpt4 key购买 nike

我使用 CheckStyle 来检查我的 java 代码(Intellij idea),我解决了除“应避免内部赋值”之外的所有建议在这一行 { 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;
}

}

最佳答案

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

关于java - 如何解决 "inner assignments should be avoided",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34257727/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com