如何创建文本框以在按下按钮时打印文本

How to create a text box to print text when button is pressed

我是 Java 的新手。我正在尝试创建一个文本框来打印我制作的数组。我不确定要写什么或如何着手?

任何帮助都会很棒。

您必须向您的按钮添加一个 ActionListener。这将允许您在按下时执行操作。

JButton button = new JButton();
JTextField textField = new JTextField();

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt)
    {
       //set text to whatever in array probably in for loop
       textField.setText( getArrayValues(array) );
    }
});

...

public String getArrayValues(int[] array)
{
   String value = new String();
   for(int i = 0; i < array.length-1; i++)
   {
      value += i + ", ";
   }
   value += i;
   return value;
}

你应该有这样的东西:

JFrame frame = new JFrame();
frame.setSize(300, 300);
frame.setLayout(null);
JTextField field = new JtextField();
field.setBounds(10, 10, width, height)
field.setText(Arrays.toString(yourArray));
frame.add(field);

按下按钮设置文本,参见Orin的回答