我应该在我的主方法中放什么内容

huangapple go评论71阅读模式
英文:

What am I supposed to put in my main method

问题

以下是您提供的内容的翻译部分:

代码输入:

    int[] output = new int[4];
    output[0] = addObj.getSumA();
    output[1] = addObj.getSumB();
    output[2] = addObj.getSumC();
    output[3] = addObj.getSumD();

    Output outputObj = new Output(output);

Output 类的代码:

public class Output extends JFrame implements ActionListener
{
    private JLabel numberA;
    private JLabel numberB;
    private JLabel numberC;
    private JLabel numberD;
    private Box numberBox;
    private Box numberBox2;

    public Output(int output[])
    {
        super("Output Frame");
        this.setBounds(430, 300, 600, 450);
        this.getContentPane().setBackground(Color.PINK);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setLayout(new BorderLayout());

        this.numberA = new JLabel(Integer.toString(output[0]));
        this.numberB = new JLabel(Integer.toString(output[1]));
        this.numberC = new JLabel(Integer.toString(output[2]));
        this.numberD = new JLabel(Integer.toString(output[3]));

        numberBox = Box.createVerticalBox();
        numberBox.add(numberA);
        numberBox.add(numberC);

        numberBox2 = Box.createVerticalBox();
        numberBox2.add(numberB);
        numberBox2.add(numberD);

        this.setVisible(true);
    }

    public static void main(String[] args)
    {
        Output outputObj = new Output(output);
    }
}

请注意,这是一个 GUI(图形用户界面)应用程序。错误出现在上面的那行代码中。int[] 不是正确的输入,但我不知道应该输入什么。

英文:

I have an output class receiving an array from an input class. The array is then changed into labels in the output class. I have an error in the main method of my output class. It might have something to deal with the connection between the input class. What should I have put in my main method of the output class to fix the error?

Code of input:

int[]output = new int[4];
    output[0] = addObj.getSumA();
    output[1] = addObj.getSumB();
    output[2] = addObj.getSumC();
    output[3] = addObj.getSumD();
            
    Output outputObj = new Output(output);

Code of Output Class:

public class Output extends JFrame implements ActionListener
{
    private JLabel numberA;
    private JLabel numberB;
    private JLabel numberC;
    private JLabel numberD;
    private Box numberBox;
    private Box numberBox2;

public Output(int output[])
{
    super("Output Frame");
    this.setBounds(430,300,600,450);
    this.getContentPane().setBackground(Color.PINK);
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    this.setLayout(new BorderLayout());
    
    this.numberA = new JLabel(Integer.toString(output[0]));
    this.numberB = new JLabel(Integer.toString(output[1]));
    this.numberC = new JLabel(Integer.toString(output[2]));
    this.numberD = new JLabel(Integer.toString(output[3]));
    
    numberBox = Box.createVerticalBox();
    numberBox.add(numberA);
    numberBox.add(numberC);
    
    numberBox2 = Box.createVerticalBox();
    numberBox2.add(numberB);
    numberBox2.add(numberD);
    
    this.setVisible(true);
}

public static void main (String[] args)
{
    Output outputObj = new Output(int[]);
}

Keep in mind this is gui. The error is in the line above. int[] isn't the correct thing to enter, but I don't know what is.

答案1

得分: 0

你需要实际声明并初始化一个数组,作为参数传递。

所以首先像这样创建一个 int 数组。

这将创建一个大小为 10 的数组(尽管没有分配值)

int[] intArr = new int[10];

你还可以一行内创建一个数组并填充值,像这样

int[] intArr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 
// 现在你可以调用你的方法并传递你创建的数组
Output outputObj = new Output(intArr);
英文:

You need to actually declare and initialize an array to pass as an argument.

So create an int array first like this.

this creates an array of size 10 (doesn't have values assigned though)

int[] intArr = new int[10]

you can also create an array and populate the values in one line like this

int[] intArr = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; 
// now you can call your method and pass the array you created
Output outputObj = new Output(intArr);

huangapple
  • 本文由 发表于 2020年3月17日 02:28:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/60711368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定