Как напечатать текст из JtextField, нажав JButton - PullRequest
0 голосов
/ 30 мая 2019

Я создаю небольшую программу для самостоятельного изучения Java, сейчас я сделал графический интерфейс с 12 JTextFields, и теперь я хочу использовать текст, который находится в текстовых полях другого класса, где я их распечатываю.

Когда я нажимаю кнопку, класс print успешен, поэтому я получаю последний System.out.println("Test if class is triggerd") в своей консоли, но я не получаю JTextFields вывод в консоли

enter image description here

Класс Frame1


import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class Frame1 {

    public JFrame frmCratingRecipieGenerator;
    public JTextField txtSlot;
    public JTextField textField_1;
    public JTextField textField_2;
    public JTextField textField_3;
    public JTextField textField_4;
    public JTextField textField_5;
    public JTextField textField_6;
    public JTextField textField_7;
    public JTextField textField_8;
    public JTextField textField_9;
    public JTextField textField_10;
    public JTextField textField_11;
    public JLabel lblNewLabel;
    public String slot1;
    public String slot2;
    public String slot3;
    public String slot4;
    public String slot5;
    public String slot6;
    public String slot7;
    public String slot8;
    public String slot9;
    public String output;
    public String output_count;
    public String file_num;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Frame1 window = new Frame1();
                    window.frmCratingRecipieGenerator.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Frame1() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    public void initialize() {
        frmCratingRecipieGenerator = new JFrame();
        frmCratingRecipieGenerator.setResizable(false);
        frmCratingRecipieGenerator.setTitle("Crating Recipie Generator Alpha 1");
        frmCratingRecipieGenerator.getContentPane().setBackground(new Color(192, 192, 192));
        frmCratingRecipieGenerator.setBounds(100, 100, 452, 248);
        frmCratingRecipieGenerator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmCratingRecipieGenerator.getContentPane().setLayout(null);

        txtSlot = new JTextField();
        txtSlot.setBounds(10, 19, 86, 20);
        frmCratingRecipieGenerator.getContentPane().add(txtSlot);
        txtSlot.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setBounds(106, 19, 86, 20);
        frmCratingRecipieGenerator.getContentPane().add(textField_1);
        textField_1.setColumns(10);

        textField_2 = new JTextField();
        textField_2.setBounds(202, 19, 86, 20);
        frmCratingRecipieGenerator.getContentPane().add(textField_2);
        textField_2.setColumns(10);

        textField_3 = new JTextField();
        textField_3.setBounds(10, 50, 86, 20);
        frmCratingRecipieGenerator.getContentPane().add(textField_3);
        textField_3.setColumns(10);

        textField_4 = new JTextField();
        textField_4.setBounds(106, 50, 86, 20);
        frmCratingRecipieGenerator.getContentPane().add(textField_4);
        textField_4.setColumns(10);

        textField_5 = new JTextField();
        textField_5.setBounds(202, 50, 86, 20);
        frmCratingRecipieGenerator.getContentPane().add(textField_5);
        textField_5.setColumns(10);

        textField_6 = new JTextField();
        textField_6.setBounds(10, 81, 86, 20);
        frmCratingRecipieGenerator.getContentPane().add(textField_6);
        textField_6.setColumns(10);

        textField_7 = new JTextField();
        textField_7.setBounds(106, 81, 86, 20);
        frmCratingRecipieGenerator.getContentPane().add(textField_7);
        textField_7.setColumns(10);

        textField_8 = new JTextField();
        textField_8.setBounds(202, 81, 86, 20);
        frmCratingRecipieGenerator.getContentPane().add(textField_8);
        textField_8.setColumns(10);

        textField_9 = new JTextField();
        textField_9.setBounds(351, 19, 86, 20);
        frmCratingRecipieGenerator.getContentPane().add(textField_9);
        textField_9.setColumns(10);

        textField_10 = new JTextField();
        textField_10.setBounds(351, 81, 46, 20);
        frmCratingRecipieGenerator.getContentPane().add(textField_10);
        textField_10.setColumns(10);

        textField_11 = new JTextField();
        textField_11.setText("1");
        textField_11.setBounds(351, 50, 46, 20);
        frmCratingRecipieGenerator.getContentPane().add(textField_11);
        textField_11.setColumns(10);

        JLabel lblFileId = new JLabel("File ID");
        lblFileId.setFont(new Font("Tahoma", Font.PLAIN, 16));
        lblFileId.setHorizontalAlignment(SwingConstants.LEFT);
        lblFileId.setBounds(298, 82, 86, 14);
        frmCratingRecipieGenerator.getContentPane().add(lblFileId);

        JLabel lblOutput = new JLabel("Output");
        lblOutput.setFont(new Font("Tahoma", Font.PLAIN, 16));
        lblOutput.setBounds(298, 20, 58, 14);
        frmCratingRecipieGenerator.getContentPane().add(lblOutput);

        JLabel lblCount = new JLabel("Count");
        lblCount.setFont(new Font("Tahoma", Font.PLAIN, 16));
        lblCount.setBounds(298, 51, 52, 14);
        frmCratingRecipieGenerator.getContentPane().add(lblCount);


        JLabel lblThisJavaProgram = new JLabel("This Java program is coded by Jason");
        lblThisJavaProgram.setFont(new Font("Tahoma", Font.PLAIN, 16));
        lblThisJavaProgram.setBounds(10, 179, 270, 20);
        frmCratingRecipieGenerator.getContentPane().add(lblThisJavaProgram);

        JLabel TestLabel = new JLabel("");
        TestLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
        TestLabel.setBounds(391, 179, 46, 19);
        frmCratingRecipieGenerator.getContentPane().add(TestLabel);

        JButton btnNewButton = new JButton("Generate the McFunction File's");
        btnNewButton.setBounds(10, 108, 427, 60);
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                TestLabel.setText(textField_10.getText());              
                print name = new print();
                name.Print();
            }
        });
        frmCratingRecipieGenerator.getContentPane().add(btnNewButton);

        lblNewLabel = new JLabel("Last File num");
        lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
        lblNewLabel.setBounds(287, 182, 110, 14);
        frmCratingRecipieGenerator.getContentPane().add(lblNewLabel);


        this.slot1 = txtSlot.getText();
        this.slot2 = textField_1.getText();
        this.slot3 = textField_2.getText();;
        this.slot4 = textField_3.getText();;
        this.slot5 = textField_4.getText();;
        this.slot6 = textField_5.getText();;
        this.slot7 = textField_6.getText();;
        this.slot8 = textField_7.getText();;
        this.slot9 = textField_8.getText();;
        this.output = textField_9.getText();
        this.output_count = textField_11.getText();
        this.file_num = textField_10.getText();



        }


}

