我怎样才能参加终端输入?

时间:2015-12-12 10:57:29

标签: java graphics

我问你是否有办法在应用程序(窗口)启动之前从命令行参加输入。

这是代码,如果它可能有用:

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扫描?

1 个答案:

答案 0 :(得分:0)

只需将System.in部分移动到main方法并将值传递给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
}