Mình có frameA với 1 TextFeild , 1 Button . frameB với 1 TextArea .Khi nhập dữ liệu trong TextFeild của frameA sau đó ấn nút button hiển thị trong TextArea của frameB . vậy làm thế nào mọi người .Mình dùng giao diện kéo thả NetBeans
Truyền dữ liệu giữa các Frame trong Java
Mình ít dùng NetBean nên mình thử code trực tiếp trên Eclipse. Mình cũng tạo 2 frame như bạn nói.
Trong đó class Frame1 của mình sẽ có JTextArea để nhận dữ liệu, Frame2 sẽ có JButton và JTextField.
class Frame2:
package Test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Frame2 extends JFrame {
private JLabel label;
private JTextField textField;
private JButton button;
private GroupLayout layout;
public Frame2() {
this.initialize();
}
private void initialize() {
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400, 150);
label = new JLabel("Label");
textField = new JTextField();
button = new JButton("Button");
layout = new GroupLayout(this.getContentPane());
this.getContentPane().setLayout(layout);
layout.setAutoCreateContainerGaps(true);
layout.setAutoCreateGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addComponent(label)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(textField)
.addComponent(button))
);
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label)
.addGroup(layout.createSequentialGroup()
.addComponent(textField)
.addComponent(button))
);
}
public JButton getButton() {
return button;
}
public String getText() {
return textField.getText();
}
}
class Frame1:
package Test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Frame1 extends JFrame {
private GroupLayout layout;
private JTextArea text;
private Frame2 frame2;
public Frame1() {
this.initialize();
}
private void initialize() {
this.getContentPane().setLayout(new BorderLayout());
text = new JTextArea();
this.add(text, BorderLayout.CENTER);
frame2 = new Frame2();
frame2.getButton().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
text.setText(frame2.getText());
}
});
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class Main:
package Test;
import java.awt.EventQueue;
public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame1();
}
});
}
}
Lưu ý: đây chỉ là code mẫu để tham khảo, làm thực tế thì chẳng ai viết như thế này cả
public JButton getButton() {
return button;
}
vì người tái sử dụng code này có thể điều khiển button bên trong frame 2.
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?