我应该在主要方法中输入什么

时间:2020-03-16 18:28:05

标签: java user-interface

我有一个输出类,它从输入类接收数组。然后,将数组更改为输出类中的标签。我的输出类的main方法出错。它可能需要处理输入类之间的连接。我应该在输出类的main方法中添加什么来解决该错误?

输入代码:

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);

输出类别代码:

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[]);
}

请记住,这是gui。错误在上面的行中。 int []输入不正确,但是我不知道是什么。

1 个答案:

答案 0 :(得分:0)

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

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

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

int[] intArr = new int[10]

您还可以创建一个数组并像这样在一行中填充值

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);