2016-04-28 75 views
-2

我的GuiTest類有問題。錯誤:錯誤:在類GUITest中找不到主要方法,請定義主要方法爲: public static void main(String [] args) 或者JavaFX應用程序類必須擴展javafx.application.Application。我怎樣才能解決這個問題?遇到錯誤有問題錯誤:在類GUITest中找不到主要方法

import java.awt.BorderLayout; 
    import java.awt.Color; 
    import java.awt.FlowLayout; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 

    import javax.swing.JButton; 
    import javax.swing.JFrame; 
    import javax.swing.JLabel; 
    import javax.swing.JPanel; 
    //Here i modified this class to add button and implement its action  listener. 
    public class GUITest { 

static class buttonlistener implements ActionListener 
{ 
JFrame l1 ; 
buttonlistener(JFrame l1) 
{ 
this.l1 = l1; 


public static void main(String[] args){ 
    final JFrame f=new JFrame(); 
    f.setLayout(new BorderLayout()); 
    f.add(new PatternPanel()); 
    f.setSize(500,500); 
    f.setVisible(true); 
    f.setBackground(Color.CYAN); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel buttonPanel = new JPanel(); 
    buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); 
    JButton b1 = new JButton("change color"); 
    b1.addActionListener(new ActionListener(){ 

     public void actionPerformed(ActionEvent ae) 
     { 
      f.setLayout(new BorderLayout()); 
      f.add(new CircleTile()); 
      f.setSize(500,500); 
      f.setVisible(true); 
      f.setBackground(Color.CYAN); 
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      repaint(); 
     } 
     }); 
      ; 
      buttonPanel.add(b1); 
      f.add(buttonPanel,BorderLayout.SOUTH); 


    } 

} 

protected void repaint() { 
    // TODO Auto-generated method stub 

} 

@Override 
public void actionPerformed(ActionEvent e) { 
    // TODO Auto-generated method stub 

     } 
    } 
    } 




    import java.awt.Graphics; 
`` import java.awt.Color; 
    import javax.swing.*; 
    import java.awt.Graphics2D; 

    public class PatternPanel extends JPanel { 
    int i,j,x,y; 
    @Override 
    public void paint(Graphics g){ 
    Graphics2D g1=(Graphics2D)g;   
    g1.setColor(Color.GREEN);  
    g1.fillRect(0,0,100,100); 
    g1.drawRect(0,0,100,100); 
    g1.setColor(Color.BLACK); 
    g1.drawOval(0,0,100,100); 
    g1.setColor(Color.YELLOW); 
    g1.fillOval(0,0,100,100); 
    g1.setColor(Color.BLACK); 
    g1.drawOval(10,10,80,80); 
    g1.drawOval(20,20,60,60);  
    g1.drawOval(30,30,40,40);  
    g1.drawOval(40,40,20,20); 
    g1.drawLine(50,0,50,100); 
    g1.drawLine(0,50,100,50); 
    for(i=0;i<3;i++){   
     x=0 + 50*i; 
     g1.drawLine(x,0,x,0+100);   
    } 
    for(i=0;i<3;i++){   
     y=0 + 50*i; 
     g1.drawLine(0,y,0+100,y);   
    } 

    //2nd Block 
    g1.setColor(Color.GREEN);  
    g1.fillRect(10,120,120,120); 
    g1.drawRect(10,120,120,120);     
    for(i=0;i<5;i++){ 
     y=120 + 24*i; 
     for(j=0;j<5;j++){ 
      x=10 + 24*j; 
      g1.setColor(Color.BLACK); 
      g1.drawOval(x,y,24,24); 
      g1.setColor(Color.YELLOW); 
      g1.fillOval(x,y,24,24); 
      g1.setColor(Color.BLACK); 
      g1.drawOval(x + 3,y + 3,18,18); 
      g1.drawOval(x + 6,y + 6,12,12); 
      g1.drawOval(x + 9,y + 9,6,6);    
     }       
    } 
    for(i=0;i<11;i++){   
     x=10 + 12*i; 
     g1.drawLine(x,120,x,120+120);   
    } 
    for(i=0;i<11;i++){   
     y=120 + 12*i; 
     g1.drawLine(10,y,10+120,y);   
    }  

    //3rd Block 
    g1.setColor(Color.GREEN);  
    g1.fillRect(150,20,240,240); 
    g1.drawRect(150,20,240,240);     
    for(i=0;i<6;i++){ 
     y=20 + 40*i; 
     for(j=0;j<6;j++){ 
      x=150 + 40*j; 
      g1.setColor(Color.BLACK); 
      g1.drawOval(x,y,40,40); 
      g1.setColor(Color.YELLOW); 
      g1.fillOval(x,y,40,40); 
      g1.setColor(Color.BLACK); 
      g1.drawOval(x + 4,y + 4,32,32); 
      g1.drawOval(x + 8,y + 8,24,24); 
      g1.drawOval(x + 12,y + 12,16,16); 
      g1.drawOval(x + 16,y + 16,8,8);    
     }    
    } 
    for(i=0;i<13;i++){   
     x=150 + 20*i; 
     g1.drawLine(x,20,x,20+240);   
    } 
    for(i=0;i<13;i++){   
     y=20 + 20*i; 
     g1.drawLine(150,y,150+240,y);   
    } 

    //4th Block 
    g1.setColor(Color.GREEN);  
    g1.fillRect(130,275,108,108); 
    g1.drawRect(130,275,108,108);    
    for(i=0;i<3;i++){ 
     y=275 + 36*i; 
     for(j=0;j<3;j++){ 
      x=130 + 36*j; 
      g1.setColor(Color.BLACK); 
      g1.drawOval(x,y,36,36); 
      g1.setColor(Color.YELLOW); 
      g1.fillOval(x,y,36,36); 
      g1.setColor(Color.BLACK); 
      g1.drawOval(x + 6,y + 6,24,24); 
      g1.drawOval(x + 12,y + 12,12,12);        
     }    
    } 
    for(i=0;i<7;i++){   
     x=130 + 18*i; 
     g1.drawLine(x,275,x,275+108);   
    } 
    for(i=0;i<7;i++){   
     y=275 + 18*i; 
     g1.drawLine(130,y,130+108,y);   
    }  
} 

}

   import java.awt.Graphics; 
