如何在java中的文本字段上显示输出?

How to display output on textfield in java?

我又得到了这段代码,我卡在了必须将数组中的数字之一打印到 swing 文本字段的部分。我让它在 awt 中工作了一次,但我似乎无法让它在 swing 中工作。这是代码:

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Koodibaasitäiustus extends JFrame {
/**
 * 
 */
    private static final long serialVersionUID = 1L;
    private JTextField tfCount;

    public Koodibaasitäiustus() {
        Container cp = getContentPane();
        cp.setLayout(new FlowLayout());
        setResizable(false);
        cp.add(new JLabel("enter code"));
        tfCount = new JTextField("0", 10);
        tfCount.setEditable(true);
        cp.add(tfCount);
        JButton nupp = new JButton("get answer");
        cp.add(nupp);
        nupp.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                int[] array;
                array = new int[30];
                array[0] = 111111;
                array[1] = 888888;
                array[2] = 333333;
                array[3] = 555555;
                array[4] = 111111;
                array[5] = 643208;
                array[6] = 643208;
                array[7] = 643208;
                array[8] = 643208;
                array[9] = 643208;
                array[10] = 643208;
                array[11] = 643208;
                array[12] = 643208;
                array[13] = 643208;
                array[14] = 643208;
                array[15] = 643208;
                array[16] = 643208;
                array[17] = 643208;
                array[18] = 643208;
                array[19] = 643208;
                array[20] = 643208;
                array[21] = 643208;
                array[22] = 643208;
                array[23] = 643208;
                array[24] = 643208;
                array[25] = 643208;
                array[26] = 643208;
                array[27] = 643208;
                array[28] = 643208;
                array[29] = 643208;
                array[30] = 643208;
                int number = Integer.parseInt(tfCount.getText());
                tfCount.setText(String.valueOf(array[number]));

            }
        });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Koodibaas");
        setSize(300, 100);
        setVisible(true);
        }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Koodibaasitäiustus();
            }
        });
    }
}

您的密码是"good"

array[30] = 643208; 行会产生问题,因为您的数组只能包含 30 个元素...从 0 到 29

如果您注释掉或删除此行...一切都会正常