Donald J. Trump shook his head. He exhaled loudly. He whispered to his lawyer. He muttered “con job” and “witch hunt.”

As the writer E. Jean Carroll told a jury in a Manhattan courtroom Wednesday how Mr. Trump had wrecked her reputation and made her a target of his most fervent followers, the former president sat at the defense table and bridled.

His comments, the kind of protestations familiar from his rallies and speeches, were unusual only because they came amid a civil jury trial over a suit that Ms. Carroll had brought against Mr. Trump.

She was testifying about what happened when Mr. Trump called her a liar after she accused him in 2019 of a decades-old rape.

Suddenly, Mr. Trump’s grousing escalated into an extraordinary exchange after a lawyer for Ms. Carroll, out of the jury’s presence, cited Mr. Trump’s comments about Ms. Carroll’s testimony and complained that jurors might be able to hear him.

The judge, Lewis A. Kaplan, who had sparred all morning with one of Mr. Trump’s lawyers, Alina Habba, over her objections to Ms. Carroll’s testimony, appeared to be losing his patience. “Mr. Trump has the right to be present here,” the judge began, adding that Mr. Trump could forfeit that right if he is disruptive and if he disregards court orders.

“I hope I don’t have to consider excluding you from the trial,” Judge Kaplan said. “I understand you’re probably very eager for me to do that.”

Mr. Trump threw his hands up. “I would love it,” he said.

“I know you would,” Judge Kaplan said. “You just can’t control yourself in these circumstances, apparently.”

The confrontation came on the second day of trial in a defamation lawsuit by Ms. Carroll, 80, who is seeking millions of dollars in damages for comments Mr. Trump, 77, made after she first said publicly that he had raped her in the mid-1990s in a department store dressing room.

Mr. Trump’s behavior in court Wednesday may foretell clashes between a man who has assailed American institutions and a court system defined by decorum and restraint. Mr. Trump has tried to make his legal problems — which include four criminal indictments — a selling point for voters, and in recent days alternated between appearances in courtrooms and in politically important states.

On Tuesday, he sat through jury selection in Ms. Carroll’s case, flew to New Hampshire later in the day for a campaign stop, then returned to Manhattan on Wednesday morning and attended the session in which she took the witness stand.

Mr. Trump has been attacking Ms. Carroll for more than four years, ever since she first accused him of rape in a book excerpt that appeared in New York magazine in June 2019. She has since sued Mr. Trump twice.

In the first case that went to trial, a jury last May found Mr. Trump liable for sexually abusing her and awarded her just over $2 million in damages. It also awarded her just under $3 million after finding that Mr. Trump had defamed her in a 2022 post on his Truth Social website, in which he called her claim “a complete con job” and a hoax.

The current trial, which had been delayed by appeals, is solely focused on what damages, if any, Mr. Trump must pay Ms. Carroll for defaming her earlier, shortly after the book excerpt appeared.

She is asking for at least $10 million in damages, as well as an unspecified amount of punitive damages, which are intended to punish a defendant and, in this case, to get Mr. Trump to stop insulting her.

Her lawyers have told the jury that Mr. Trump’s attacks have been unceasing. One day after the multimillion-dollar verdict against him last May, he went on CNN and, in response to questions from a moderator, called her a “wack job” with a “made-up story” and said the trial had been “a rigged deal.”

Mr. Trump did not testify in that two-week trial, or even go to the proceedings, but he recently said he wanted to attend this week’s trial and testify. He has been a presence in court, seated at the defense table, since the trial began on Tuesday.

Ms. Carroll, the author of five books, appeared regularly on programs like “Good Morning America” and the “Today” show before her 2019 book excerpt was published. She said those appearances stopped after Mr. Trump accused her of lying, and that she was deluged with threats on social media and in messages to her inbox, some calling for her rape or death.

“He shattered my reputation,” Ms. Carroll testified Wednesday in response to a question from her lawyer, Roberta A. Kaplan, about how the president’s comments had affected her.

“Previously, I was known simply as a journalist,” she said, “and now I’m known as the liar, the fraud and the ‘wack job.’”

Ms. Carroll largely maintained her composure as she described the impact of the messages she had received, but she paused at times, seeming to hold back tears. When she talked about a message that said “stick a gun in your mouth and pull the trigger,” her voice quavered. Mr. Trump shook his head.

Mr. Trump appeared engaged in the proceedings throughout the day, passing notes to his lawyers and consulting with them. Several times, he looked at the nine-member jury, offering a tight smile and at one point nodding in their direction. The jurors remained poker-faced, with some taking notes as Ms. Carroll testified.

Late Wednesday, Mr. Trump’s lawyer, Ms. Habba, who had said in an opening statement that Ms. Carroll had wanted the limelight and had used her story to obtain “fame and notoriety,” began to cross-examine her.

“You’re a journalist, correct?” Ms. Habba asked. “Couldn’t you have expected that some people would not believe you?”

“Yes,” Ms. Carroll said.

Ms. Habba mentioned warm messages of support that Ms. Carroll said she had received in the wake of her accusations, citing her comment in a 2019 interview in USA Today that she was in a “cocoon of love.”

“You weren’t suffering too much emotional harm after his response, isn’t that fair to say?” Ms. Habba asked.

“That’s not fair to say,” Ms. Carroll responded.

“I experienced both great support, which I found very encouraging,” she said, and a “horrible, menacing, terrible flood of slime.”

After the lunch break, another of Mr. Trump’s lawyers, Michael T. Madaio, asked Judge Kaplan to recuse himself, citing what he called “general hostility towards the defense throughout this case.” The judge denied the request.

At a news conference after court ended for the day, Mr. Trump castigated the judge — “a nasty man,” “a nasty judge,” “a Trump-hating guy” — for not allowing a day’s delay for him to attend his mother-in-law’s funeral in Florida on Thursday.

He stood before eight American flags in the lobby of his building at 40 Wall Street and said that the courtroom roles should be reversed and that he should be given money.

“I, frankly, am the one that suffered damages,” Mr. Trump said.

Olivia Bensimon contributed reporting.