将 JButton 操作从一个 class 传递到另一个 class 中的 JTextfield
Passing JButton action from one class to JTextfield in another class
我正在试验 JButton 的操作,我正在尝试使用 class Test1 中的按钮清除 class 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 class 中的按钮时,Test2 class 中的文本框没有被清除。希望得到各位前辈的指点。如果我的问题有任何缺点,也请告诉我。非常感谢。
用这个
更新了你的Reset()
方法
public void Reset(){
t1.setText("");
}
您不能在重置中再次实例化文本字段 method.Because 框架包含文本 field.Please 阅读 Java 编码规则使用 reset()
而不是 Reset()
已更新
还有一件事是您使用 Test2
的两个瞬间。所以你在 Test2
中制作你的 start()
方法
public void start() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
//test2 window = new test2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
我正在试验 JButton 的操作,我正在尝试使用 class Test1 中的按钮清除 class 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 class 中的按钮时,Test2 class 中的文本框没有被清除。希望得到各位前辈的指点。如果我的问题有任何缺点,也请告诉我。非常感谢。
用这个
更新了你的Reset()
方法
public void Reset(){
t1.setText("");
}
您不能在重置中再次实例化文本字段 method.Because 框架包含文本 field.Please 阅读 Java 编码规则使用 reset()
而不是 Reset()
已更新
还有一件事是您使用 Test2
的两个瞬间。所以你在 Test2
中制作你的 start()
方法
public void start() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
//test2 window = new test2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}