A federal judge ruled today that Democrat Adlai E. Stevenson III cannot run as an independent candidate for governor of Illinois, dealing another setback to the former U.S. senator's beleaguered campaign.

Stevenson now apparently faces the complicated and politically risky task of forming a third party with a full slate of statewide candidates by Aug. 4 to continue his effort to oust three-term Gov. James R. Thompson (R) in the November election.

Stevenson strategists and Democratic Party leaders have said that, if a third-party effort goes forward, they would choose "nominal" candidates with little voter appeal as a way to minimize the impact on other Democrats running statewide.

Stevenson resigned from the state Democratic ticket last month rather than run with two ultra-right candidates who won nomination in the March primary as his running mates at the top of the slate.

Stevenson, whose father was Illinois governor from 1949-53 and Democratic presidential nominee in 1952 and 1956, said the ruling is "a disappointment, not a setback. I will be on the ballot. This is only a question of position on the ballot." He said he is uncertain whether the ruling will be appealed.

In a 21-page opinion, U.S. District Court Judge James B. Parsons upheld the constitutionality of the state's "sore loser" law, which set a deadline of last December for independent candidacies.

Stevenson sought to change the deadline to Aug. 4, the same as for third-party candidates. Independent candidates in Illinois do not need to fill a full slate of statewide nominees.

Noting that Stevenson "is not a sore loser . . . but a candidate running with a person whose views he finds intolerable," the judge concluded that "this singular problem, though substantial, is still not serious enough to strike down the statute, particularly in light of the availability of equally palatable means of access to the ballot."

Stevenson, looking tanned but tense at a news conference during a campaign stop at DePaul University Law School in the downtown Loop, quipped ruefully that "another funny thing happened on my way to the executive mansion."

Meanwhile, Illinois Republicans are having campaign troubles of their own. Today, seven-term Rep. George M. O'Brien (R-Ill.) disclosed that he has cancer and ended his reelection bid. Republicans will choose a candidate to replace O'Brien, 68, in the heavily Republican 4th District.

On Wednesday, Thompson's candidate for state attorney general, suburban Mayor James Ryan, withdrew from the race amid charges that he beat his wife and his ex-wife. He has denied the allegations.

Stevenson's 1986 campaign stalled when Mark A. Fairchild unexpectedly won nomination in the March 18 primary as Democratic lieutenant governor nominee and Janice Hart won the nomination for secretary of state. Both are backed by ultra-right political activist Lyndon H. LaRouche Jr., whose views Stevenson and other Democrats have denounced as "neo-Nazi."

Under Illinois law, a statewide third-party slate must include nominees for governor, lieutenant governor and eight other offices. If Stevenson forms a third party, he thus must field candidates to run against the regular Democratic ticket, including Sen. Alan J. Dixon, who is seeking a second term.

"Our best hope was that [Stevenson] would be an independent candidate," Dixon said today.

He added that, if Stevenson were forced to form a third party, "then we agreed he would choose nominal candidates against each of us and there would be a discussion process in which each of us would help in the selection process."

Stevenson, who narrowly lost to Thompson in 1982, said he and Thompson are having difficulties and added, "Pretty soon, both sides will get it organized." Then, he promised, there will be a genuine rematch.