Класс печати

package example;

public class print {
    public void Print() {
        Frame1 slots =  new Frame1();
        String slot1 = slots.slot1;
        String slot2 = slots.slot2;
        String slot3 = slots.slot3;
        String slot4 = slots.slot4;
        String slot5 = slots.slot5;
        String slot6 = slots.slot6;
        String slot7 = slots.slot7;
        String slot8 = slots.slot8;
        String slot9 = slots.slot9;
        String output = slots.output;
        String output_count = slots.output_count;
        String file_num = slots.file_num;

        System.out.println(slot1);
        System.out.println(slot2);
        System.out.println(slot3);
        System.out.println(slot4);
        System.out.println(slot5);
        System.out.println(slot6);
        System.out.println(slot7);
        System.out.println(slot8);
        System.out.println(slot9);
        System.out.println(output);
        System.out.println(output_count);
        System.out.println(file_num);
        System.out.println("Test if class is triggerd");
    }


}

1 Ответ

0 голосов
/ 30 мая 2019

Это происходит потому, что вы создаете в классе печати новый объект frame1, поэтому переменные по умолчанию равны нулю.Вы можете добавить в метод Print параметр списка, который хранит все строки в этом списке.

public void Print(List<String> slots) {
    System.out.println(slots.get(0));
    System.out.println(slots.get(1));
    System.out.println(slots.get(2));
    System.out.println(slots.get(3));
    System.out.println(slots.get(4));
    System.out.println(slots.get(5));
    System.out.println(slots.get(6));
    System.out.println(slots.get(7));
    System.out.println(slots.get(8));
    System.out.println(slots.get(9));
    System.out.println(slots.get(10));
    System.out.println(slots.get(11));
}

и переопределить, прежде чем метод вызовет все переменные и сохранит их в списке.

Вы можете узнать кое-что о списках Java здесь

Если вы не можете найти код, который я сделал для вас

            slot1 = txtSlot.getText();
            slot2 = textField_1.getText();
            slot3 = textField_2.getText();;
            slot4 = textField_3.getText();;
            slot5 = textField_4.getText();;
            slot6 = textField_5.getText();;
            slot7 = textField_6.getText();;
            slot8 = textField_7.getText();;
            slot9 = textField_8.getText();;
            output = textField_9.getText();
            List<String> slots = new ArrayList<String>();
            slots.add(slot1);
            slots.add(slot2);
            slots.add(slot3);
            slots.add(slot4);
            slots.add(slot5);
            slots.add(slot6);
            slots.add(slot7);
            slots.add(slot8);
            slots.add(slot9);
            slots.add(output);
            slots.add(output_count);
            slots.add(file_num);
            output_count = textField_11.getText();
            file_num = textField_10.getText();
            TestLabel.setText(textField_10.getText());     

            print name = new print();

            name.Print(slots);

и Printкласс:

public void Print(List<String> slots) {


    System.out.println(slots.get(0));
    System.out.println(slots.get(1));
    System.out.println(slots.get(2));
    System.out.println(slots.get(3));
    System.out.println(slots.get(4));
    System.out.println(slots.get(5));
    System.out.println(slots.get(6));
    System.out.println(slots.get(7));
    System.out.println(slots.get(8));
    System.out.println(slots.get(9));
    System.out.println(slots.get(10));
    System.out.println(slots.get(11));
    System.out.println("Test if class is triggerd");
}
...