如何通过单击JButton从JtextField打印文本

时间:2019-05-30 16:58:22

标签: java swing jbutton jtextfield

我正在为自己创建一个小程序来学习Java,现在我确实使用12 JTextFields制作了一个GUI,现在我想使用另一个类的文本字段中的文本来打印它们。 。

当我成功按下按钮类print时,提示我在控制台中得到了最后一个System.out.println("Test if class is triggerd"),但是在控制台中却没有得到JTextFields的输出

enter image description here

第1类框架


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 :(得分:0)

之所以发生这种情况,是因为您在打印类上创建了一个新的frame1对象,因此默认情况下变量为null。您可以在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列表here

的一些知识

如果您正在努力寻找我为您准备的代码

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

和印刷课:

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