import java.awt.Color; 

import javax.swing.*; 

import java.awt.Graphics2D; 
public class CircleTile extends JPanel { 
int i,j,x,y; 
@Override 
public void paint(Graphics g){ 
    Graphics2D g1=(Graphics2D)g;   
    g1.setColor(Color.GREEN);  
    g1.fillRect(0,0,100,100); 
    g1.drawRect(0,0,100,100); 
    g1.setColor(Color.BLACK); 
    g1.drawOval(0,0,100,100); 
    g1.setColor(Color.YELLOW); 
    g1.fillOval(0,0,100,100); 
    g1.setColor(Color.BLACK); 
    g1.drawOval(10,10,80,80); 
    g1.drawOval(20,20,60,60);  
    g1.drawOval(30,30,40,40);  
    g1.drawOval(40,40,20,20); 
    g1.drawLine(50,0,50,100); 
    g1.drawLine(0,50,100,50); 
    for(i=0;i<3;i++){   
     x=0 + 50*i; 
     g1.drawLine(x,0,x,0+100);   
    } 
    for(i=0;i<3;i++){   
     y=0 + 50*i; 
     g1.drawLine(0,y,0+100,y);   
    } 

    //2nd Block 
    g1.setColor(Color.GRAY);   
    g1.fillRect(10,120,120,120); 
    g1.drawRect(10,120,120,120);     
    for(i=0;i<5;i++){ 
     y=120 + 24*i; 
     for(j=0;j<5;j++){ 
      x=10 + 24*j; 
      g1.setColor(Color.BLACK); 
      g1.drawOval(x,y,24,24); 
      g1.setColor(Color.YELLOW); 
      g1.fillOval(x,y,24,24); 
      g1.setColor(Color.BLACK); 
      g1.drawOval(x + 3,y + 3,18,18); 
      g1.drawOval(x + 6,y + 6,12,12); 
      g1.drawOval(x + 9,y + 9,6,6);    
     }       
    } 
    for(i=0;i<11;i++){   
     x=10 + 12*i; 
     g1.drawLine(x,120,x,120+120);   
    } 
    for(i=0;i<11;i++){   
     y=120 + 12*i; 
     g1.drawLine(10,y,10+120,y);   
    }  

    //3rd Block 
    g1.setColor(Color.BLUE);   
    g1.fillRect(150,20,240,240); 
    g1.drawRect(150,20,240,240);     
    for(i=0;i<6;i++){ 
     y=20 + 40*i; 
     for(j=0;j<6;j++){ 
      x=150 + 40*j; 
      g1.setColor(Color.BLACK); 
      g1.drawOval(x,y,40,40); 
      g1.setColor(Color.RED); 
      g1.fillOval(x,y,40,40); 
      g1.setColor(Color.BLACK); 
      g1.drawOval(x + 4,y + 4,32,32); 
      g1.drawOval(x + 8,y + 8,24,24); 
      g1.drawOval(x + 12,y + 12,16,16); 
      g1.drawOval(x + 16,y + 16,8,8);    
     }    
    } 
    for(i=0;i<13;i++){   
     x=150 + 20*i; 
     g1.drawLine(x,20,x,20+240);   
    } 
    for(i=0;i<13;i++){   
     y=20 + 20*i; 
     g1.drawLine(150,y,150+240,y);   
    } 

    //4th Block 
    g1.setColor(Color.GRAY);   
    g1.fillRect(130,275,108,108); 
    g1.drawRect(130,275,108,108);    
    for(i=0;i<3;i++){ 
     y=275 + 36*i; 
     for(j=0;j<3;j++){ 
      x=130 + 36*j; 
      g1.setColor(Color.BLACK); 
      g1.drawOval(x,y,36,36); 
      g1.setColor(Color.PINK); 
      g1.fillOval(x,y,36,36); 
      g1.setColor(Color.BLACK); 
      g1.drawOval(x + 6,y + 6,24,24); 
      g1.drawOval(x + 12,y + 12,12,12);        
     }    
    } 
    for(i=0;i<7;i++){   
     x=130 + 18*i; 
     g1.drawLine(x,275,x,275+108);   
    } 
    for(i=0;i<7;i++){   
     y=275 + 18*i; 
     g1.drawLine(130,y,130+108,y);   
    } 
} 
} 
+0

