FORT LAUDERDALE, Fla. — In the four years since a former student killed 17 people and injured 17 others in Parkland, Fla., the classroom building where the shooting took place has remained standing, unused and fenced off, on the grounds of Marjory Stoneman Douglas High School.

Someday, the thinking went, the gunman could go on trial, and prosecutors might want to take jurors there to witness the remnant horrors of the tragedy from Feb. 14, 2018.

That trial began on Monday in a Fort Lauderdale courthouse with the first stages of jury selection in the state’s case against the gunman, Nikolas Cruz, who pleaded guilty in October to 17 murders and 17 attempted murders. Now, a jury will have to determine whether he should be sentenced to death or life in prison.

Though the trial will consist of only a sentencing phase, it could still last four to six months, according to lawyers for both sides. Prosecutors will detail how the gunman planned his rampage and killed or injured each of his victims. His public defense lawyers will lay out his difficult family life, mental health problems and attempts to get treatment. Testimony is expected from many victims and mental health experts.

Jury selection alone is scheduled to last through May, given how challenging it is expected to be to find prospects who will be available for such a long trial and who will be impartial in a death penalty case that drew so much publicity. As many as 1,500 people may be summoned over the coming weeks. Twelve jurors and eight alternates must be chosen. Judge Elizabeth Scherer said on Monday that the trial is likely to begin on May 31.

Enduring a lengthy, televised trial full of grisly evidence is certain to exact an emotional and painful toll from the two communities, Stoneman Douglas and Parkland, whose names became synonymous with America’s unbridled gun violence. The shooting, in a school of largely affluent teenagers vocal about their political opinions, ignited a national gun control movement and propelled young people and some of their parents into sustained activism.

After the Parkland shooting, Florida adopted some gun restrictions, though the following year, it allowed some staff members in schools to carry guns. The tumultuous local repercussions to the shooting included the removal of the elected sheriff and the resignation of the superintendent of the county’s school system, following a perjury charge stemming from a grand jury investigation. Separately, a sheriff’s deputy assigned to Stoneman Douglas awaits trial on charges of felony neglect of a child for staying outside the building during the shooting.

Both the superintendent and the deputy have pleaded not guilty.

Killed in the shooting were Alyssa Alhadeff, 14; Scott Beigel, 35; Martin Duque, 14; Nicholas Dworet, 17; Aaron Feis, 37; Jaime Guttenberg, 14; Christopher Hixon, 49; Luke Hoyer, 15; Cara Loughran, 14; Gina Montalto, 14; Joaquin Oliver, 17; Alaina Petty, 14; Meadow Pollack, 18; Helena Ramsay, 17; Alex Schachter, 14; Carmen Schentrup, 16, and Peter Wang, 15.

On Monday, less than a dozen family members sat quietly in the courtroom, watching the proceedings intently. The gunman, dressed in an olive green sweater over a collared shirt, sat with his lawyers up front.

Judge Scherer asked prospective jurors about scheduling conflicts or other obstacles that could prevent them from serving for the duration of the trial. Those who did not have any were asked to fill out questionnaires that asked about their backgrounds and their ability to handle graphic evidence. The remaining jury pool will be asked to return in a few weeks for courtroom interviews to gauge whether they can be impartial.

“Please do not discuss this case with anyone,” the judge urged.

A murmur arose from the first group of prospective jurors when Judge Scherer told them that the trial could last through September. Many rattled off reasons that serving on the jury would represent an extreme inconvenience, a sign of the long selection process ahead. One man said he operated a restaurant. Another said his employer would not pay him if he were selected. A woman said she was the caregiver for her grandchildren. Another said she was about to start a new job as a nurse.

“I’m a server at the Cheesecake Factory — if I don’t work, they don’t compensate me,” one man said. If the restaurant were to, the man added, it would pay him only $8 an hour. “And I’m not going into debt,” he said.

Of the first group of 60 prospective jurors, 18 were asked to fill out questionnaires and return in May.

That the gunman faces a court penalty at all is unusual for mass shootings, in which perpetrators often die, either by suicide or by being killed by the police.

The gunman’s guilt was never in doubt: Police officers captured him in the hours after the shooting, and his lawyers offered a guilty plea in exchange for a life sentence. (He was 19 at the time and is now 23.) But the Broward County state attorney, Michael J. Satz, turned down that plea, saying he was obliged to seek capital punishment for such a heinous crime.

Mr. Satz, a Democrat, has since retired, and Broward voters elected a new Democratic state attorney, Harold F. Pryor, who opposes the death penalty. But Mr. Satz stayed on to lead the gunman’s prosecution and to continue to pursue a death sentence. Such a sentence would require a unanimous jury; under Florida law, a single dissent would prevent Judge Scherer from imposing the death penalty.

The gunman’s trial, initially scheduled for 2020, was repeatedly delayed by the coronavirus pandemic.

Last year, he faced a separate trial related to a fight with a sheriff’s deputy in November 2018 while in jail. But during jury selection for that case, several prospective jurors cried after seeing the gunman, underscoring just how traumatized the region’s residents remained. He, too, sobbed in front of prospective jurors.

Soon after, before that trial could begin, the gunman pleaded guilty to battery and other charges in that case — and to the murder and attempted murder charges in the shooting case.

“I am very sorry for what I did, and I have to live with it every day,” he said to the victims’ families the day he entered his guilty pleas. “I love you, and I know you don’t believe me,” he added.

Public records and investigations have revealed a string of missed opportunities to intervene in the gunman’s life as it appeared to be unraveling before the shooting. At one point, school officials and a sheriff’s deputy wanted him forcibly committed for psychiatric evaluation, but he never was.

The F.B.I. failed to investigate tips about his interest in school shootings. Last month, the Department of Justice finalized a $127.5 million settlement for 40 of the shooting victims and their survivors. A separate settlement was reached last year with Broward County Public Schools for $25 million.

The gunman, a former Stoneman Douglas student, recorded three videos on his cellphone before the shooting. “You’re all going to die,” he said in one of them.

Armed with a legally purchased semiautomatic rifle, he killed 14 students and three faculty members in one of the deadliest school shootings in American history, targeting them from the hallways of Building 12, also known as the freshman building.

Bullet-pocked, bloodstained and strewn with valentines, the building has not been used since. The school district plans to eventually demolish it.

Prosecutors have said they might want to take jurors to the building during the trial. Defense lawyers have countered that such a tour would only “inflame” emotions.

The judge decided that she would allow it.