如何从构造函数中创建的JtextField获取值

时间:2018-05-08 08:54:25

标签: java jframe

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

public class GUI extends JFrame {
public static JButton solveButton = new JButton("Solve");

public GUI() {
    // Create panel for Sudoku
    JPanel board = new JPanel();
    board.setLayout(new GridLayout(3, 3));
    board.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    for (int row = 0; row < 3; ++row) {
        for (int col = 0; col < 3; ++col) {
            JPanel box = new JPanel(new GridLayout(3, 3));
            box.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            for (int cell = 0; cell < 9; ++cell) {
                box.add(new JTextField(2));
            }
            board.add(box);
        }
    }

    // Create menu panel
    JPanel menu = new JPanel();
    menu.setLayout(new GridBagLayout());
    GridBagConstraints menuConstraints = new GridBagConstraints();

    menuConstraints.anchor = GridBagConstraints.WEST;
    menuConstraints.weightx = 0.5;
    menuConstraints.weighty = 0.5;
    menuConstraints.gridwidth = 2;

    menuConstraints.gridx = 2;
    menuConstraints.gridy = 2;
    menu.add(solveButton, menuConstraints);


    menuConstraints.fill = GridBagConstraints.HORIZONTAL;
    menuConstraints.weightx = 0.5;
    menuConstraints.weighty = 0.5;
    menuConstraints.gridwidth = 2;
    menuConstraints.gridx = 0;
    menuConstraints.gridy = 5;

    // Create output panel
    JTextArea output = new JTextArea(10, 15);
    output.setEditable(false);
    output.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLUE), "Output Area"));

    // Join the 3 panels on the frame
    Container cp = getContentPane();
    cp.setLayout(new BorderLayout());

    cp.add(board, BorderLayout.WEST);
    cp.add(menu, BorderLayout.EAST);
    cp.add(output, BorderLayout.SOUTH);
}

public static void main(String[] args) {
    // Create a frame and set its properties
    JFrame frame = new GUI();
    frame.setTitle("Sudoku Solver");
    frame.setSize(600, 600);
    frame.setLocationRelativeTo(null); // Center the frame

    // Setup the window
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

    solveButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("I was pressed");
        }
    });
}

}

我有一个为数独求解器设计的GUI。如何从JTextField获取所有值并将它们存储在2x2数组中。

我想将所有值存储在2x2数组中,然后将这些值传递给我为解决数独而制作的类。

我不知道在按下saveButton时如何从文本字段中获取所有值。

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

我认为你可以设置一个必须是唯一值的textfield数组的id。

您可以创建一个由;

定义的texfield数组
JTextField[]  txtfields = new JTextField[3*3*9];

并且有一个增量值的计数器。

int cnt = 0;
for (int row = 0; row < 3; ++row) {
    for (int col = 0; col < 3; ++col) {
        JPanel box = new JPanel(new GridLayout(3, 3));
        box.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        for (int cell = 0; cell < 9; ++cell) {
            txtfields[cnt] = new JTextField(2);
            box.add(txtfields[cnt++]);
        }
        board.add(box);
    }
}

您可以在动作侦听器中按相同顺序找到文本字段的某些值,然后将其显示到文本区域。

solveButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("I was pressed");
        StringBuilder stbuld = new StringBuilder();
        int cnt = 0;
        for (int row = 0; row < 3; ++row) {
            for (int col = 0; col < 3; ++col) {
                for (int cell = 0; cell < 9; ++cell) {
                    String value = txtfields[cnt].getText();

                    if(value != null && !value.equals(""))
                    {
                        System.out.println(row + ":" + col + ":" + ":" + cell + ":" + value);
                        stbuld.append("["+row + "][" + col + "][" + cell + "] = " + value + "\n");
                    }

                    cnt++;
                }
            }
        }

        output.setText(stbuld.toString());
    }
});

输出jframe如下。

enter image description here

有一个好的编码...