香港專業教育學院發現了問題,感謝尋求幫助 –

回答

3

你的主要方法是埋一個內部類的構造函數裏面 - 爲什麼?你所要做的就是修正錯誤告訴你修正的內容:把它放在主外部類GUITest類中。

永遠不要忽略編譯器錯誤信息,事實上解決這個問題和大多數編譯器錯誤的關鍵是嘗試嚴格地讀取錯誤信息,然後修復它告訴你修復的問題。做到這一點,你會解決這些類型的錯誤的90%。

你的代碼的另一個問題是:你的縮進遍佈各處,變化很大,正因爲如此,你不會看到你做錯了什麼。如果你正確和經常地縮進,你很快就會看到你的主要方法如何深深嵌入到錯誤的課堂中。瞭解縮進和格式化規則並不是爲了讓代碼看起來很漂亮(儘管它有助於做到這一點),而是可以幫助您更好地理解和調試代碼。

舉例來說,如果你的代碼進行了適當格式化的,你會發現深藏在縮進代碼顯然埋葬的主要方法:

public class GUITest { 

    static class buttonlistener implements ActionListener { 
     JFrame l1 ; 
     buttonlistener(JFrame l1) { 
      this.l1 = l1; 

      // ***** here is your main method, where it does not belong 
      public static void main(String[] args){ 
       final JFrame f=new JFrame(); 
       f.setLayout(new BorderLayout()); 
       f.add(new PatternPanel()); 
       f.setSize(500,500); 
       f.setVisible(true); 
       f.setBackground(Color.CYAN); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       JPanel buttonPanel = new JPanel(); 
       buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); 
       JButton b1 = new JButton("change color"); 
       b1.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent ae) { 
         f.setLayout(new BorderLayout()); 
         f.add(new CircleTile()); 
         f.setSize(500,500); 
         f.setVisible(true); 
         f.setBackground(Color.CYAN); 
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         repaint(); 
        } 
       }); 
       buttonPanel.add(b1); 
       f.add(buttonPanel,BorderLayout.SOUTH); 
      } 
     } 

     protected void repaint() { 
      // TODO Auto-generated method stub 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      // TODO Auto-generated method stub 
     } 
    } 
} 

所以解決這個問題:

public class GUITest { 

    // ***** So move it to where it now does belong 
    public static void main(String[] args){ 
     final JFrame f=new JFrame(); 
     f.setLayout(new BorderLayout()); 
     f.add(new PatternPanel()); 
     f.setSize(500,500); 
     f.setVisible(true); 
     f.setBackground(Color.CYAN); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel buttonPanel = new JPanel(); 
     buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); 
     JButton b1 = new JButton("change color"); 
     b1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
       f.setLayout(new BorderLayout()); 
       f.add(new CircleTile()); 
       f.setSize(500,500); 
       f.setVisible(true); 
       f.setBackground(Color.CYAN); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       repaint(); 
      } 
     }); 
     ; 
     buttonPanel.add(b1); 
     f.add(buttonPanel,BorderLayout.SOUTH); 
    }  

    static class buttonlistener implements ActionListener { 
     JFrame l1 ; 
     buttonlistener(JFrame l1) { 
      this.l1 = l1; 
     } 

     protected void repaint() { 
      // TODO Auto-generated method stub 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      // TODO Auto-generated method stub 
     } 
    } 
} 
相關問題