我從this鏈接找到一個java代碼。類沒有主要方法(紅綠燈)
當我試圖運行它時,錯誤類沒有主要方法。我知道這是一個類似的帖子,但我試圖尋找/嘗試不同的解決方案,但是當我嘗試它們時,他們不工作。下面是代碼
package trafficlight;
import javax.swing.*;
import java.awt.*;
public final class TrafficLight extends JPanel implements Runnable{
int redDuration, // Time (in seconds) for duration of red light
yellowDuration, // Time (in seconds) for duration of yellow light
greenDuration; // Time (in seconds) for duration of green light
PedestrianLight PL; // Associated coordinated pedestrian light
Color onLight; // To indicate which color light is on
int TLWidth = 0; // Width of traffic light
int TLHeight; // Height of traffic light
int xOrigin; // Coordinates of upper left-hand corner of
int yOrigin; // traffic light
int xLCoor; // x coordinate of lights
int yLCoor; // y coor of red light
int LDiam; // Diameter of each light
int interLSpace; // Space between lights
/*************************************
* Constructors
*************************************/
TrafficLight(PedestrianLight PL) // One-parameter constructor
{
this.PL = PL;
onLight = Color.green;
redDuration = 15000; // Set red duration to 15 secs
yellowDuration = 5000; // Set yellow duration to 5 seconds
greenDuration = 15000; // Set green duration to 15 secs
}
TrafficLight(PedestrianLight PL,Color c) // Two-parameter constructor
{
this.PL = PL;
setOnLight(c); // Verify color setting
redDuration = 15000; // Set red duration to 15 secs
yellowDuration = 5000; // Set yellow duration to 5 seconds
greenDuration = 15000; // Set green duration to 15 secs
}
TrafficLight(PedestrianLight PL,int redDur,int yellowDur,int greenDur){
this.PL = PL;
onLight = Color.green;
redDuration = 1000*redDur; // Duration params given in secs
yellowDuration = 1000*yellowDur; // Convert to milliseconds
greenDuration = 1000*greenDur;
}
/**************************************
* Setters and getters
* @return
**************************************/
public Color getOnLight()
{
return onLight;
}
public void setOnLight(Color c)
{
// Setters and constructors should insure that class variables
// are set to valid values.
if (c == Color.red || c == Color.yellow || c == Color.green)
{
onLight = c;
}
else
{
System.out.println("TrafficLight.setOnLight: cannot set " +
"traffic light to color " + c +
"\nSetting color to default green.");
onLight = Color.green;
}
}
public void setColor(Color c){
setOnLight(c);
repaint();
}
/************************************************
* Paint
************************************************/
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g); // For background
System.out.println("Painting traffic light again");
if (TLWidth == 0){
Dimension d = getSize(); // Get size of panel
System.out.println("d = " + d);
TLWidth = d.width/2; // Set width of traffic light
TLHeight = 3*d.height/4; // Set height of traffic light
xOrigin = (d.width - TLWidth)/2; // Center traffic light on panel
yOrigin = (d.height - TLHeight)/2;
LDiam = TLHeight/6; // Diameter of each light
xLCoor = xOrigin + (TLWidth - LDiam)/2; // x coordinate of lights
interLSpace = (TLHeight - 3*LDiam)/4; // Space between lights
yLCoor = yOrigin + interLSpace; // y coor of red light
}
Color colorSave = g.getColor(); // Save current color
//Draw outline of traffic light
g.setColor(Color.lightGray);
g.fill3DRect(xOrigin,yOrigin,TLWidth,TLHeight,true);
Color Red, Yellow, Green; // Colors to change light to
// Change the light
if (onLight == Color.red){
Red = turnOn(Color.red);
Yellow = turnOff(Color.yellow);
Green = turnOff(Color.green);
}
else if (onLight == Color.yellow){
Red = turnOff(Color.red);
Yellow = turnOn(Color.yellow);
Green = turnOff(Color.green);
}
else{
Red = turnOff(Color.red);
Yellow = turnOff(Color.yellow);
Green = turnOn(Color.green);
}
// Now color the lights. onLight is bright others are darkened.
g.setColor(Red);
g.fillOval(xLCoor,yLCoor,LDiam,LDiam);
g.setColor(Yellow);
g.fillOval(xLCoor,yLCoor+LDiam+interLSpace,LDiam,LDiam);
g.setColor(Green);
g.fillOval(xLCoor,yLCoor+2*LDiam+2*interLSpace,LDiam,LDiam);
// Now draw black outline around each light
g.setColor(Color.black);
// Red light
g.drawOval(xLCoor,yLCoor,LDiam,LDiam);
// Yellow light
g.drawOval(xLCoor,yLCoor+LDiam+interLSpace,LDiam,LDiam);
// Green light
g.drawOval(xLCoor,yLCoor+2*LDiam+2*interLSpace,LDiam,LDiam);
g.setColor(colorSave); // Restore original color
}
/************************************************
* Auxillary methods used by paintComponent
************************************************/
Color turnOn(Color c)
{ return c.brighter().brighter(); }
Color turnOff(Color c)
{ return c.darker().darker(); }
/************************************************
* run method as required by Runnable interface
************************************************/
@Override
public void run(){
System.out.println("Entering TrafficLight.run()");
long startTime;
PL.setMessage("Don't Walk");
while (true)
{
setColor(Color.red); // Change traffic light to red
try{
System.out.println("TL.run() sleep for " + redDuration +
"milliseconds.");
Thread.sleep(redDuration);
}
catch (InterruptedException e) {}
startTime = System.currentTimeMillis();
setColor(Color.green); // Change traffic light to green
PL.setMessage("Walk"); // Change ped light to "Walk"
try{ // Sleep for 2/3 green dur minus time
startTime += 2*greenDuration/3; // to change lights
Thread.sleep(Math.max(0,startTime-System.currentTimeMillis()));
}
catch (InterruptedException e) {}
//PL.setMessage("Don't Walk"); // change ped light to "Don't Walk"
//PL.setFlashing(true); // & start ped light flashing.
startTime = System.currentTimeMillis();
PL.setMessage("Don't Walk",true); //Atomize above two calls
try{
startTime += greenDuration/3; // Sleep 1/3 green duration
Thread.sleep(Math.max(0,startTime-System.currentTimeMillis()));
}
catch (InterruptedException e) {}
startTime = System.currentTimeMillis();
PL.setFlashing(false); // Chg ped light from flash to solid
setColor(Color.yellow); // Change traffic light to yellow
try{
startTime += yellowDuration;
Thread.sleep(Math.max(0,startTime-System.currentTimeMillis()));
}
catch (InterruptedException e) {}
}
}
private static class PedestrianLight {
public PedestrianLight() {
}
private void setFlashing(boolean b) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private void setMessage(String dont_Walk, boolean b) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private void setMessage(String walk) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
看看你的代碼中,有是不是主要的方法。然後錯誤 – SpringLearner
你能舉個例子嗎?對不起,我剛剛開始編碼java – Vista
難道你沒有找到任何你找到課程的指示嗎? 您向我們展示的類是JPanel(JPanel的擴展,但您可以將其用作JPanel)。將它包裝在JFrame中,並在某處創建一個主方法來創建JFrame的實例。在使用Java和Swing構建圖形用戶界面的教科書中查找更多詳細信息。您應該也可以使用交通燈作爲Runnable運行線程。 –