Java 按下按钮等于打印标签上JTextField上的内容
我想将我的Java 按下按钮等于打印标签上JTextField上的内容,java,swing,jbutton,jtextfield,Java,Swing,Jbutton,Jtextfield,我想将我的JButton调用的enter函数作为我的JTextField的enter。因此,如果我按enter按钮,它会将我在JTextField中写下的内容放在JLabel中 这是我的密码: package Main_Config; import java.awt.*; import javax.swing.*; import javax.swing.border.*; import java.awt.event.*; public class SET_UP extends JFrame
JButton
调用的enter函数作为我的JTextField
的enter。因此,如果我按enter按钮,它会将我在JTextField
中写下的内容放在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);
}
}
我想让我的JButton调用enter函数作为JTextField的enter
您在文本字段中添加了一个ActionListener
,但现在需要共享ActionListener
使用文本字段和按钮
因此,您的代码类似于:
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String input = textField.getText();
consol.setText(input);
}
};
...
textField.addActionListener(al);
enter.addActionListener(al);
现在,如果焦点在测试字段上,并使用Enter键,则调用
ActionListener
。或者,如果单击“回车”按钮,将调用相同的ActionListener
。请查看上的教程(现在提供了链接)。这一切都是为了你的学习乐趣。然后,如果仍然卡住,请告诉我们到底是什么让您困惑,以及您的代码是如何工作的,或者它显示了哪些错误或异常。最后,您的代码没有任何缩进,这使得它完全正确,几乎无法阅读、理解和调试。请重新格式化您发布的代码,给它适当的缩进,通常每个块3个空格,并确保同一块上的所有代码都在相同的缩进级别。非常感谢您在这方面的合作,这可能会提高您获得适当和及时答案的机会。很抱歉,我的格式不允许我这样做,谢谢您的回答。我会做家务来阅读java教程。但是我仍然需要帮助,所以所有的答案都是好的不过。您所发布的只是GUI框架代码和一个带有空操作侦听器的按钮。你至少可以让我们看看你解决这个问题的方法。同样,教程是很好的资源,可能会向您展示第一次尝试所需的所有知识。看看他们。试试看。试试看。投票关闭。JavaGUI可能必须在多种平台上工作,在不同的屏幕分辨率上&使用不同的PLAF。因此,它们不利于部件的精确放置。要为健壮的GUI组织组件,请改为使用布局管理器,或者与布局填充和边框一起使用。但是,在学习该语言的基础知识时,暂时将GUI放在一边。