2015-12-12 58 views
-1

我問你是否有方法在應用程序(窗口)啓動之前從命令行參加輸入。我如何參加終端輸入?

這裏的,如果它可能是有用的代碼:

package classi.luca; 

import java.awt.Color; 
import java.awt.Graphics; 
import javax.swing.JPanel; 
import java.util.Scanner; 

public class Test1 extends JPanel { 
    public void paint(Graphics g) { 
     super.paint(g);   // call superclass to make panel display correctly 
     System.out.print("Do you want to visualize Circle data or Rectangle data? "); 
     Scanner in = new Scanner(System.in); 
     String figure_init = in.next(); 
     FiguresInit figure = new FiguresInit(); // create a FiguresInt variable 
     switch (figure_init) { 
      case "Circle": figure.initCircle(g); // Initialize a Circle from FiguresInit 
       break; 
      case "Rectangle": figure.initRectangle(g); 
       break; 
     } 
    } 

    public static void main(String[] args) { 
     Test1 panel = new Test1(); 
     FrameClass fr = new FrameClass("Disegno e Area del Cerchio"); // Inizialize a drawing panel of FrameClass Type 

     fr.init(panel); 
     panel.setBackground(Color.WHITE); 
    } 
} 

有沒有辦法出席打開應用程序窗口前System.in掃描?

回答

0

的System.in部分只需移動到您的主要方法和價值傳遞給您的Test1的構造函數:

public static void main(String[] args) { 
    System.out.print("Do you want to visualize Circle data or Rectangle data? "); 
    Scanner in = new Scanner(System.in); 
    String figure_init = in.next(); 
    FiguresInit figure = new FiguresInit(); // create a FiguresInt variable 
    Test1 panel = new Test1(figure); 
    //... 

你的構造函數會是這個樣子:

Test1(FiguresInit figure) { 
    this.figure = figure; 
    // other init stuff 
} 
+0

確定。我怎樣才能將'figure'傳遞給'Test1'? @Burkhard –

+0

@LucaPierri看看他的代碼,他已經這樣做了(並在代碼上面的他的句子中寫到)。 – Tom

+0

好的,非常感謝! –