按下按钮等于打印标签上JTextField上的内容 [英] Press on button equals print what ever is on JTextField on the label
问题描述
我想将我的JButton
称为enter函数,作为我的JTextField
的enter.因此,就像我穿上按下Enter键一样,它将把我在JTextField
中写下的内容放在JLabel
中.
I want to make my JButton
called enter function as enter for my JTextField
. So like if I wear to press on the enter button it would put what I wrote down in JTextField
in the the JLabel
.
这是我的代码:
package Main_Config;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class SET_UP extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JLabel consol;
public static Dimension size = new Dimension(800, 700);
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SET_UP frame = new SET_UP();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public SET_UP() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(370, 70, 0, 0);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
setSize(size);
setLocationRelativeTo(null);
textField = new JTextField();
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String input = textField.getText();
consol.setText(input);
}
});
textField.setBounds(10, 452, 243, 20);
contentPane.add(textField);
textField.setColumns(10);
JButton enter = new JButton("Enter");
enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {}
});
enter.setBounds(253, 452, 89, 20);
contentPane.add(enter);
JLabel consol = new JLabel("");
consol.setBounds(0, 483, 335, 189);
contentPane.add(consol);
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(352, 451, 200, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New button");
btnNewButton_1.setBounds(584, 451, 200, 23);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("New button");
btnNewButton_2.setBounds(0, 0, 89, 23);
contentPane.add(btnNewButton_2);
}
}
推荐答案
我想将名为Enter函数的JButton用作JTextField的enter.
I want to make my JButton called enter function as enter for my JTextField.
您在文本字段中添加了ActionListener
,但是现在您需要共享ActionListener
与文本字段和按钮.
You added an ActionListener
to the text field, but now you need to share the ActionListener
with the text field and the button.
所以您的代码将类似于:
So your code would be something like:
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String input = textField.getText();
consol.setText(input);
}
};
...
textField.addActionListener(al);
enter.addActionListener(al);
现在,如果焦点位于测试字段上并且您使用Enter键,则会调用ActionListener
.或者,如果您单击"Enter"按钮,则会调用相同的ActionListener
.
Now if focus is on the test field and you use the Enter key the ActionListener
is invoked. Or if you click on the "Enter" button, the same ActionListener
is invoked.
这篇关于按下按钮等于打印标签上JTextField上的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!