Java 将args和stdin从main传递到类
有人能根据我所掌握的知识给我举个例子,说明如何将命令行args或stdin从main传递给名为drawOnGrid的类吗?我很难理解它。基本上,我需要使用“g.drawString(argOne,10,10);”而不是Draw椭圆形或drawLine。我已附上我的密码Java 将args和stdin从main传递到类,java,Java,有人能根据我所掌握的知识给我举个例子,说明如何将命令行args或stdin从main传递给名为drawOnGrid的类吗?我很难理解它。基本上,我需要使用“g.drawString(argOne,10,10);”而不是Draw椭圆形或drawLine。我已附上我的密码 import java.awt.*; import javax.swing.*; import java.util.*; public class Tictactoe extends JFrame { //construct
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class Tictactoe extends JFrame {
//construct a figurePanel
public Tictactoe() {
Container RandomTicTacToePanel = getContentPane();
RandomTicTacToePanel.setLayout(new GridLayout(3, 3));
for (int i = 0; i < 9; i++) {
RandomTicTacToePanel.add(new drawOnGrid());
}
}
//Main method
public static void main(String[] args) {
String argOne;
String argTwo;
Scanner in = new Scanner(System.in);
int length = args.length;
if (length <= 0) {
System.out.println("Please enter player One's symbol: ");
argOne = in.nextLine();
System.out.println("Please enter player Two's symbol: ");
argTwo = in.nextLine();
in.close();
}
Tictactoe Tframe = new Tictactoe();
Tframe.setTitle("Tic Tac Toe Panel: Random Entries");
Tframe.setSize(350, 350);
Tframe.setResizable(true);
Tframe.setLocationRelativeTo(null);
Tframe.setVisible(true);
Tframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class drawOnGrid extends JPanel {
//overide the paintComponent
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int random = (int) (Math.random() * 3);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (random == 0) {
System.out.print(" ");
} else if (random == 1) {
g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
} else if (random == 2) {
g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
}
}
}
}
}
}
import java.awt.*;
导入javax.swing.*;
导入java.util.*;
公共类Tictactoe扩展JFrame{
//构造一个图形面板
公共交通{
容器RandomTicTacToePanel=getContentPane();
setLayout(新的GridLayout(3,3));
对于(int i=0;i<9;i++){
添加(newdrawingrid());
}
}
//主要方法
公共静态void main(字符串[]args){
字符串argOne;
字符串arg2;
扫描仪输入=新扫描仪(系统输入);
int length=args.length;
如果(长度请尝试以下操作:
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class Tictactoe extends JFrame {
//construct a figurePanel
public Tictactoe(String text) {
Container RandomTicTacToePanel = getContentPane();
RandomTicTacToePanel.setLayout(new GridLayout(3, 3));
for (int i = 0; i < 9; i++) {
RandomTicTacToePanel.add(new drawOnGrid(text));
}
}
//Main method
public static void main(String[] args) {
String argOne = null; // Init with null
String argTwo = null; // Init with null
Scanner in = new Scanner(System.in);
int length = args.length;
if (length <= 0) {
System.out.println("Please enter player One's symbol: ");
argOne = in.nextLine();
System.out.println("Please enter player Two's symbol: ");
argTwo = in.nextLine();
in.close();
} else if(length == 1) {
argOne = args[0];
} else if(length == 2) {
argOne = args[0];
argTwo = args[1];
}
Tictactoe Tframe = new Tictactoe(argOne);
Tframe.setTitle("Tic Tac Toe Panel: Random Entries");
Tframe.setSize(350, 350);
Tframe.setResizable(true);
Tframe.setLocationRelativeTo(null);
Tframe.setVisible(true);
Tframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class drawOnGrid extends JPanel {
private String text;
public drawOnGrid(String text) {
this.text = text;
}
//overide the paintComponent
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int random = (int) (Math.random() * 3);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (random == 0) {
System.out.print(" ");
} else if (random == 1) {
g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
} else if (random == 2) {
g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
}
}
}
}
}
import java.awt.*;
导入javax.swing.*;
导入java.util.*;
公共类Tictactoe扩展JFrame{
//构造一个图形面板
公共Tictactoe(字符串文本){
容器RandomTicTacToePanel=getContentPane();
setLayout(新的GridLayout(3,3));
对于(int i=0;i<9;i++){
添加(新的DrawongGrid(文本));
}
}
//主要方法
公共静态void main(字符串[]args){
字符串argOne=null;//初始化为null
字符串argTwo=null;//初始化为null
扫描仪输入=新扫描仪(系统输入);
int length=args.length;
if(length)您可以通过任何方法用于获取参数的相同机制,直接将args
传递给方法,和/或直接将局部变量传递给方法。您可以创建String[]DrawongGrid
类的属性,并通过构造函数或setter传递args
。@JackManey你能给我一个不复杂的例子吗?@user1646877-aymeric的答案与我给出的例子非常相似。谢谢!你已经被“箭头”了向上。尝试了您所做的操作,并在此行中出现错误Tictactoe Tframe=new Tictactoe(argOne);
statingerror:变量argOne可能尚未初始化Tictactoe Tframe=new Tictactoe(argOne)
有什么想法吗?谢谢,我假设如果我想要第二个命令行arg,我需要创建另一个变量text2?我在g.drawString(text,10,10)方法中使用了第一个变量text。确切地说:)祝其余的好运!