The writer E. Jean Carroll on Wednesday told a Manhattan jury a harrowing story of being raped in the mid-1990s by Donald J. Trump in a department-store dressing room — describing a brutal attack that she tried to fight off by stamping on his foot and that has left her traumatized for decades.

Just before she began testifying in federal court, the former president infuriated the judge overseeing the case by railing against the proceeding on social media. Mr. Trump, who has so far avoided the trial, was not there as Ms. Carroll related a tale she said she had waited decades to tell.

“Being able to get my day in court, finally, is everything to me,” she said, her shaky voice rising. “I’m happy. I’m glad that I got to tell my story.”

Ms. Carroll spoke of an encounter that haunted her and ended her romantic life for good.

“I was ashamed. I thought it was my fault,” she said, describing how she had initially been laughing and joking with Mr. Trump after she ran into him at Bergdorf Goodman in Manhattan. “It was high comedy. It was funny, and then to have it turn into the …” Her voice then trailed off.

Ms. Carroll, 79, testified on Day 2 of the civil trial stemming from the lawsuit she filed against Mr. Trump last year under a New York law that granted adult sexual assault victims a one-year window to seek redress for long-ago events. Her suit, heard in federal court because she and Mr. Trump live in different states, added to a litany of legal action against him.

She is seeking damages for battery in connection with the rape allegations and also for defamation for the attacks he made on her on his Truth Social platform last October, when he called her case a “Hoax and a lie.”

Mr. Trump, 76, who has denied Ms. Carroll’s allegations, has not said whether he will testify in his own defense and has not appeared in court so far. Seeking to regain the presidency, he is scheduled to make a campaign appearance in New Hampshire Thursday. But from outside the courtroom, he attacked the proceedings within.

On Wednesday morning, Mr. Trump used Truth Social to call Ms. Carroll’s case a “made up SCAM” and a “fraudulent & false story,” which led the judge, Lewis A. Kaplan of Federal District Court, to suggest that the former president was trying to influence the jury.

Speaking without the jury present, Judge Kaplan told Mr. Trump’s lawyer Joseph Tacopina that Mr. Trump’s statements seemed “entirely inappropriate.”

“Your client is basically endeavoring certainly to speak to his ‘public,’” Judge Kaplan said, “but, more troublesome, to the jury in this case about stuff that has no business being spoken about.”

The judge implied the statements could lead to a contempt sanction by the court.

Mr. Tacopina said he would talk with his client, but attacks continued, with Mr. Trump’s son, Eric, posting later in the day on Twitter that a prominent backer of Ms. Carroll’s case had been motivated by “pure hatred, spite or fear of a formidable candidate.”

The Truth Social and Twitter posts were brought to the judge’s attention by Ms. Carroll’s lawyer, Roberta A. Kaplan. Mr. Tacopina said that he had not seen or been aware of them.

Nonetheless, after Eric Trump’s attack, Judge Kaplan implied that stronger action might be required.

“Remedies that might be available from this court may not be the only relevant remedies,” the judge told Mr. Tacopina. “If I were in your shoes, I’d be having a conversation with the client.”

Mr. Tacopina and Ms. Carroll’s lawyer, Ms. Kaplan, had no comment after court.

One legal expert, Daniel C. Richman, a criminal law professor at Columbia Law School and a former prosecutor, said the judge might have been referring to a federal obstruction statute that outlaws efforts to corruptly influence or intimidate a juror, whether in a criminal or civil trial.

It would be another in a series of legal matters facing the former president: Mr. Trump already is facing several criminal investigations, a lawsuit by the New York attorney general and fraud charges filed by the Manhattan district attorney stemming from hush money paid to a porn star. Mr. Trump has pleaded not guilty to those charges and has denied wrongdoing in all cases.

While the former president has so far shunned the courtroom where Ms. Carroll’s case is being heard, his accuser relished her chance to speak in an official forum. On the witness stand, she was questioned by one of her lawyers, Michael Ferrara.

“Why are you here today?” Mr. Ferrara asked.

“I am here because Donald Trump raped me, and when I wrote about it, he said it didn’t happen,” Ms. Carroll said. “He lied and shattered my reputation, and I am here to try to get my life back.”

Thus began testimony by Ms. Carroll that lasted most of the day, during which she appeared poised and deliberate but also acknowledged some lapses in her memory. When she was asked precisely when the encounter occurred, she said, “The date has just been something that I am constantly trying to pin down. It’s very difficult.”

There were moments of humor from Ms. Carroll, who once wrote for “Saturday Night Live.” At one point, Mr. Ferrara asked pointedly, “How do you feel about men?”

“I like them!” she said, eliciting a laugh from at least one male juror.

But the courtroom was silent when Ms. Carroll testified in excruciating detail about the events that she said took place nearly 30 years ago.

Ms. Carroll, a longtime advice columnist for Elle magazine, told the jury how she had bumped into Mr. Trump as she was leaving Bergdorf’s after work one evening.

“He came through the door and he said, ‘Hey, you are that advice lady,’” she testified, adding that she replied, “Hey, you’re that real estate tycoon.”

She said she was delighted when he asked for her help selecting a gift for a woman. “I love to give advice, and here was Donald Trump asking me for advice about buying a present,” Ms. Carroll testified. “It was a wonderful prospect for me.”

They made their way to the lingerie section, where Mr. Trump found a bodysuit, directing her to put “go put this on.” She declined and said he should put it on instead. She recalled how he motioned her over to a dressing room; she said did not see anybody else in the area.

Once they were inside, Mr. Trump immediately shut the door, and the sexual assault began, she said.

“I was extremely confused and suddenly realizing that what I thought was happening was not happening,” Ms. Carroll said. She said she didn’t want to anger Mr. Trump, explaining, “I didn’t want to make a scene.”

She said she pushed him back and he again shoved her against the wall, banging her head. She described how Mr. Trump used his weight to hold her against the wall, then pulled down her tights. Ms. Carroll grew emotional as she testified. “I was pushing him back,” she said. “I was almost too frightened to think if I was afraid or not,” she added later.

“His fingers went into my vagina, which was extremely painful,” Ms. Carroll testified. Then, she said, he inserted his penis. She testified that she had not had sex since.

After the attack, Ms. Carroll said, she fled Bergdorf’s onto Fifth Avenue in a state of shock. She said she blamed herself afterward, saying her decision to go into the dressing room was “very stupid.”

Ms. Carroll testified that she told two friends about her experience within a day of the attack. One, Lisa Birnbach, the author and journalist, told her she had been raped and that she needed to go to the police. A second friend, Carol Martin, told her not to tell anyone because Mr. Trump was powerful and had a team of lawyers who would bury her.

Ms. Carroll remained silent for more than 20 years. “I was frightened of Donald Trump,” she explained.

When Mr. Ferrara asked whether she was afraid of how others might react to her story, Ms. Carroll said rape victims are “looked at as soiled goods.” Although people profess sympathy, they can also be judgmental, she said: A victim should have been smarter, screamed louder, dressed differently. And she said that she had never wanted to tell her family.

Ms. Carroll said “visions” of the incident filled her mind repeatedly over the years. “I’ve had them ever since the attack. They were more frequent right after the attack, and they stayed,” Ms. Carroll said.

She said that the experience had robbed her of an essential sense of possibility.

“I am a happy person, basically, but I’m aware that I have lost out on one of the glorious experiences of any human being,” Ms. Carroll said. “Being in love with somebody else, making dinner with them, walking the dog together.”

“I don’t have that,” she said.