将JButton操作从一个类传递到另一个类中的JTextfield

时间:2017-11-21 19:03:47

标签: java jtextfield windowbuilder

我正在试验JButton的动作,我试图使用Test1类中的按钮清除Test2类中的文本字段。这是代码

public class Test2 {
private JFrame frame;
private JTextField t1;
private JTextField t2;

/**
 * Launch the application.
 */
public void start() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                test2 window = new test2();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public test2() {
    initialize();
}
public void Reset(){
    t1 = new JTextField();
    t1.setText("");

}
/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    t1 = new JTextField();
    t1.setColumns(10);
    t1.setText("Start");


    GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
    groupLayout.setHorizontalGroup(
        groupLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(groupLayout.createSequentialGroup()
                .addGap(35)
                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                    .addComponent(t2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(t1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addContainerGap(281, Short.MAX_VALUE))
    );
    groupLayout.setVerticalGroup(
        groupLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(groupLayout.createSequentialGroup()
                .addGap(38)
                .addComponent(t1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addGap(75)
                .addComponent(t2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(98, Short.MAX_VALUE))
    );
    frame.getContentPane().setLayout(groupLayout);
}

}

public class Test1 {

private JFrame frame;



/**
 * Launch the application.
 */
static test1 window = new test1();
static test2 window2 = new test2();
private JTextField textField;
private JTextField ownText;
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                window2.start();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 * @wbp.parser.entryPoint
 */
public test1() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String test ="";
            window2.Reset(test);

        }
    });
    frame.getContentPane().add(btnNewButton, BorderLayout.WEST);

    ownText = new JTextField();
    frame.getContentPane().add(ownText, BorderLayout.EAST);
    ownText.setColumns(10);


}

}

目前,当我单击Test1类中的按钮时,Test2类中的文本字段不会被清除。希望得到所有老年人的建议。如果我的问题有任何缺点,请告诉我。非常感谢你。

1 个答案:

答案 0 :(得分:0)

使用此

更新了您的Reset()方法
public void Reset(){
        t1.setText("");     
}

您无法在Reset方法中再次实例化文本字段。因为框架包含文本字段。请阅读Java编码规则使用reset()而不是Reset()

已更新 还有一件事是你使用Test2的两个瞬间。因此,您可以在start()中使用Test2方法

public void start() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                //test2 window = new test2();
                 frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}